Sponsored Content
Full Discussion: && meaning in UNIX
Top Forums UNIX for Beginners Questions & Answers && meaning in UNIX Post 302966010 by bakunin on Saturday 6th of February 2016 06:16:57 AM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 09:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy