Bash: if condition as variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: if condition as variable
# 1  
Old 02-20-2010
[SOLVED] Bash: if condition as variable

How can I use a variable that has the conditions for the if statement stored in it?

my test script
Code:
condition="[ a = b ] || [ a = a ] || [ c = a ]"

if "$condition"
   then echo "true"
   else echo "false"
fi

output
Code:
$ ./test2.sh
./test2.sh: line 3: [ a = b ] || [ a = a ] || [ c = a ]: command not found
false


Last edited by curlee2002; 02-20-2010 at 02:27 PM..
# 2  
Old 02-20-2010
Use the eval command and build the condition string as:
Code:
condition="[ $a = $b -o  $a = $a -o  $c = $a ]"

if eval "$condition"
then 
  echo "true"
else 
  echo "false"
fi

# 3  
Old 02-20-2010
[SOLVED] Bash: if condition as variable

Thank You, Franklin52.

Adding eval after the if did the trick. I added it on line 28 of the test script below.

my final test script
Code:
#!/bin/bash
#test.sh

fileType=( avi flv iso mkv mp4 mpeg mpg wmv )

if [ -n "$1" ]
then
   inputFileName="$1"
   echo "\$1 = $1"
   echo "inputFileName = $inputFileName"
else
   for inputFileName in *
   do
      if [ -f "$inputFileName" ]
         then
            echo -e "\ninputFileName = $inputFileName"
            fileNameExt=`echo $inputFileName|sed 's/.*\.//'`
            predicate="[ \"$fileType\" = \"$fileNameExt\" ]"

            if [ ${#fileType[@]} != 0 ]
            then
               i="1"
               while [ $i -lt ${#fileType[@]} ]
               do
                  predicateN=""
                  predicateN=`echo -e "$predicateN|| [ \"${fileType[i]}\" = \"$fileNameExt\" ]"`
                  predicate="$predicate $predicateN"
                  i=$[$i+1]
               done
            fi

            if eval "$predicate"
            then
               echo "yes, $inputFileName has a usable file extension."
            fi
      fi
   done
fi

exit

sample output
Code:
$ ./test.sh 

inputFileName = bill.mkv
yes, bill.mkv has a usable file extension.

inputFileName = bob.mkv
yes, bob.mkv has a usable file extension.

inputFileName = examples.desktop


Last edited by curlee2002; 02-20-2010 at 02:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Condition in bash script

I want get from user and pass these parameters to bash script. script should copy files in user home directory. FYI: each file might be exist or not, might be one of them exist or four of them. Here is my script, it always copy file1 and seems only one of them execute! #!/bin/bash for... (6 Replies)
Discussion started by: indeed_1
6 Replies

2. Shell Programming and Scripting

Creating a condition on a bash script

I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters. (3 Replies)
Discussion started by: germany1517
3 Replies

3. Shell Programming and Scripting

Bash script if condition not executing

issue is with .txt files (7 Replies)
Discussion started by: anil529
7 Replies

4. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. UNIX for Dummies Questions & Answers

Variable in IF condition

In AIX, why is it variable VAR becomes true in the condition despite VAR was unassigned and not equal to 1? In Linux, it was traced as an error as VAR is not declared as variable and expecting an integer as argument. one.sh VAR=1 if ; then echo "One" fi if ; then echo "Two"... (5 Replies)
Discussion started by: budz26
5 Replies

6. Shell Programming and Scripting

Script with variable and condition

Hello newbies question... I just need a script able to launch a command when a condition is matched : #!/bin/ksh SIZ = 'cat /nurp/control.lst|wc -l' if test "$SIZ" -gt 0 then echo 1 else echo 2 fi but I receive errors messages ./t2: SIZ: not found 2 whats wrong ? (5 Replies)
Discussion started by: vdurieu
5 Replies

7. Shell Programming and Scripting

Compare the two variable with if condition

Please help me with this: I need to compare two values in if condition in shell script but its goes always to else condition: TIME_CHECK=PM TIME-CLOCK=PM if ; then echo "You have access!" else echo "ACCESS DENIED!" fi (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

8. Red Hat

how to use if condition with sed command in BASH

Urgent help with bash scripting 1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit. 2- also i want to add # in front of the... (2 Replies)
Discussion started by: lamoul
2 Replies

9. Programming

if condition in bash

Hi, I have meaning to include an if condition statement in my code to check the directory for existing output files and if its existing i want the program to delete it before doing the succeeding command. i just dont know the correct syntax for it. thanks much guys, this forum has indeed been very... (4 Replies)
Discussion started by: ida1215
4 Replies

10. Shell Programming and Scripting

Multiple condition checking in bash

Hi All, I am trying to check if two variables have value assigned to it. i am doing it like if ] then echo "Please specify either single hostname or host file for the report" usage exit fi But its not working for it.Even i specify values for both variables it dont go... (6 Replies)
Discussion started by: kailash19
6 Replies
Login or Register to Ask a Question