&& meaning in UNIX

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers && meaning in UNIX
# 1  
Old 02-06-2016
&& meaning in UNIX

Hi Team,

I know that "&" holds the result of current pattern match.
But what does "&&" means and its use please?

Thanks & Regards,
Batta Archana
# 2  
Old 02-06-2016
In an awk script or a shell script it is used as a logical and operator:
awk :
Code:
if ($1=="A" && $2=="B") {print $3}

In shell command lines (or scripts) it is also used to create a multiple statement.
It tests the return code of the previous statement, and if it returns OK which in shell is 0, then it does the next step
Example: if directory a exists, list it
Code:
[ -d a ] && ls a

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-06-2016
Hi,

I am not sure about awk but in Shell scripting the logical and operator should be "-a" but not "&&". Isn't it??

I tried below script using && which didn't work. But it worked only after replacing && with "-a".

Code:
if [ $a && $b ]
then
   echo "I am in IF block"
fi

Below syntax worked fine
Code:
if [ $a -a $b ]
then
   echo "I am in IF block"
fi

Thanks & Regards
Batta Archana

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for code and sample output. Thank you.

Last edited by bakunin; 02-06-2016 at 06:48 AM..
# 4  
Old 02-06-2016
Quote:
Originally Posted by Archana Batta
I am not sure about awk but in Shell scripting the logical and operator should be "-a" but not "&&". Isn't it??
No, it isn't. Note that there is no such thing as a "meaning of <whatever> in UNIX" and this is where your confusion perhaps comes from.

When you write the following in a shell script:

Code:
if [ .... ] ; then
    something
else
    other
fi

what happens is the following:

The keyword if is interpreted by the shell. "if" will execute the command immediately following, evaluate its return code and execute everything between the keyword then and the keyword else if this return code is 0 (zero), otherwise the part between else and fi.

You can try this out by doing the following: "root" is a user known to exist and will therefore have a line in the file /etc/passwd, "blabla" is a use i suppose will not exist (if it does, use another name known to not exist).

Code:
if grep -q "root" /etc/passwd ; then
     echo "this user exists"
else
     echo "this user does not exist"
fi

Replace the "root" by "blabla" and run again.

You probably ask yourself what the "[" now is. In fact it is a command: the command /bin/test in disguise. Test it (this is from a Fedora installation, your result might look slightly different):

Code:
# ls -l /bin/[
-rwxr-xr-x. 1 root root 41496 Sep 16 20:06 /bin/[

To make shell scripts look more like ordinary programming languages the developers of the UNIX shell devised this (alias-name) and, because this would have resulted in command lines like this:

Code:
if [ <condition> ; then

where the bracket is opened but not closed, they further decreed that, if /bin/test is called as [, the ] is to be given as the last argument.

You can test that easily by trying [ as a single command, without any if:

Code:
[ "x" == "y" ] ; echo $?
[ "x" == "x" ] ; echo $?

"echo $?" displays the return code of /bin/test, alias [: a 1 for the first line (which means logically FALSE) and a 0 for the second (which means logically TRUE).

Now, back to your original question: the command test does indeed not know about "&&" - for a list of parameters and logical evaluations issue man test and read there. But the shell (actually: some shells - the Bourne shell and all of its descendants, ksh, bash, .... do. There are other shells and they may or may not do the same) has a device which is, written generally:

Code:
command1 && command2

and it does the following: command1 is executed and if its return code is 0 then command2 is executed too, otherwise not. There is the opposite of "&&" too: "||". This:

Code:
command1 || command2

means that command1 is executed and if it returns a non-zero return code command2 is executed, otherwise not. An example would be:

Code:
mkdir "/some/dir" || echo "command mkdir /some/thing failed"

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 5  
Old 02-06-2016
Quote:
Originally Posted by Archana Batta
Hi,

I am not sure about awk but in Shell scripting the logical and operator should be "-a" but not "&&". Isn't it??

I tried below script using && which didn't work. But it worked only after replacing && with "-a".

Code:
if [ $a && $b ]
then
   echo "I am in IF block"
fi

Below syntax worked fine
Code:
if [ $a -a $b ]
then
   echo "I am in IF block"
fi

[..]
The -a operand is deprecated in the POSIX standard. The recommended way is to use the && operator like so (leaving the variable expansions unquoted, like in your example):
Code:
if [ $a ] && [ $b ]
then
   echo "I am in IF block"
fi

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-06-2016
Thank you guys. Now, i could understand the difference between && and -a.
# 7  
Old 02-07-2016
Quote:
Originally Posted by Archana Batta
.
.
.
I know that "&" holds the result of current pattern match.
.
.
.
That's true only in regexes of certain text processing commands like sed or awk. In the shell (that your are dealing with in the rest of the post), depending of contexts, it means
- send command / pipe to background
- duplicate file descriptors for redirection
- bitwise AND
- certain behaviour of case statements
and some other very special meanings/behaviours.
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. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

3. Shell Programming and Scripting

meaning of &1

All, In the below mentioned line --- $SCRIPT_FILES/wrapper_sleep.ksh > $MVXREFLOG 2>&1 what is the meaning of &1 here. Regards Oracle User (2 Replies)
Discussion started by: Oracle_User
2 Replies

4. 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

5. Shell Programming and Scripting

replace & with &amp; 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

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. Shell Programming and Scripting

Meaning of >&2

What does >&2 at the end of the statement echo "Alright man..." >&2 mean? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

8. Shell Programming and Scripting

meaning of & in sh shell

All, I have a line in my code like below , could any one please tell me what this actually mean what is the & doding there. I am in sh shell #!/bin/sh .............. mv &fname &III.tar.gz Thanks in Advance, Arun (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

9. UNIX for Dummies Questions & Answers

meaning of .& in shell

Hi , can anyone explain me the meaning of following line ". &13FNAME/version_encours/cfg/dfm.cfg" Regards (1 Reply)
Discussion started by: scorpio
1 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