Sponsored Content
Top Forums Shell Programming and Scripting Need help on " if condition" in shell Post 302602711 by ygemici on Tuesday 28th of February 2012 06:05:29 AM
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:
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
All times are GMT -4. The time now is 04:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy