LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-05-2012, 03:25 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Rep: Reputation: 33
[bash-script] A question about using grep in the script


Hi,

My script looks as follow:
Code:
#!/bin/sh

GREP=$(grep "//s001fs02/temp"  "/proc/mounts")
if [ ! -z $GREP ]; then
  echo "it is mounted"
else
  echo "it is not mounted"
fi
I use this to check if a directory is mounted or not. In the fact, if the directory not mounted, I got:
Code:
it is not mounted
This is correct. But if the directory is mounted, I got:
Code:
./test.sh: line 4: [: too many arguments
it is not mounted
Why?
 
Old 03-05-2012, 03:31 AM   #2
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Original Poster
Rep: Reputation: 33
I solved my problem. It should be:

...
if [[ ! -z $GREP ]]; then
...
 
Old 03-05-2012, 04:27 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Another way is:
Code:
if grep -q "//s001fs02/temp" /proc/mounts
then
  echo "it is mounted"
else
  echo "it is not mounted"
fi
An aside note: in a test the negation of -z is equivalent to -n (the length of string is nonzero).
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help in bash script and grep manya Programming 24 05-07-2010 08:06 AM
bash script grep without double saavik Programming 2 05-14-2009 11:15 AM
Bash: Script with tail and grep Primsi Linux - General 2 11-16-2006 06:30 AM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 04:14 PM
bash script and grep syros Programming 4 01-13-2005 04:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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