Sponsored Content
Top Forums Shell Programming and Scripting Bash: if condition as variable Post 302397035 by curlee2002 on Saturday 20th of February 2010 01:24:28 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Bash script if condition not executing

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

9. 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

10. 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
asn1_parser2array(3)						     libtasn1						      asn1_parser2array(3)

NAME
asn1_parser2array - function that generates a C structure from an ASN1 file SYNOPSIS
#include <libtasn1.h> int asn1_parser2array(const char * inputFileName, const char * outputFileName, const char * vectorName, char * errorDescription); ARGUMENTS
const char * inputFileName specify the path and the name of file that contains ASN.1 declarations. const char * outputFileName specify the path and the name of file that will contain the C vector definition. const char * vectorName specify the name of the C vector. char * errorDescription return the error description or an empty string if success. DESCRIPTION
Creates a file containing a C vector to use to manage the definitions included in *INPUTFILENAME file. If *INPUTFILENAME is "/aa/bb/xx.yy" and OUTPUTFILENAME is NULL, the file created is "/aa/bb/xx_asn1_tab.c". If VECTORNAME is NULL the vector name will be "xx_asn1_tab". RETURNS
ASN1_SUCCESS The file has a correct syntax and every identifier is known. ASN1_FILE_NOT_FOUND An error occured while opening FILE_NAME. ASN1_SYNTAX_ERROR The syntax is not correct. ASN1_IDENTIFIER_NOT_FOUND In the file there is an identifier that is not defined. ASN1_NAME_TOO_LONG In the file there is an identifier whith more than ASN1_MAX_NAME_SIZE characters. COPYRIGHT
Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. SEE ALSO
The full documentation for libtasn1 is maintained as a Texinfo manual. If the info and libtasn1 programs are properly installed at your site, the command info libtasn1 should give you access to the complete manual. libtasn1 2.2 asn1_parser2array(3)
All times are GMT -4. The time now is 08:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy