How come this if statement doesn't work?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How come this if statement doesn't work?
# 1  
Old 03-05-2013
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?

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

thanx!
# 2  
Old 03-05-2013
What shell are you using? I'm more use to single square brackets, but then I'm happiest in ksh


Robin
# 3  
Old 03-05-2013
This has to be bash. @crimso - please remember to tell us what shell you use. It removes the guess factor.

your construct should be:
Code:
if [[ `hostname` =~ "t70cra2[01-40]" ]] || [[ `hostname` =~ "t70c6n2[01-28]" ]]; then

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

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 03-05-2013
This can be done with a traditional "case" construct as well
Code:
case `hostname` in
 *t70cra2[01-40]*|*t70c6n2[01-28]*)
 echo true
 ;;
esac

# 5  
Old 03-05-2013
Quote:
Originally Posted by rbatte1
What shell are you using? I'm more use to single square brackets, but then I'm happiest in ksh


Robin
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.
# 6  
Old 03-05-2013
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:

Code:
[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]#

# 7  
Old 03-05-2013
You are running the hostname command twice:
Code:
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:
Code:
HOSTNAME=$(hostname)
if [[ $HOSTNAME  =~ "t70cra2[01-40]" ]] || [[ $HOSTNAME =~ "t70c6n2[01-28]" ]]; then

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit 1 doesn't work

Hi tail -f $PROGPATH/NBU_pgbaserestore_$1.log | while read LOGLINE do if ] && ! ] then date "+%d.%B.%Y %H:%M:%S" echo "ERROR: NBU" echo "$LOGLINE" TAILKILL=$(pgrep -P $$ -x tail) kill -9 $TAILKILL exit 1 ... (3 Replies)
Discussion started by: kvaikla
3 Replies

2. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

My if statement doesn't work why?

I have the following and for some reason I can't have two options together. I mean if I choose -u and -p it won't work... why? #!/bin/bash resetTime=1 mytotalTime=0 totalHour=0 totalMin=0 averagemem=0 finalaverage=0 times=0 function usage() { cat << EOF USAGE: $0 file EOF } (10 Replies)
Discussion started by: bashily
10 Replies

5. Solaris

shutdown -y -i5 -g0 DOESN'T work

hello, The command above seems not working on my solaris 8/9 sparc machines. a. resulted to the ff below when I instead use "shutdown" only. Broadcast Message from root (pts/1) on "hostname" date.. The system "hostname" will be shut down in 30 seconds THE SYSTEM bdosg IS BEING SHUT... (4 Replies)
Discussion started by: lhareigh890
4 Replies

6. Shell Programming and Scripting

bc: scope doesn't work for me

I am trying to use bc to calculate the difference between two nano second time stamps. bc does the calculation but seems to ignore the scale option: micro_start=$(date +%s.%N) # .. some stuff happens here micro_stop=$(date +%s.%N) TOT=$(echo "scale=3; $micro_stop - $micro_start" | bc)... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

7. UNIX for Dummies Questions & Answers

lp -o <option> doesn't work

why lp -o <option> doesn't work on my SCO Unix. (4 Replies)
Discussion started by: wendyz
4 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies

10. UNIX for Dummies Questions & Answers

my case statement doesn't work..

CO UNixware 7.1.1 Hi friends, I have chopped my case statementt out of my .profile and put it in another script called setsid. The case statement works when run from my .profile but not from my setsid file. All that the script does is set an environmental variable based on user input.... (7 Replies)
Discussion started by: sureshy
7 Replies
Login or Register to Ask a Question