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 01-30-2005, 06:12 PM   #1
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 365

Rep: Reputation: 30
Testing for mount


How might I test for the presence of a mounted filesystem
using BASH, Tcl, expect or any other scripting language?

My problem is testing for the presence of a mount and using
if-then-else to mount it if it is not already mounted or umount
if it is.

I assume I would have to somehow read either mtab or /proc/mounts
to determine this, but I am unsure of the syntax to use. I would
assume I would use GREP to extract or read the information but am
unclear on what the comparison syntax would look like.

Any suggestions?

TIA
 
Old 01-30-2005, 06:23 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
An example in bash
Code:
test='grep /mnt/cdrom /etc/mtab'
if [ "$test" ]
then
  echo CDROM mounted
else
  echo CDROM not mounted
fi
 
Old 01-30-2005, 11:36 PM   #3
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 365

Original Poster
Rep: Reputation: 30
That's kind of what I expected, but I am having trouble with:

test='grep /mnt/cdrom /etc/mtab'

I keep getting the error 'unary operator expected' indicating that
the grep function value is not being assigned to the variable even
though the grep at the command line returns the correct result.
I have tried both single and double quotes and single/double
quotes within double/single quotes all to no avail. I even tried
enclosing 'backtics' within double quotes, but still no go.

I'm sort of at a loss now and need some expert advice.

TIA
 
Old 01-31-2005, 11:47 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by raypen
That's kind of what I expected, but I am having trouble with:

test='grep /mnt/cdrom /etc/mtab'
Yes. The single quotes should have been back-quotes (i.e. ` instead of ' ).
Code:
test=`grep /mnt/cdrom /etc/mtab`
or (bash specific):
Code:
test=$(grep /mnt/cdrom /etc/mtab)
Or do it a different way:
Code:
if grep -q /mnt/cdrom /etc/mtab ; then
    echo CDROM mounted
else
    echo CDROM not mounted
fi
 
Old 01-31-2005, 01:01 PM   #5
raypen
Member
 
Registered: Jun 2002
Location: Midwest
Distribution: Slackware
Posts: 365

Original Poster
Rep: Reputation: 30
Thanks!

test=$(grep /mnt/cdrom /etc/mtab) works the way I want.

Oddly enough test = `grep /mnt/cdrom /etc/mtab`would not
work until I removed the spaces: test=`grep /mnt/cdrom /etc/mtab`

Maybe, one of these days, I'll get it right the first time.
 
Old 01-31-2005, 01:24 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by raypen
Oddly enough test = `grep /mnt/cdrom /etc/mtab`would not
work until I removed the spaces: test=`grep /mnt/cdrom /etc/mtab`
Yes, don't use any whitespace around '=' in bash.
 
Old 02-01-2005, 08:09 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
er,
what's wrong with using the mount command?
 
Old 02-01-2005, 11:14 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by bigearsbilly
er,
what's wrong with using the mount command?
Nothing...
Code:
test=$(mount | grep /mnt/cdrom)
should have the same result.
 
  


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
testing os junctionking Linux - Distributions 2 04-10-2005 10:22 PM
Setup as getting debian testing files from ftp - will it stay with testing BrianHenderson Debian 2 09-02-2004 06:06 PM
testing .. marvin_robot General 1 03-07-2004 06:13 AM
testing pld Linux - Software 0 09-30-2003 04:57 PM
Testing The Grepper Programming 1 03-29-2002 09:06 PM

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

All times are GMT -5. The time now is 07:11 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