If statement with [[ ]] and regex not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If statement with [[ ]] and regex not working as expected
# 1  
Old 03-19-2013
If statement with [[ ]] and regex not working as expected

Using BASH:
Code:
$ if [[ "2013-01-18 10:58:00" == "20[0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:00" ]]; then echo "true"; else echo "false"; fi

Code:
false

Mike
# 2  
Old 03-19-2013
Drop the quotes on the right-hand side.
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 03-19-2013
try:
Code:
if expr "2013-01-18 10:58:00" : "20[0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:00" >/dev/null; then echo "true"; else echo "false"; fi

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 03-20-2013
That's not really a regex, that's still a glob. Meaning, you can do the same thing in a more portable case statement:

Code:
case "$VAR" in
20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]" "[0-2][0-9]:[0-5][0-9]:00)
        echo "match" ;;
*) echo "no match" ;;
esac

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-20-2013
Quote:
Originally Posted by elixir_sinari
Drop the quotes on the right-hand side.
Code:
$ if [[ "2013-01-18 10:58:00" == 20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]" "[0-2][0-9]:[0-5][0-9]:00 ]]; then echo "true"; else echo "false"; fi

Code:
true

---------- Post updated at 09:28 AM ---------- Previous update was at 09:27 AM ----------

Quote:
Originally Posted by rdrtx1
try:
Code:
if expr "2013-01-18 10:58:00" : "20[0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:00" >/dev/null; then echo "true"; else echo "false"; fi

Code:
true

---------- Post updated at 09:30 AM ---------- Previous update was at 09:28 AM ----------

Quote:
Originally Posted by Corona688
That's not really a regex, that's still a glob. Meaning, you can do the same thing in a more portable case statement:

Code:
case "$VAR" in
20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]" "[0-2][0-9]:[0-5][0-9]:00)
        echo "match" ;;
*) echo "no match" ;;
esac

Do you mean that normal shell expansion (a subset of RegEx rules) works?

Thanks to all three of you.

Mike
# 6  
Old 03-20-2013
Quote:
Originally Posted by Michael Stora
Do you mean that normal shell expansion (a subset of RegEx rules) works?
Shell expansion works in case statements, yes. But it's not a regex. It's not even a subset of regex... It looks similar but acts very different.

* isn't a wildcard in regex, it's a modifier. "*" isn't a valid regex, but ".*" would be (where . is special character meaning 'any character').

BASH does have an operator for regex, but you're not using it.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

If statement fails with integer expression expected

Below is what i have in my script. htcount=$(curl -s --user tomcatstatus:tomcatstatus http://`hostname`.mypc.com:887/manager/jmxproxy?qry=Catalina:type=ThreadPool,name=\"http-nio-887\" |grep sBusy | cut -d ' ' -f2) echo $htcount if ; then echo "more than 10" else echo "Less than 10" fi... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. UNIX for Advanced & Expert Users

Test -e not working as expected (by me)

I ran into the following and still do not understand entirely the rationale behind this. If someone could explain why things are as they are I'd be thankful. The following was tested on AIX 7.1 with ksh88, but i suspect that to be ubiquitous. In an installation routine i had to create a set of... (6 Replies)
Discussion started by: bakunin
6 Replies

4. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

5. Shell Programming and Scripting

Case statement not working as expected

case "$freq" in " Hz") low=250; high=550;; "8 Hz") low=250; high=1000;; " Hz") low=400; high=1000;; "63 Hz") low=550; high=1000;; " Hz") low=400; high=550;; ... (2 Replies)
Discussion started by: Michael Stora
2 Replies

6. Shell Programming and Scripting

echo is not working as expected

for i in `cat /export/home/afahmed/Arrvial_time.txt` do echo $i echo $i | awk '$3 < $D { print $4 }' >> dynamic_DF.txt; done When i echo, its echo as Nov 15 02:24 /export/home/pp_adm/inbound//wwallet_20111115.txt where i expect it to be Nov 15 02:24... (7 Replies)
Discussion started by: afahmed
7 Replies

7. Shell Programming and Scripting

awk is Printing folders with only numbers as expected. But can't explain 'total' statement.

I am trying to get folder names that contain only numbers. Can someone explain why following command is printing 'total 450' as part of output.. $> ls -lt | awk '$9 ~ /^*$/' | more total 450 drwxr-x--x 3 user1 group1 512 Mar 9 2008 329227163 drwxr-x--x 3 user1 group1 ... (17 Replies)
Discussion started by: kchinnam
17 Replies

8. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

9. UNIX for Dummies Questions & Answers

Redirection not working as expected

Portion of my script below : if ; then NUMBEROFFEILDS=`cat ${BASE_SCRIPT_LOC}/standardfilecleanup.lst|grep -w ${db_file_path}|awk -F: '{print NF}'` COUNT=4 while ; do awk_var="$"`echo $COUNT` file_name1=`cat ${BASE_SCRIPT_LOC}/standardfilecleanup.lst|grep -w... (1 Reply)
Discussion started by: findprakash
1 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question