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 ?
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
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.
These 2 Users Gave Thanks to michaelrozar17 For This Post:
check these
from the bash manual it says for [[ expression ]]
and you can try like this [but wild char(asteriks) doesnot cause the expansion]
i remove the parentheses because it does not like seem as special chars.
and this may be like this
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.
so it is ok because asteriks char matches S* -> [ SK_BTS04A_hiQ_01b ]
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.
with double brackets bash manages it with quotes (you can think word splitting automatically created with the double quotes in the [[..]] to IFS)
regards
ygemici
These 2 Users Gave Thanks to ygemici For This Post:
@ 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
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:
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:
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 .
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:
(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:
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)
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)
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)
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)
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)
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)