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