Hi Team,
I know that "&" holds the result of current pattern match.
But what does "&&" means and its use please?
Thanks & Regards,
Batta Archana
Hi Team,
I know that "&" holds the result of current pattern match.
But what does "&&" means and its use please?
Thanks & Regards,
Batta Archana
In an awk script or a shell script it is used as a logical and operator:
awk :
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
[ -d a ] && ls a
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".
if [ $a && $b ]
then
echo "I am in IF block"
fi
Below syntax worked fine
if [ $a -a $b ]
then
echo "I am in IF block"
fi
Thanks & Regards
Batta Archana
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:
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).
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):
# 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:
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
:
[ "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:
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:
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:
mkdir "/some/dir" || echo "command mkdir /some/thing failed"
I hope this helps.
bakunin
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):
if [ $a ] && [ $b ]
then
echo "I am in IF block"
fi
Thank you guys. Now, i could understand the difference between && and -a.
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