Register
It is currently Fri Oct 24, 2014 8:42 am

[SOLVED] if condition as variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sat Feb 20, 2010 9:35 am   

Joined: Sat Feb 20, 2010 8:01 am
Posts: 4
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 curtlee2002 on Sat Feb 20, 2010 3:12 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sat Feb 20, 2010 11:49 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
don't use quotes; otherwise the whole expression is considered as a one argument where there are more.

and you'd better use
Code:
[ a = b -o a = a -o c = a ]
as it calls only one test.


Top
 Profile  
 PostPosted: Sat Feb 20, 2010 3:12 pm   

Joined: Sat Feb 20, 2010 8:01 am
Posts: 4
Thank You, Watael.

That did the trick. I posted the full test script below plus sample output.

My 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
                  predicate=`echo -e "$predicate -o \"${fileType[i]}\" = \"$fileNameExt\""`
                  i=$[$i+1]
               done
               predicate="[ $predicate ]"
            fi
            echo "$predicate"
            if $predicate
            then
               echo "yes, $inputFileName has a usable file extension."
            fi
      fi
   done
fi

exit


Sample Output
Code:
$ ./test.sh

inputFileName = bill.mkv
[ "avi" = "mkv" -o "flv" = "mkv" -o "iso" = "mkv" -o "mkv" = "mkv" -o "mp4" = "mkv" -o "mpeg" = "mkv" -o "mpg" = "mkv" -o "wmv" = "mkv" ]
yes, bill.mkv has a usable file extension.

inputFileName = bob.mkv
[ "avi" = "mkv" -o "flv" = "mkv" -o "iso" = "mkv" -o "mkv" = "mkv" -o "mp4" = "mkv" -o "mpeg" = "mkv" -o "mpg" = "mkv" -o "wmv" = "mkv" ]
yes, bob.mkv has a usable file extension.

inputFileName = examples.desktop
[ "avi" = "desktop" -o "flv" = "desktop" -o "iso" = "desktop" -o "mkv" = "desktop" -o "mp4" = "desktop" -o "mpeg" = "desktop" -o "mpg" = "desktop" -o "wmv" = "desktop" ]

inputFileName = test.sh
[ "avi" = "sh" -o "flv" = "sh" -o "iso" = "sh" -o "mkv" = "sh" -o "mp4" = "sh" -o "mpeg" = "sh" -o "mpg" = "sh" -o "wmv" = "sh" ]



Top
 Profile  
 PostPosted: Sat Feb 20, 2010 4:35 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
here are some corrections
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/.*\.//'`
            fileNameExt="${inputFileName##*.}"
            predicate="\"$fileType\" = \"$fileNameExt\""

#            if [ ${#fileType[@]} != 0 ]  ## as you give the array this doesn't seem useful
#            then
#               i="1"
#               while [ $i -lt ${#fileType[@]} ]
               for (( i=1; i<${#fileType[@]}; i++ ))
               do
#                  predicate="$(echo -e "$predicate -o \"${fileType[i]}\" = \"$fileNameExt\"")"
                  predicate="$predicate -o \"${fileType[i]}\" = \"$fileNameExt\""
#                  i=$[$i+1]
               done
               #predicate="[ $predicate ]"
#            fi
            echo "$predicate"
            if [ $predicate ]
            then
               echo "yes, $inputFileName has a usable file extension."
            fi
      fi
   done
fi

exit
and how I shortenned it
Code:
#!/bin/bash

[ -n "$1" ] && {
   inputFileName="$1"
   echo -e "\$1 = $1\ninputFileName = $inputFileName"
} || {
   fileType=( avi flv iso mkv mp4 mpeg mpg wmv )
   for inputFileName in *
   do [ -f "$inputFileName" ] && {
      fileNameExt="${inputFileName##*.}"
      predicate="\"$fileType\" = \"$fileNameExt\""
      for (( i=1; i<${#fileType[@]}; i++ ))
      do predicate="$predicate -o \"$fileNameExt\" = \"${fileType[i]}\""
      done
      echo -e "\ninputFileName = $inputFileName\n[ $predicate ]"
      [ $predicate ] && echo "yes, $inputFileName has a usable file extension."
      }
   done
}
exit


Top
 Profile  
 PostPosted: Sat Feb 20, 2010 7:02 pm   

Joined: Sat Feb 20, 2010 8:01 am
Posts: 4
Thank again, Watael, you have been very helpful.

I haven't ever seen something like the code below before. It can be used to replace an if statement as long as you don't need else, right?

Code:

[$predicate ] && echo "yes, $inputFileName has a usable file extension."


Would you happen to know where I could read more about ${inputFileName##*.} ?
I was using sed because all the other ways I found online failed to work as soon as there were spaces or extra periods in the filename, but that works great.


Top
 Profile  
 PostPosted: Sat Feb 20, 2010 7:09 pm   

Joined: Sat Feb 20, 2010 8:01 am
Posts: 4
Now I see that you used || { } to do an else.


Top
 Profile  
 PostPosted: Sat Feb 20, 2010 8:33 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Quote:
you used || { } to do an else
right

${parameter##word} is one among other useful bash's Parameter Expansion


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP