1833760 Members
2190 Online
110063 Solutions
New Discussion

Shell script help

 
SOLVED
Go to solution
shell script
Advisor

Shell script help

Hello,

I am trying to find unowned files on hundreds of servers using the following script. I am new to scripting and have an error. could someone help me in debugging it please.

#!/usr/bin/sh
OSTYPE=`uname -a | cut -d" " -f1`
print "MSG: OS type is $OSTYPE."
LOGFILE=/tmp/unowned_file

if [[ $OSTYPE = "HP-UX"]]
then
find /-xdev -nouser -o -nogroup >> ${LOGFILE}

if [[ $? = "0" ]]
print "MSG: No unowned files found." >> ${LOGFILE}
else
print "MSG: Unowned files found" >> ${LOGFILE}
fi
elif [[ $OSTYPE = "AIX"]]
then
find / -nouser -o -nogroup > ${LOGFILE}
print "MSG: No unowned files found." > ${LOGFILE}
else
print "MSG: Unowned files found" > ${LOGFILE}
fi
exit 0


Thanks.
18 REPLIES 18
shell script
Advisor

Re: Shell script help

sorry, forgot to post the error.


./find[6]: 0403-057 Syntax error at line 8 : `then' is not expected.
Peter Nikitka
Honored Contributor

Re: Shell script help

Hi,

one cause is the missing 'then' in your statement, checking the return value.
Next, there is no space between the directory parameter '/' and the find-option(s).
More, there is no need to diddle with 'uname -a'.

Try:
#!/usr/bin/sh
OSTYPE=`uname -s`
print "MSG: OS type is $OSTYPE."
LOGFILE=/tmp/unowned_file

case $OSTYPE in
HP-UX) findopt='-xdev '\( -nouser -o -nogroup \)' ;;
AIX) findopt='-local \( -nouser -o -nogroup \)' ;;
*) findopt='\( -nouser -o -nogroup \)' ;;
esac

find / $findopt -print > ${LOGFILE}

if [ -s $LOGFILE ]; then
print "MSG: No unowned files found."
else
print "MSG: Unowned files found"
fi >> ${LOGFILE}

exit 0

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
shell script
Advisor

Re: Shell script help

Thanks but i still have an error.

Syntax error at line 9 : `'' is not matched.
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi:

This appears to be a continuation of your thread here:

http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1341268

If so, you should continue your queries there.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again):

