LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2007, 12:13 AM   #1
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Rep: Reputation: 15
How to check if NAS is mounted via shell script?


Hi,
I'm trying to automate a backup process via shell scripts.
I have a NAS box that I use across two machines so I would like to first check if my NAS is mounted via shell.

What's the best way to do this?

Thanks for your time.
 
Old 12-14-2007, 12:22 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, /etc/mtab tells you what is actually mounted, as opposed to /etc/fstab, which tells you what is mounted a boot time.
 
Old 12-14-2007, 12:34 AM   #3
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
Well, /etc/mtab tells you what is actually mounted, as opposed to /etc/fstab, which tells you what is mounted a boot time.

That's fine but I'm not sure what I should grep for.

Code:
# tail -n 1 /etc/mtab 
//nas01.my.box.com/USER-1 /mnt/nas smbfs 0 0 0

would this work:
Code:
cat /etc/mtab | grep -i \/mnt\/nas
or is that insufficient to ensure NAS is mounted?

And how would I use it in an IF condition? I know how to use IF's in terms of structure in a shell script but what would the logical condition itself be?


Thanks for your time.
 
Old 12-14-2007, 12:38 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Don't use cat, use
Code:
grep -i '/mnt/nas' >/dev/null
if [[ $? -eq 0 ]]
then
    echo "nas mounted"       # your code goes here
fi

bookmark this: http://www.tldp.org/LDP/abs/html/ & http://rute.2038bug.com/index.html.gz
 
Old 12-14-2007, 12:42 AM   #5
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
Don't use cat, use
Code:
grep -i '/mnt/nas' >/dev/null
if [[ $? -eq 0 ]]
then
    echo "nas mounted"       # your code goes here
fi

bookmark this: http://www.tldp.org/LDP/abs/html/ & http://rute.2038bug.com/index.html.gz

But that would only see if the folder /mnt/nas has something in it, right?

If another user happens to copy something to that folder without checking if it was mounted (while NAS is not mounted, hypothetical case), then that condition would result in a false positive, would it not?


Thanks.
 
Old 12-14-2007, 01:09 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As I said before:
"Well, /etc/mtab tells you what is actually mounted, as opposed to /etc/fstab, which tells you what is mounted a boot time."
Try cat'ing that file then dismount something and cat it again.....
 
Old 12-14-2007, 01:32 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
Code:
grep -i '/mnt/nas' >/dev/null
.....
missing input filename to grep?
 
Old 12-14-2007, 02:09 AM   #8
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
As I said before:
"Well, /etc/mtab tells you what is actually mounted, as opposed to /etc/fstab, which tells you what is mounted a boot time."
Try cat'ing that file then dismount something and cat it again.....
as i mentioned in my second post, i'm not sure how the IF condition should be written. I need to know what to check for on the IF on the return of the cat of mtab.
 
Old 12-14-2007, 02:46 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You don't need an IF statement for this.
Code:
echo -n "NAS mounted: "; grep -qi '/mnt/nas' /etc/mtab && echo yes || echo no
 
Old 12-18-2007, 02:17 AM   #10
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
You don't need an IF statement for this.
Code:
echo -n "NAS mounted: "; grep -qi '/mnt/nas' /etc/mtab && echo yes || echo no


actually i do need an IF condition because I want to do something like this:
Code:
if [ $(grep -qi '/mnt/nas' /etc/mtab) ]; then
    echo 'mounted';
    #do other things here
else
    echo 'NOT mounted';
    #do other things here
fi
but that does not work as expected...it always goes to false.
 
Old 12-18-2007, 05:39 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
That's because grep returns zero (false) if it matches, 1 if no matches, 2 (?) if an error, so reverse your logic.
It'd be clearer if you did the grep on it's own line, then just test the status ($?) in the if [[ ]] eg
Code:
grep -flags string file
if [[ $? -eq 0 ]]
then
    grep matched, do s'thing
else
    no match, do other thing
fi
 
Old 12-27-2007, 01:46 AM   #12
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
That's because grep returns zero (false) if it matches, 1 if no matches, 2 (?) if an error, so reverse your logic.
It'd be clearer if you did the grep on it's own line, then just test the status ($?) in the if [[ ]] eg
Code:
grep -flags string file
if [[ $? -eq 0 ]]
then
    grep matched, do s'thing
else
    no match, do other thing
fi


Thanks.

I had the opportunity to try this out today again (I was assigned to do something else the last few days).

I ran it on another machine, testing if the SATA drive was mounted. When I tested for a mounted SATA drive, it worked fine but when I tested for a non existent SATA drive, the else portion of the condition did not work. I'm assuming it did not work because I didn't see the echo'd message.

Code:
grep -qi \/dev\/sda4 /etc/mtab
if [[ $? -eq 0 ]]
then
    echo 'NAS is mounted. Continuing...';
else
    echo 'NAS is not mounted. Can not continue. Exiting...';
fi
 
Old 12-31-2007, 01:25 AM   #13
koobi
Member
 
Registered: Jun 2006
Location: Colombo, Sri Lanka
Distribution: Ubuntu
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by koobi View Post
Thanks.

I had the opportunity to try this out today again (I was assigned to do something else the last few days).

I ran it on another machine, testing if the SATA drive was mounted. When I tested for a mounted SATA drive, it worked fine but when I tested for a non existent SATA drive, the else portion of the condition did not work. I'm assuming it did not work because I didn't see the echo'd message.

Code:
grep -qi \/dev\/sda4 /etc/mtab
if [[ $? -eq 0 ]]
then
    echo 'NAS is mounted. Continuing...';
else
    echo 'NAS is not mounted. Can not continue. Exiting...';
fi


Does anyone know why my else doesn't work?
 
Old 01-12-2008, 10:07 AM   #14
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
This is very puzzling, I tested your code by pasting it into a shell & it worked fine.

I tried it on a non-existent SATA (/dev/sda4), & on existent & non-existent PATA (/dev/hda1, /dev/hda4) partitions w/ no problems.


Post the results of:
Code:
grep  /dev/sdan /etc/mtab
grep  /dev/sdam /etc/mtab
where n exists & m doesn't.

Then the results of:
Code:
grep  /mnt/nas /etc/mtab
once w/ the NAS mounted & once w/ it unmounted.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test is a drive is mounted in a shell script? agtlewis Linux - General 7 03-18-2009 09:58 AM
script to check if a drive is mounted gazman1 Programming 3 07-10-2006 09:09 AM
Check for mounted device from shell seagoj Linux - Software 6 02-24-2006 10:20 AM
shell script to check ftp communication yuva_mca Linux - General 2 12-01-2005 07:15 AM
script check to see if device is mounted Warmduvet Programming 14 09-03-2004 06:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration