LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Conditional statement not working bash script (http://www.linuxquestions.org/questions/programming-9/conditional-statement-not-working-bash-script-4175445927/)

martin083 01-16-2013 04:17 PM

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

colucix 01-16-2013 04:48 PM

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


David the H. 01-18-2013 06:58 AM

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



All times are GMT -5. The time now is 06:52 AM.