> Syntax error at line 9 : `'' is not matched.

Yes, you have an extra single quote in the line _before_ that.

You have:

HP-UX) findopt='-xdev '\( -nouser -o -nogroup \)' ;;

...which should be:

HP-UX) findopt='-xdev \( -nouser -o -nogroup \)' ;;

...

Regards!

...JRF...
Peter Nikitka
Honored Contributor
Solution

Re: Shell script help

Thanks, JRF -

I should have used my glasses ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
shell script
Advisor

Re: Shell script help

Well...now i have an AIX error.
find: 0652-017 -local\( is not a valid option. If i remove "-local" i get an error for " There is a missing conjunction". Please help.


#!/usr/bin/sh
OSTYPE=`uname -s`
LOGFILE=/tmp/unowned_file

case $OSTYPE in
HP-UX) findopt='-xdev \( -nouser -o -nogroup \)' ;;
AIX) findopt='-local\( -nouser -o -nogroup \' ;;
*) findopt='\( -nouser -o -nogroup \)' ;;
esac

find / $findopt -print > ${LOGFILE}

if [ -s $LOGFILE ]; then
print "MSG: No unowned files found."
else
print "MSG: Unowned files found"
fi >> ${LOGFILE}

exit 0
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again):

The '-local' option isn't valid for AIX. You might use:

# find / ! -fstype nfs \( -nouser -o -nogroup \)

Regards!

...JRF...
shell script
Advisor

Re: Shell script help

Ok, everything works fine. Lastly i have one error
Cannot open file /proc/548988.

any ideas on how to include it as well in the find.
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again):

> Cannot open file /proc/548988.

The '/proc' filesystem does isn't implemented by HP-UX. You will find it in AIX and in Linux (of course).

Regards!

...JRF...
shell script
Advisor

Re: Shell script help

Right i am getting that error on AIX boxes, how do I exclude that filesystem or include it in the find command.
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again):

> Right i am getting that error on AIX boxes, how do I exclude that filesystem or include it in the find command.

On AIX I simply redirect STDERR to /dev/null to vanquish the "cannot open" of files in the '/proc' directory:

# find / ! -fstype nfs \( -nouser -o -nogroup \) 2> /dev/null

Regards!

...JRF...
shell script
Advisor

Re: Shell script help

Actually i have changed the script a bit. Now i want to have a message displayed if the script does not find any orphan files in the output file like no orphan files found or write to the file if they exist.

Thanks in advance.
#!/usr/bin/sh
OSTYPE=`uname -s`
TEMP=/tmp/unowned_file

case $OSTYPE in
HP-UX)
find / -xdev \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>&1;;
AIX)
find / ! -fstype nfs \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>&1;;
*)
find / -local \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>&1;;
esac

exit 0

James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi:

Per you last request:

#!/usr/bin/sh
set -u
OSTYPE=`uname -s`
TEMP=/tmp/unowned_file
rm -f ${TEMP}
case $OSTYPE in
HP-UX)
find / -xdev \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>&1;;
AIX)
find / ! -fstype nfs \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>&1;;
*)
find / -local \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>&1;;
esac
[ -s ${TEMP} ] && echo "See '${TEMP} for orphans" || echo "NO orphaned files exist"
exit 0
...

Note that I added 'set -u' to protect things when I do 'rm -f ${TEMP}.

Now, we simply do our 'find()' and examine the output file for a non-zero size.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again):

There is one correction that needs to be made. You do _not_ want to redirect STDERR to your file since this could give a false positive --- particularly in servers with a '/proc' filesystem as we discussed before.

Instead, use:

#!/usr/bin/sh
set -u
OSTYPE=`uname -s`
TEMP=/tmp/unowned_file
rm -f ${TEMP}
case $OSTYPE in
HP-UX)
find -xdev \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>/dev/null;;
AIX)
find / ! -fstype nfs \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>/dev/null;;
*)
find / -local \( -type f -o -type d \) \( -nouser -o -nogroup \) > ${TEMP} 2>/dev/null;;
esac
[ -s ${TEMP} ] && echo "See '${TEMP}' for orphans" || echo "NO orphaned files exist"
exit 0

...

Regards!

...JRF...
shell script
Advisor

Re: Shell script help

James,

The echo command is displaying the message on the screen. I want the message to go the file. I do not want any message onscreen. Please advise.

Thanks again.
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi:

> The echo command is displaying the message on the screen. I want the message to go the file. I do not want any message onscreen.

OK, then change the line that tests for a zero-size file to be:

[ -s ${TEMP} ] || echo "NO orphaned files exist" >> ${TEMP}

If there are orphaned files then they will be listed in your ${TEMP} file. If there are none, the string "NO orphaned file exist" will be present instead.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again):

If you always want output written to your file, you could condense things a bit by redirecting STDOUT and STDERR once:

#!/usr/bin/sh
set -u
OSTYPE=$(uname -s)
TEMP=/tmp/unowned_file
rm -f ${TEMP}
exec 1> ${TEMP}
exec 2> /dev/null
case $OSTYPE in
HP-UX)
find / -xdev \( -type f -o -type d \) \( -nouser -o -nogroup \)
;;
AIX )
find / ! -fstype nfs \( -type f -o -type d \) \( -nouser -o -nogroup \)
;;
* )
find / -local \( -type f -o -type d \) \( -nouser -o -nogroup \)
;;
esac
[ -s ${TEMP} ] || echo "NO orphaned files exist"
exit 0

...Too, instead of pushing STDERR to '/dev/null' you might want to substitute a file explicitly for errors.

Lastly, I suggest that you change your Forum name to something appropriate --- not "shell script" :-)

Regards!

...JRF...