string test in IF statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string test in IF statement
# 8  
Old 03-17-2010
File Name Generation

I am using the ksh File Name Generation syntax out of the man pages. This syntax works:



Code:
if [[ $TBS = ?(USERS|TOOLS|SYSAUX) ]] THEN
do something....
fi

If I try to dynamically generate the TBS names the syntax breaks. Like below:

Code:
Orig_File_Num=`$SQLPLUS<<EOF
Connect / as sysdba
Select file_id from dba_data_files;
EOF`

File_Num=`print $Orig_File_Num | sed ‘s/ /|/g'`

If I print $File_Num I will get:

1|2|3|4|5|6

So I know this part is working because I print it to the screen.

But this does not work if I use the variable for the TBS list:

Code:
If [[ $TBS = ?(${File_Num}) ]]
Then
.....
Fi


Any idea how to make this work?

thanks.
# 9  
Old 03-17-2010
Quote:
Originally Posted by djehresmann
I am using the ksh File Name Generation syntax out of the man pages. This syntax works:



Code:
if [[ $TBS = ?(USERS|TOOLS|SYSAUX) ]] THEN
do something....
fi

If I try to dynamically generate the TBS names the syntax breaks. Like below:

Code:
Orig_File_Num=`$SQLPLUS<<EOF
Connect / as sysdba
Select file_id from dba_data_files;
EOF`

File_Num=`print $Orig_File_Num | sed ‘s/ /|/g’`

If I print $File_Num I will get:

1|2|3|4|5|6

So I know this part is working because I print it to the screen.

But this does not work if I use the variable for the TBS list:

Code:
If [[ $TBS = ?(${File_Num}) ]]
Then
…..
Fi

Any idea how to make this work?

thanks.
Code:
eval "
if [[ $TBS = ?(${File_Num}) ]]
then
…..
fi
"

OR use 'case':
Code:
eval "
case ${TBS} in
   ${File_Num} ) echo match
                   ;;
   *             ) echo no match
                   ;;
esac
"

# 10  
Old 03-17-2010
Thanks that works great. I got:

Code:
while ! TRUE
do
  print "enter a datafile number: "
  read DBFILE
 
  if [[ -n $DBFILE ]]
  then
  eval "
    if [[ $DBFILE = ?($NEW_FILEID) ]]
      then
      DBFILE_VALID=true
     else
       print "invalid number"
     fi
     "
  else
   DBFILE_VALID=false
  fi
done


The file numbers are generated dynamically thru sqlplus and placed into $NEW_FILEID. It all works great.

If I enter a character like !, ` , or # in response to the question "enter a datafile number?" it will stop the shell script and exit. How do I trap for those special characters?

thanks.
# 11  
Old 03-18-2010
Code:
# remove all allowed chars, rest is not allowed
extra=$(echo "$DBFILE" | tr -d "[a-z][A-Z][0-9]._" )
[ "$extra" != "" ] && echo "$extra not allowed" >&2  && exit 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement. ex. if ];then echo "not in range" else echo "number within range" fi (8 Replies)
Discussion started by: leemalloy
8 Replies

2. Shell Programming and Scripting

string test

How do I use bash to test if a line begins with a random number of spaces followed by a letter? (1 Reply)
Discussion started by: locoroco
1 Replies

3. Shell Programming and Scripting

Help with test statement

Hello, I am trying to build a test statement but I can't make it work I want to rearrange some fields, so if my "$cfg" variable contains a string ending with .log (*.log) I want to move it in another field. Any help will be much appreciated! Thank you Shell:sh if then log="${cfg}"... (9 Replies)
Discussion started by: drbiloukos
9 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Perl - automating if statement test

Hello all, I'm trying to automate an if statement in my Perl script. The script opens an input file for reading, checks each line in the file for a particular substring, and if it finds the substring, writes it to an output file. There are approximately 200 different input files. Each has... (3 Replies)
Discussion started by: Galt
3 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. UNIX for Dummies Questions & Answers

if test statement

Can you use an if statement after an else? example if then echo "word" else if then echo "word" (1 Reply)
Discussion started by: skooly5
1 Replies

8. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

9. UNIX for Dummies Questions & Answers

test a string...

Hi! I'm using echo $string | grep "" -c to test in a script if a string is a number and it seems to work. But how can i find, for example, if a string is a four figures number ? Thanks to all! (2 Replies)
Discussion started by: Kaminski
2 Replies

10. UNIX for Dummies Questions & Answers

string test?

hey guys- what is the syntax for a string test to verify that a user has entered a 6 digit numeric? Thanks for your help in advance! Todd (9 Replies)
Discussion started by: hedrict
9 Replies
Login or Register to Ask a Question