LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 07-03-2008, 10:43 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Help checking for mounted share with a bash script


I mount a SMB share at boot-time.

I now have created a script to test to see if the share is mounted.
Code:
#!/bin/bash

SUCCESS=`mount|grep engineer|cut -d '/' -f3`
FAIL=`mount|grep engineers|cut -d '/' -f3`
echo $SUCCESS
echo #FAIL

if [ -n $SUCCESS ]
        then
        echo "Engineer Mounted."
else
        echo "Engineer NOT Mounted."
fi

if [ -n $FAIL ]
        then
        echo "Engineers Mounted."
else
        echo "Engineers NOT Mounted."
fi
This is just a test script.
If I run mount I get:
Code:
root@gcnpd:~/scripts# mount 
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
/sys on /sys type sysfs (rw,noexec,nosuid,nodev)
varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)
varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
udev on /dev type tmpfs (rw,mode=0755)
devshm on /dev/shm type tmpfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
lrm on /lib/modules/2.6.22-14-generic/volatile type tmpfs (rw)
securityfs on /sys/kernel/security type securityfs (rw)
//gcctfs1/engineer on /media/engineer type smbfs (rw)
The last line is the one of interest.
Here are the results of a run of my script:
Code:
root@gcnpd:~/scripts# ./temp.sh 
gcctfs1

Engineer Mounted.
Engineers Mounted.
Why does the $FAIL case "succeed"?

Any thoughts?

Chris
 
Old 07-03-2008, 11:53 AM   #2
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,320

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
A good tool for debugging a script is to insert 'set -x' and 'set +x' into your script to turn verbose output on and off (note that it's '-x' for on and '+x' for off), or alternately add -x to the shebang (#!/bin/bash -x) to set it globally. This will output each action as it's completed so you can see what's happening.

When I did this for your script (substituting a text file with your input for 'mount') it showed me this:
Code:
david:~/temp/test$ ./mounttest.sh
++ cat ./testfile.txt
++ grep engineer
++ cut -d / -f3
+ SUCCESS=gcctfs1
++ cat ./testfile.txt
++ grep engineers
++ cut -d / -f3
+ FAIL=
+ echo gcctfs1
gcctfs1
+ echo

+ '[' -n gcctfs1 ']'
+ echo 'Engineer Mounted.'
Engineer Mounted.
+ '[' -n ']'
+ echo 'Engineers Mounted.'
Engineers Mounted.
So for some reason the test function is seeing the '-n $VARIABLE' as a single literal string. You can fix this by putting quotes around the variable names so that they will be seen as unified strings for testing ([ -n "$FAIL" ]). But really you shouldn't even need the '-n' option at all in this case, because if you remove it test will simply check for whether the variable string exists or not. And that should work even if you don't use quotes.
 
Old 07-03-2008, 12:12 PM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 51
Ignore post

Last edited by nx5000; 07-03-2008 at 12:17 PM.
 
Old 07-03-2008, 12:14 PM   #4
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
I am embarrassed that it was that simple.
I forget why I even added the -n in the if statement.

Thank you
 
Old 07-03-2008, 12:21 PM   #5
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,320

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Hey, no problem. Sometimes you just need a separate set of eyes to see a problem that you keep overlooking.
 
Old 07-03-2008, 01:11 PM   #6
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 196

Rep: Reputation: 31
A much easier way to accomplish the same thing is to use the exit status of the grep command. So, something like this would shorten your script:

Code:
if [[ `mount | grep something` ]]; then
      echo "Mounted"
else
      /usr/bin/mount blah
fi
You could use the ${comamnd} notation too (which is preferred).

Last edited by 0.o; 07-03-2008 at 01:16 PM.
 
Old 07-03-2008, 01:18 PM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 51
True, that's what I would do. Unless you really need the cutted part.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script for checking if a drive is mounted Soulja1 Linux - General 2 05-07-2007 03:07 PM
Checking and changing DOS attributes on a samba mounted share nkendrick Linux - Software 1 08-24-2006 07:47 PM
Executing a script stored in an mounted NFS Share Riddick Linux - Software 6 01-13-2006 06:14 PM
samba: linux mounted share = choppy playback, windows mounted share = smooth kleptophobiac Linux - Software 2 04-10-2005 08:23 AM
Checking if a device is mounted using a script thar Linux - Software 2 02-22-2004 03:17 PM


All times are GMT -5. The time now is 11:19 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration