[Solved] BASH - chaining TEST and COMMAND with && and II


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] BASH - chaining TEST and COMMAND with && and II
# 1  
Old 11-02-2012
[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing.
What I have understood is :
-- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found
-- branch B="$B $C" is to install the software
-- branch D="$D $C" is to update the software

How does [ "$A" == "0" ] && B="$B $C" || D="$D $C" works ?
Code:
A="0" or not
B=""
C="a_software-5.4.3-7.x86_64.rpm"
D=""
#
Download_the_package_and_put_it_in_tmp_directory   $C
#
if [ -f /tmp/$C ] ; then
    [ "$A" == "0" ] && B="$B $C" || D="$D $C"
else
        echo "Package not found"
fi

Help would be appreciated.
# 2  
Old 11-02-2012
As long as the command between && and || has an exit code of 0, the construct works like an if...then...else.

But if not ...

Code:
true && false || echo oops

is not the same as

Code:
if true
then
    false
else
    echo oops
fi

# 3  
Old 11-02-2012
Basically "&&" and "||" are logical operators, meaning "AND" and "OR".

Every command has a return value (=error level, $?) and the Unix convention is that a return value of "0" (=TRUE) means successful completion, everything else means failing for different reasons. A program might return the following values, for instance:

Code:
0     success
1     file(s) not found
127   parameter error - user gave a command line which doesn't make sense

"AND" only is true when both parts are true. "A AND B" only evaluates to true if A is TRUE AND B is TRUE, otherwise it is false. Because the programmers of these operators know this too they implemented "&&" to work this way: first run A, and if this returns FALSE there is no sense in attempting B, because the complete expression can never be TRUE, regardless of what B returns. This means in turn, that B is only attempted if A returns true.

Therefore the following two constructs are identical:

Code:
command1 && command2

if command1 ; then
     command2
fi

The same goes for "||", which is a logical OR. As it is an EXCLUSIVE OR, it is only true if exactly one of its operands are true. It is false if both are false and it is false if both are true. Therefore the system again runs the first operand and if this returns TRUE, it will not even attempt B, because the whole cannot result in TRUE any more. Only if A returns FALSE it will attempt B. In other words: the following two are equivalent:

Code:
command1 || command2

if ! command1 ; then
     command2
fi


Here is an example: we will use "cat", which returns an error level of "0" if it can display a file, but returns non-0 if the file name specified doesn't exist (or is not readable, ...).

Code:
cat /etc/passwd >/dev/null

This will display the contents of /etc/passwd to <stdout>, which is redirected to /dev/null. Basically this command will show nothing on the screen, but it will still produce a return level.

Code:
cat /etc/passwd >/dev/null; echo error level is $?
cat /some/path/blabla >/dev/null; echo error level is $?

The second file mentioned is supposed to not exist (should you happen to actually have the file "/some/path/blabla", replace it with another filename). Both commands will produce no output, but both will have different error levels.

Now we use this device with our logical operators:
Code:
cat /etc/passwd >/dev/null && echo "file /etc/passwd does exist"
cat /etc/passwd >/dev/null || echo "file /etc/passwd does not exist"
cat /some/path/blabla >/dev/null && echo "file /some/path/blabla does exist"
cat /some/path/blabla >/dev/null || echo "file /some/path/blabla does not exist"

All the first commands in each line are executed, but notice, that only the first and the fourth of the second commands are executed, depending on the existence of the mentioned files. Basically these lines are tests for the existence of the respective files, so it would have been easier to do than this. It should explain how the operators work, though.

I hope this helps.

bakunin

Last edited by bakunin; 11-02-2012 at 08:12 AM..
These 2 Users Gave Thanks to bakunin For This Post:
# 4  
Old 11-02-2012
Bakunin's explanation is correct and little to add for the cases covered. But the orig. request had a construct like A && B || C. In this case, C is the "fallback" branch in case A should fail:
Code:
$ true && echo "YES" || echo "NO"
YES
$ false  && echo "YES" || echo "NO"
NO

And this is what happens in the request: Variable B collects all packages to be installed and D all packages to be updated.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-21-2012
Thank you very much every body for taking time to help me.
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

If && command giving wrong output

Hi All, I am trying to run a script which will search for 2 strings(stopped,started) in a text file and echo an output depending on below condition -bash-3.2$ cat trial1.txt v ggg f -bash-3.2$ cat trial1.sh VAR9=` grep 'stopped' /tmp/trial1.txt` VAR10=` grep 'started'... (4 Replies)
Discussion started by: srkmish
4 Replies

3. Shell Programming and Scripting

Read input while another command is running &&

This script runs tshark, then displays the output. I want to read a keypress at all times throughout this script, especially during the 10 seconds that tshark is running. This code however, only takes input before tshark runs. #!/bin/bash main() { while (( "1" == "1" )) ; do keypress &&... (3 Replies)
Discussion started by: Kiah07
3 Replies

4. UNIX for Dummies Questions & Answers

[solved] Where & what bash env file, Mac OS?

Hi! I wanted to simplify my bash prompt, so I edited my etc/bashrc file. I thought this was the file that would override any other env files. When I opened it, I saw that the way it was setup was not what my prompt looked like, although I forget exactly what was there. But i edited it the way I... (1 Reply)
Discussion started by: sudon't
1 Replies

5. Shell Programming and Scripting

bash aliases and command chaining with ; (semi-colon)

What am I doing wrong here? Or is this not possible? A bug? alias f='find . >found 2>/dev/null &' f ; sleep 20 ; ls -l -bash: syntax error near unexpected token `;' (2 Replies)
Discussion started by: star_man
2 Replies

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

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

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

9. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

10. UNIX for Dummies Questions & Answers

sleep 10 && command doesn't work

Hi there, I found a trick to easily postpone a command by a few seconds: supernova:~# sleep 10 && command &If you logout, the command should still be executed... But not all the time. Could anyone of you explain me why the following command is executed even after logging out: supernova:~# sleep... (2 Replies)
Discussion started by: chebarbudo
2 Replies
Login or Register to Ask a Question