Need help on " if condition" in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on " if condition" in shell
# 1  
Old 02-28-2012
Question Need help on " if condition" in shell

Is the below script/condition correct ?
There are two square brackets, is it really required
If everything seems ok, what would be the o/p ( 1 or 2 )
what exactly the condition is looking for ?

Code:
   answer = SK_BTS04A_hiQ_01b
   ......                   
   ......
   ......
   elif [[ $answer != [a-zA-Z]*([a-zA-Z0-9_-]) ]]
   then
         echo 1
   else 
         echo 2
   fi

Thanks in Advance

Last edited by methyl; 02-29-2012 at 06:48 AM..
# 2  
Old 02-28-2012
Giving double square brackets is not wrong, the if statement would do the regular test. For more information read link1 and link2.Coming to the regular expression $answer != [a-zA-Z]*([a-zA-Z0-9_-]) on splitting it
Code:
[a-zA-Z]*     --> Matches 0 or more alphabets both upper and lower case. According to the input given this matches first two letters SK in SK_BTS04A_hiQ_01b
(             --> literal open brace match
[a-zA-Z0-9_-] --> Match 0 or 1 alphabets both lower and upper case,numbers from 0 to 9, underscore and a hyphen
)             --> literal close brace match

Since it matches only first 2 letters the output would be 1. Additionally having double quotes around variables and literals while comparing is a good way to program.
Code:
#!/bin/ksh
answer=SK_BTS04A_hiQ_01b
if [[ "$answer" != "[a-zA-Z]*([a-zA-Z0-9_-])" ]]
then
        echo 1
else
        echo 2
fi

These 2 Users Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 02-28-2012
check these
Code:
if [[ $answer != [a-zA-Z]*[a-zA-Z0-9_-] ]]; then echo ok; fi
ok

from the bash manual it says for [[ expression ]]
Code:
Any part of the pattern may be quoted to force it to be matched as a string.

and you can try like this [but wild char(asteriks) doesnot cause the expansion]
Code:
# if [ "$answer" != "[a-zA-Z]*[a-zA-Z0-9_-]" ]; then echo ok; fi
ok

i remove the parentheses because it does not like seem as special chars.

and this may be like this
Code:
......
elif [[ $answer != [a-zA-Z]*[a-zA-Z0-9_-] ]]
then
echo "answer value is not equal to the pattern"
else
echo "answer is equal to the pattern "
fi

if used the asteriks(*) char in the your regex then you must be it in the quotes for escaping its meaning so shell is not to expand it.
Code:
# echo *  --> all files
# echo "*" --> asteriks char

Code:
*	The preceding item will be matched zero or more times.

so it is ok because asteriks char matches S* -> [ SK_BTS04A_hiQ_01b ]
Code:
# if [[ $answer == [a-zA-Z]*[a-zA-Z0-9_-] ]] ; then echo ok ;fi
ok

with single square brackets bash gives error beacuse of [a-z] and other expression's expansion brings a lot of char sets and does not recognized by shell by globbing '*' and single bracket have not capable of manage this.
Code:
# if [ $answer == [a-zA-Z]*[a-zA-Z0-9_-] ] ; then echo ok ;fi
-bash: [: too many arguments

with double brackets bash manages it with quotes (you can think word splitting automatically created with the double quotes in the [[..]] to IFS)
Code:
# if [[ $answer == [a-zA-Z]*[a-zA-Z0-9_-] ]] ; then echo ok ;fi
ok


regards
ygemici
These 2 Users Gave Thanks to ygemici For This Post:
# 4  
Old 02-28-2012
@ michael: the double quotes on the RHS of the expression should be removed at any case otherwise the characters will be interpreted as string. Also, the RHS will not be evaluated using regex but by using shell pattern matching, which is a different beast.

For extended regex, really recent bash and ksh93 provide the =~ operator.

---------- Post updated at 12:09 ---------- Previous update was at 12:06 ----------

Quote:
Originally Posted by ygemici
Code:
# if [[ $answer == [a-zA-Z]*[a-zA-Z0-9_-] ]] ; then echo ok ;fi
ok

I think it is good to point out that the OK is because the first character of $answer is a letter and the last character is in [a-zA-Z0-9_-] and that the intermediate characters can be anything. I am not sure if that is what the OP has in mind.

Last edited by Scrutinizer; 02-28-2012 at 08:42 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-28-2012
Thanks Scruti and ygemici. I got the same error -bash: [: too many arguments as ygemici but after placing the quotes around RHS the error went off. Hence i assumed that we would need to quote the RHS.
This User Gave Thanks to michaelrozar17 For This Post:
# 6  
Old 02-28-2012
Quote:
what exactly the condition is looking for ?
[[ $answer != [a-zA-Z]*([a-zA-Z0-9_-]) ]]
It is validating the string $answer. It checks whether the string starts with an alphabetic character and that every other character is alphanumeric or a hyphen or an underscore.

To be 100% sure we'd need to know what Shell you have.

The double square brackets are mandatory because this is a Conditional Expression. See the "Conditional Expressions" section of your Shell "man" pages. It is not a "test".


Beware that every post from post #2 onwards mentions a totally different condition from the one in post #1 .
This User Gave Thanks to methyl For This Post:
# 7  
Old 02-28-2012
Oops, you are right. The OP is using extended pattern matching (like extended globbing). Hadn't even noticed that..
For those testing with bash first execute:
Code:
shopt -s extglob

(in recent ksh93 this works automatically).

Last edited by Scrutinizer; 02-28-2012 at 10:59 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

5. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

8. Shell Programming and Scripting

shell "if" condition not executed

Hi, The below script should should create a file "COUNTFILE.log" if not found. But I am trying to run this and it is not creating the file . #!/bin/sh find . -name "COUNTFILE.log" > /dev/null 2>&1 if ; then echo 0> COUNTFILE.log fi Please advice why it is not creating... (8 Replies)
Discussion started by: himvat
8 Replies

9. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question