How come this if statement doesn't work?

greetings,

the following code isn't working as i expect it to. the first dbl brackets do but the second set gets ignored. ie: if i'm on t70c6n229 it echoes "Something" and i expect it not to. what am i missing?

if [[ `hostname` =~ "t70cra2[01-40]" ]] || [[ `hostname` =~ "t70c6n2[01-28]" ]]; then
echo "Something"
fi

thanx!

What shell are you using? I'm more use to single square brackets, but then I'm happiest in ksh

Robin

This has to be bash. @crimso - please remember to tell us what shell you use. It removes the guess factor.

your construct should be:

if [[ `hostname` =~ "t70cra2[01-40]" ]] || [[ `hostname` =~ "t70c6n2[01-28]" ]]; then

if [[ `hostname` =~ "t70cra2[01-40]"  ||  `hostname` =~ "t70c6n2[01-28]" ]]; then
1 Like

This can be done with a traditional "case" construct as well

case `hostname` in
 *t70cra2[01-40]*|*t70c6n2[01-28]*)
 echo true
 ;;
esac

Double square brackets do work in ksh and I would say their usage is recommended over their "slimmer" counterparts.
They protect their contents from the shell's globbing.
They also allow you to use variables without double quotes, without the fear of the test breaking.

mr. mcnamara,

i ended up using case|esac as it still doesn't work as expected and yes i am using bash. sorry about that as i should have known. anyways, if i log on to t70cra200 or t70c6n229 which are outside the "reach" of either the test ends up true. ie:

[root@t70cra200 scripts.dev]# echo $SHELL
/bin/bash
[root@t70cra200 scripts.dev]# if [[ `hostname` =~ "t70cra2[01-40]" || `hostname` =~ "t70c6n2[01-28]" ]]; then echo "Hello"; fi
Hello
[root@t70cra200 scripts.dev]#

You are running the hostname command twice:

if [[ `hostname` =~ "t70cra2[01-40]" ]] || [[ `hostname` =~ "t70c6n2[01-28]" ]]; then

Instead, for efficiency, call it once and save it into a variable and test that:

HOSTNAME=$(hostname)
if [[ $HOSTNAME  =~ "t70cra2[01-40]" ]] || [[ $HOSTNAME =~ "t70c6n2[01-28]" ]]; then

The right hand side should not use double quotes (these would only be used in the case of a string comparison).
[[ ... =~ t70cra2[01-40] ]] , and also [01-40]* is not a range in regex, nor is it a meaningful range in pattern matching (pattern matching uses the == operator).. In the case of regex we would also need to use anchors ( ^ and $ )

IMO it would be preferable to use pattern matching rather than of regex here: Something like this should work :

if [[ `hostname` == t70cra2[0-4][0-9] || `hostname` == t70c6n2[0-2][0-8] ]]; then

or

case `hostname` in 
  t70cra2[0-4][0-9]|t70c6n2[0-2][0-8]) echo hello ;;
esac

Yep, there is certainly a misunderstanding of the [character_range].
E.g. t70cra2[01-40] is a 0 or the 1-4 range i.e. t70cra2[0-4]

Like Scrutinizer said, with double quotes on the RHS, you are trying for a literal string match.
Though ugly and not easily maintainable, this does the work and also matches what you want (and not some other strings):

hostnm=$(hostname)
if [[ $hostnm =~ t70cra2([0-3][1-9]|(1|2|3|4)0)|t70c6n2([0-1][1-9]|(1|2)0|2[1-8]) ]]; then