1843959 Members
1937 Online
110226 Solutions
New Discussion

shell programming

 
SOLVED
Go to solution
Olga_1
Regular Advisor

shell programming

I need to capture the output of 'bdf|grep /mnt', if there is nothing, then do other tasks.

Is there a better way to hadle this, other then capturing the output to a file?
13 REPLIES 13
Ted Ellis_2
Honored Contributor
Solution

Re: shell programming

you could pipe this output to awk instead of a file... and make the entire sting a variable that you can then check with an if loop

on my system the 6th field on a line returned from bdf is the mount point... if your /mnt is not mounted, it will not be returned and MNT_CHECK should be null.


MNT_CHECK = `bdf|grep /mnt | awk '{print $6}'`
echo ${MNT_CHECK} # just to see if you get what you expect...

now set up some if statement that will verify your logic and execute other tasks...

Ted
steven Burgess_2
Honored Contributor

Re: shell programming

Hi

You can check if the exit code was succesful

bdf | grep /mnt
if [ $? != 1 ]
then
task
fi

HTH

Steve
take your time and think things through
James R. Ferguson
Acclaimed Contributor

Re: shell programming

Hi:

How about:

# bdf|grep -q /mnt && echo "It's mounted!"

Regards!

...JRF...
MANOJ SRIVASTAVA
Honored Contributor

Re: shell programming

export A=`bdf | grep /mnt `

if [ $A = 0 ]

then

command


exit

fi



Manoj Srivastava
Dietmar Konermann
Honored Contributor

Re: shell programming

I wonder, how $A could ever get "0"?

IMHO, James' approach is by far the coolest. :)
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
James R. Ferguson
Acclaimed Contributor

Re: shell programming

Hi Olga:

The concept here is that no file is used. Rather the output of 'bdf' is piped to 'grep' which is run "quietly" (without output). The return code of grep is examined and only if it is non-zero then is your command (or commands as the example shows) executed:

# bdf|grep -q /mnt || ( echo "It's not mounted!";date )

You can exchange the double pipe (||) for a double ampersand (&&) and invert the logic.

Regards!

...JRF...
Olga_1
Regular Advisor

Re: shell programming

James,

Thank you for the explanations.
I wanted to assighn you 10 points, but somehow it turned to 0. Can I modify it?
John Palmer
Honored Contributor

Re: shell programming

Checking /etc/mnttab would be quicker than running bdf.

I would also recommend changing your grep arguments to only match for a filesystem called /mnt. All the above examples would also match something like /mntxxx or even /dev/vgxx/mnt...

Something like:

if bdf | grep -q " /mnt$"
then...

or:

if grep -q " /mnt " /etc/mnttab
then...

Would do the trick.

Regards,
John
S.K. Chan
Honored Contributor

Re: shell programming

In addition to others .. a couple of ways ..

var=$(bdf|grep /mnt)
if [[ $var != "" ]]
then
# do your stuff here
fi

OR

if [[ $(bdf|grep /mnt|wc -l) -ne 0 ]]
then
# do your stuff here
fi

Take note that "bdf" command may hung if you got stale NFS filesystem or bad FS. I'm in favor of "mount -v" instead.
James R. Ferguson
Acclaimed Contributor

Re: shell programming

Hi Olga:

Well, once you assign points there's no changing them! ;-)

John Palmer makes a very good point. You could leverage his suggestion like this too:

# grep -q "/mnt " /etc/mnttab || ( echo "It's not mounted!";date )

Notice that I let 'grep' open the '/etc/mnttab' rather than spawn a separate process to do so, like 'cat /etc/mnttab|grep...'.

...JRF...
Dietmar Konermann
Honored Contributor

Re: shell programming

Folks, I like this thread... what about

mount | grep -q '^/opt ' && echo "Yo, brother."

Should be more water-proof, I think. In general, I don't like reading /etc/mnttab directly.

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
James R. Ferguson
Acclaimed Contributor

Re: shell programming

Hi Dietmar:

Das ist sehr gut!

Yes, I like that better. It's short, matches exactly what (and only what it should) and executes very quickly.

BTW, your new hat looks just like it was made for you! :-))

With warm regards...

...JRF...
Dietmar Konermann
Honored Contributor

Re: shell programming

Hi, James!

Vielen Dank fuer die Blumen! :)

Greetings to Sarasota.

Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)