LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   BASH scripting Condition Statements (http://www.linuxquestions.org/questions/linux-general-1/bash-scripting-condition-statements-4175446937/)

Xyaib 01-23-2013 01:50 PM

BASH scripting Condition Statements
 
Hello, my code looks as follows:

Code:

if [ "$state" = 1 ] && [ "$i" = *1* ] && [ "$i" != *Batch* ] && [ "$i" != *Int* ]; then

  echo "--------------------------------"

However it yells at me because I have more then 2 conditions.

Is there a way to force it to accept 4-6 conditions?

Thanks

dru8274 01-23-2013 02:18 PM

Quote:

Originally Posted by Xyaib (Post 4876317)
Code:

if [ "$state" = 1 ] && [ "$i" = *1* ] && [ "$i" != *Batch* ] && [ "$i" != *Int* ]; then
  echo "--------------------------------"
fi


I see two problems here. First, you have used "=" (used for variable assignment) when you need to use "==" (used for equality in a test statement). Second, bash has a preferred double-bracket syntax for test-conditions - it allows you to move the "&&" inside, as described here.
Code:

if [[ "$state" == 1  && "$i" == *1* && "$i" != *Batch* && "$i" != *Int* ]] ; then
  echo "--------------------------------"
fi

Happy with ur solution... then tick "yes" and mark as Solved!


All times are GMT -5. The time now is 09:43 PM.