In an awk script or a shell script it is used as a logical and operator:
awk :
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
This User Gave Thanks to jim mcnamara For This Post:
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:
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).
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):
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:
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:
"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:
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:
means that command1 is executed and if it returns a non-zero return code command2 is executed, otherwise not. An example would be:
I hope this helps.
bakunin
These 2 Users Gave Thanks to bakunin For This Post:
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".
Below syntax worked fine
[..]
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):
This User Gave Thanks to Scrutinizer For This Post:
.
.
.
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.
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)
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)
Hi, I have text file abc.txt. In this file, I have the following data.
Input:
Mr Smith & Mrs Smith
Mr Smith &apos Mrs Smith
Mr Smith & Mrs Smith
Mr Smith& Mrs Smith
Mr Smith &Mrs Smith
Output:
Mr Smith & Mrs Smith
Mr Smith &apos Mrs Smith
Mr Smith & Mrs Smith
Mr Smith&... (4 Replies)
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&data3</tag3>
Now i can grep & and replace with & for whole file but it will replace all... (4 Replies)
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)
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)
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)