[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 ?
Help would be appreciated.
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:
"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:
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:
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, ...).
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.
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:
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 09:12 AM..
These 2 Users Gave Thanks to bakunin For This Post:
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:
And this is what happens in the request: Variable B collects all packages to be installed and D all packages to be updated.
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 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)
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)
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)
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)
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)
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)
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)