1834637 Members
2568 Online
110069 Solutions
New Discussion

Re: Shell script help

 
SOLVED
Go to solution
Simon R Wootton
Regular Advisor

Shell script help

I'm wanting to write a shell script that unmounts a file system, run's a command, then re-mounts the filesystem. I've done the basic script but I need help on only running the command if the unmounting of the filesystem is successful, an appropriate error message would also help helpful for the operator. All help appreciated & rewarded, I'm not too hot on scripts at the moment but learning.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell script help

Hi Simon:

For example:

umount /cdrom
if [ "$?" -eq 0 ]; then
echo "unmounted_ok"
else
echo "NOT unmounted"
fi

Regards!

...JRF...
Simon R Wootton
Regular Advisor

Re: Shell script help

Thanks James - that'll do nicely!

Simon
Helen French
Honored Contributor

Re: Shell script help

Something like this:

umount /test
if [ "$?" = 0 ]
then
echo "unmount successful"
run_your_command
else
echo "unmount failed"
fi
Life is a promise, fulfill it!
Simon R Wootton
Regular Advisor

Re: Shell script help

What is the ?0 testing for ??

Simon
Patrick Wallek
Honored Contributor

Re: Shell script help

The $? is the return code for the process that has just run. If the return code is 0 then it ran OK. If it is anything else, an error of some sort occurred.
S.K. Chan
Honored Contributor

Re: Shell script help

$? is the return status of a previous command.
For example ..
$ date
...
# echo $?
0
You would get 0 since the date command is successful. If you get a non-zero value (for example 1) it means unsucessful.
James R. Ferguson
Acclaimed Contributor

Re: Shell script help

Hi (again) Simon:

The "$?" variable is the return value from the last command (see the man pages for 'sh-posix' for more information).

Standard return values are:

0 = success
1 = failure
2 = warning

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: Shell script help

Just wanted to let you know about && operator and a small function.

//

do_mywork()
{
echo "doing the work"
}

umount /filesystem && do_mywork && mount /filesystem


If umount /syscore is successful then it will do the work. Add as much as you want in do_mywork function. If umount is unsuccessful, the rest won't get executed and you will get the standard error of umount.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try