- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell script help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2009 01:11 PM
05-19-2009 01:11 PM
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.
Solved! Go to Solution.
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2009 01:15 PM
05-19-2009 01:15 PM
Re: Shell script help
./find[6]: 0403-057 Syntax error at line 8 : `then' is not expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2009 01:27 PM
05-19-2009 01:27 PM
Re: Shell script help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2009 01:36 PM
05-19-2009 01:36 PM
Re: Shell script help
Syntax error at line 9 : `'' is not matched.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2009 01:37 PM
05-19-2009 01:37 PM
Re: Shell script help
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2009 01:42 PM
05-19-2009 01:42 PM
Re: Shell script help
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 01:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 08:00 AM
05-20-2009 08:00 AM
Re: Shell script help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 08:52 AM
05-20-2009 08:52 AM
Re: Shell script help
The '-local' option isn't valid for AIX. You might use:
# find / ! -fstype nfs \( -nouser -o -nogroup \)
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 10:16 AM
05-20-2009 10:16 AM
Re: Shell script help
Cannot open file /proc/548988.
any ideas on how to include it as well in the find.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 10:19 AM
05-20-2009 10:19 AM
Re: Shell script help
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 10:40 AM
05-20-2009 10:40 AM
Re: Shell script help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2009 10:55 AM
05-20-2009 10:55 AM
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.
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 05:59 AM
05-21-2009 05:59 AM
Re: Shell script help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 06:12 AM
05-21-2009 06:12 AM
Re: Shell script help
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 06:22 AM
05-21-2009 06:22 AM
Re: Shell script help
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 06:58 AM
05-21-2009 06:58 AM
Re: Shell script help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 07:11 AM
05-21-2009 07:11 AM
Re: Shell script help
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2009 07:35 AM
05-21-2009 07:35 AM
Re: Shell script help
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...