shanenin Posted April 22, 2005 Report Share Posted April 22, 2005 I want to use this function in my bash scriptcheck-nfs (){echo "checking"mount | grep 192.168.1 > /dev/nullif [ "$?" = 0 ]then echo "NFS is mounted"else echo "NFS is not mounted" fi}depending on the output either "NFS is mounted" or "NFS is not mounted" how could i use that output as a condition in an other if else statement? Sorry if this is not clear. Quote Link to post Share on other sites
jcl Posted April 22, 2005 Report Share Posted April 22, 2005 (edited) You can pipe the function as you would any utility, but if you only want to know whether NFS is mounted and don't care about the output of the function it might be easier to use 'return'. 'return' will cause the function to immediately return to its caller with a specifiable exit status; as usual $? can be used to access the exit status. For examplecheck-nfs (){ echo "checking" mount | grep 192.168.1 > /dev/null if [ "$?" = 0 ] then echo "NFS is mounted" return 0 else echo "NFS is not mounted" return 1 fi}which could be used like socheck-nfsif [ "$?" = 0 ]then # do something with NFSelse # mount NFS or panicfi Edited April 22, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted April 22, 2005 Author Report Share Posted April 22, 2005 return was what I was looking for, Thanks :-) I originally tried to use some other ways that did not work. I will give you guys something to chuckle at here are some failed attemps:check-nfs (){echo "checking"mount | grep 192.168.1 > /dev/nullif [ "$?" = 0 ]then echo "NFS is mounted" exit 0 #this gave me the correct return, but had the negative effect of stoping my scriptelse echo "NFS is not mounted" exit 1fi}I also tried this, I was just reaching for anything that might work ;-) check-nfs (){echo "checking"mount | grep 192.168.1 > /dev/nullif [ "$?" = 0 ]then echo "NFS is mounted" $?=0else echo "NFS is not mounted" $?=1fi} Quote Link to post Share on other sites
jcl Posted April 22, 2005 Report Share Posted April 22, 2005 (edited) exit was the first thing I considered too The second one was a good idea; too bad the shell is a bit inconsistent about how it handles variables. Many rather useful variables are untouchable, often because of syntax issues. Edited April 22, 2005 by jcl Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.