LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-16-2013, 05:17 PM   #1
martin083
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Rep: Reputation: 0
Conditional statement not working bash script


I am trying to check if my mount points are up and ready with the following code but I get syntax error

if [[ grep -qs $copyFrom /proc/mounts && grep -qs $copyTo /proc/mounts ]];
then
pGreen "Remote file servers are up and running."
else
pRed "Remote file servers are down."
fi
 
Old 01-16-2013, 05:48 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
When evaluating a command, the square brackets must not be used:
Code:
if grep -qs $copyFrom /proc/mounts && grep -qs $copyTo /proc/mounts
then
  do something
fi
 
1 members found this post helpful.
Old 01-18-2013, 07:58 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


It's also always a good idea to get into the habit of quoting your variables, to avoid word-splitting on any whitespace, if such exists.

Code:
if grep -qs "$copyFrom" /proc/mounts && grep -qs "$copyTo" /proc/mounts; then
Here's another idea for you, BTW:

Code:
case $( grep -c -e "$copyFrom" -e "$copyTo" /proc/mounts ) in

    2) pGreen "Remote file servers are up and running." ;;
    *) pRed "Remote file servers are down." ;;

esac
 
1 members found this post helpful.
  


Reply

Tags
bash, conditional, multiple


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
BASH: IF statement not working? EdinburghLad Programming 6 05-15-2012 09:26 PM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
BASH script alphabetical conditional operator query. lupusarcanus Linux - Newbie 5 03-15-2010 04:14 AM
Conditional (IF) test in bash sh script - presence of first parameter Critcho Linux - Newbie 6 10-01-2008 01:20 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM


All times are GMT -5. The time now is 12:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration