- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: ksh question
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
02-20-2003 08:24 AM
02-20-2003 08:24 AM
What is the easiest way in ksh to get the results of a command into a variable?
For example I want to issue a umount of a directory. I know I can interogate the return code to see if it's successful.
However I want to grab the output if it's unsucessful to help determine why.
For example if you umount a directory that isn't mounted you get this result:
-> umount /tmp/sean2343234
umount: cannot find /tmp/sean2343234 in /etc/mnttab
cannot unmount /tmp/sean2343234
While a failure because it is in use gives:
-> umount /tmp
umount: cannot unmount /tmp : Device busy
I need to take different actions based on why it failed so I need to catch the results of the command.
This is fairly urgent as I have to have this script in production on Saturday.
TIA,
Sean
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 08:32 AM
02-20-2003 08:32 AM
Re: ksh question
Since well-behaved commands write errors to stderr:
# umount /cdrom 2> /tmp/error.$$
...and then if you wanted to put the contents of /tmp/error.$$ into a variable, do:
# ERRTXT=`
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 08:34 AM
02-20-2003 08:34 AM
Re: ksh question
umount /tmp/sean2343234 2> /umount.err
error=$(cat /umount.err)
The first statement will redirect standard error to /umount.err and the second will assign the contents of /umount.err to the variable error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 08:35 AM
02-20-2003 08:35 AM
Re: ksh question
export UMERR=`umount /tmp 2>&1`
This will set the value of UMERR to the output of the command.
The only problem that I can think of with this is that it joins all the output into a single line.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 08:37 AM
02-20-2003 08:37 AM
Re: ksh question
..
ERRORLOG=/tmp/myerror.log
..
umount /tmp 2>$ERRORLOG
..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 08:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 08:39 AM
02-20-2003 08:39 AM
Re: ksh question
I would do the following.
umount $mount_poing > /tmp/umount$$ > /dev/null 2>&1
grep "Device busy" /tmp/umount$$ > /dev/null 2>&1
if [ $? = 0 ]
then
your_busy_action
else
if
Grep "cannot find" /tmp/umount$$ > /dev/null 2>&1
if [ $? = 0 ]
then
your_mnttab_action
fi
fi
fi
etc.,
Some of the commands exit with appropriate exit codes. For ex., "useradd". It will be much easier to deal with them. So a simple case statement can effectively cover them.
But for comands like mount, we will have to do something like the above.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2003 10:20 AM
02-20-2003 10:20 AM
Re: ksh question
VAR=$(/usr/sbin/umount /mount/point 2>&1)
I was trying this and couldn't get the syntax right.
Thanks much!