Difference between && and -a


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Difference between && and -a
# 1  
Old 03-01-2007
Difference between && and -a

I've come stuck when I was making sure the hour of the day was not been two times so that the rest of the script could not be executed.
Seems simple enough.
I used the -a to join the two conditions together and it would run if the conditions was t/f ( it is only supposed to run if was t/t). Smilie
I read on this forum about using && instead of -a. It now works. Smilie
Whats the difference? Smilie

Code that doesn't work using the -a, change this to && and it works. Smilie
WORK_START_HOUR=8 # 8.00am
WORK_END_HOUR=17 # 5.00pm
CHECK_HOUR=`date +"%H"`
CHECK_HOUR=18
if [ $CHECK_HOUR -ge $WORK_START_HOUR ] -a [ $CHECK_HOUR -le $WORK_END_HOUR ]
then
echo "Hour is in the range of 8 to 17"
else
echo "Hour is in outside the range of 8 to 17"
fi


thanks
brett
spookyrtd99
# 2  
Old 03-01-2007
You're doing two different things.

If you want to use -a, you need to use it within the test braces. You're making two tests using two sets of braces and need to use && for comparisons outside the test braces.

Code:
#!/bin/ksh

WORK_START_HOUR=8 # 8.00am
WORK_END_HOUR=17 # 5.00pm
CHECK_HOUR=`date +"%H"`
CHECK_HOUR=18
if [ $CHECK_HOUR -ge $WORK_START_HOUR ] && [ $CHECK_HOUR -le $WORK_END_HOUR ]
then
  echo "Hour is in the range of 8 to 17"
else
  echo "Hour is in outside the range of 8 to 17"
fi

Code:
#!/bin/ksh

WORK_START_HOUR=8 # 8.00am
WORK_END_HOUR=17 # 5.00pm
CHECK_HOUR=`date +"%H"`
CHECK_HOUR=18
if [ $CHECK_HOUR -ge $WORK_START_HOUR -a $CHECK_HOUR -le $WORK_END_HOUR ]
then
  echo "Hour is in the range of 8 to 17"
else
  echo "Hour is in outside the range of 8 to 17"
fi

Note the second one works because the entire test is inclosed in a single set of braces.

Carl
# 3  
Old 03-01-2007
Great thanks
Brett
spookyrtd99
# 4  
Old 03-02-2007
-a is used to combine 2 conditions
&& is used to combine to actions (which could be conditions)


if [ condition1 -a condition2 ] ...........

action1 && action2

e.g.
test -f /usr/bin/ls && /usr/bin/ls

First is test if the command /usr/bin/ls is present, if the test evolves to true then /usr/bin/ls is really executed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. UNIX for Dummies Questions & Answers

Difference between & and nohup &

Hi All, Can anyone please help me understanding what the difference between the below two? 1. script.sh & 2. nohup script.sh & (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

Replace & sign to &amp word

Hi, I have text file abc.txt. In this file, I have the following data. Input: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith & Mrs Smith Mr Smith& Mrs Smith Mr Smith &Mrs Smith Output: Mr Smith &amp Mrs Smith Mr Smith &apos Mrs Smith Mr Smith &amp Mrs Smith Mr Smith&amp... (4 Replies)
Discussion started by: naveed
4 Replies

4. Shell Programming and Scripting

replace & with & xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

5. Shell Programming and Scripting

About date & time difference

Hello All, I was having a look on threads on the Forum about time calculation but didn't find exactly this issue. For instance, if we have these 2 dates, begin & end : 20100430235830 20100501000200 Is there anyway, awk, ksh, perl to calculate the difference in sec and get for... (6 Replies)
Discussion started by: rany1
6 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. Solaris

difference b/w sol9 & sol10

what is the difference b/w sol9 and sol10 booting procedure?? Recently faced this question with HP... Thiru (2 Replies)
Discussion started by: tirupathiraju_t
2 Replies

8. UNIX for Advanced & Expert Users

Difference between s & S in setuid in UNIX

Hi, what is the difference btwn s and S in setuid , access permissions. I have to make to change the access permissions of a file to rwsr_xr_r but if i type in 4655 it changes the file to rwSr_xr_r . How can I make this change ? Please suggest. (2 Replies)
Discussion started by: astha rais
2 Replies

9. Shell Programming and Scripting

Difference between ./ & . ./ ???

For executing a shell script, i know 2 ways: 1) using sh command 2) making the script file executable & then use ./ But i can across another way for executing the scripts... using ". ./" I tried this way.. but i was able to understand the difference between "./" and ". ./" I would be very... (2 Replies)
Discussion started by: abishekmag
2 Replies

10. UNIX Desktop Questions & Answers

what is the difference between Unix & linux, what are the advantages & disadvantages

ehe may i know what are the difference between Unix & Linux, and what are the advantages of having Unix as well as disadvantages of having Unix or if u dun mind i am dumb do pls tell me what are the advantages as well as the disadvantages of having linux as well. thanks (1 Reply)
Discussion started by: cybertechmkteo
1 Replies
Login or Register to Ask a Question