2

Note: A B C are commands.....

Please tell what the step process would be of these commands

A || B && C = 
A && B ; C = 

What I am really after is something like this (but would like to understand the steps above):

If A fails, then run B, but if A succeeds, then run C (How can I do this with "||" ";" and/or "&&"?)

I found information from this link, but it only shows the steps of 2 commands, not 3.... Which one is better: using ; or && to execute multiple commands in one line?

6
  • 1
    Try doing it with and if statement. Jun 5, 2015 at 17:49
  • @Eduardo Cola.. I don't think I can do this on iPhone Mobile Terminal...
    – R0tten
    Jun 5, 2015 at 19:30
  • Then you should not be here, look for an Unix forum. Jun 5, 2015 at 19:31
  • Thank you @Eduardo Cola... I will finish up my last comment, let stand and look for a unix forum.
    – R0tten
    Jun 5, 2015 at 19:36
  • @R0tten the iphone does have bash if you jailbreak, so you might be able to put this in a bash script and run it
    – Rumesh
    Jun 6, 2015 at 3:00

1 Answer 1

3

Linguistically, you can think of statement1 && statement2 by itself as saying "execute statement1, and if its exit status is 0, then execute statement2." The other statement1 || statement2 syntax by itself translates to "execute statement1, and if its exit status is not 0, then execute statement2."

These operators are primarily intended for use within single conditions of if-constructs. But, they also have use in other places to compress confusing syntax.

The semicolon, ;, is just Bash's standard statement separator (e.g. it allows you to put two statements on the same line and run the 2nd immediately after the 1st, barring circumstances irrelevant to this explanation). Any statement after the semicolon will be interpreted nomatter the result (true/false) of the previous return value.

If A fails, then run B, but if A succeeds, then run C (How can I do this with "||" ";" and/or "&&"?)

A && C || B

Let's test it out:

john@mypc:~/test$ ls
file-A.txt  file-B.txt  file-C.txt
john@mypc:~/test$ cat file-A.txt && cat file-C.txt || cat file-B.txt
file A content
file C content
john@mypc:~/test$ rm file-A.txt 
john@mypc:~/test$ ls
file-B.txt  file-C.txt
john@mypc:~/test$ cat file-A.txt && cat file-C.txt || cat file-B.txt
cat: file-A.txt: No such file or directory
file B content
john@mypc:~/test$

As you can see, file B's contents were only printed when printing file A failed. Before we deleted file A, its content printed successfully and only file C's content printed afterward.

On the other hand, ; symbols are agnostic of the previous return code. For example given three commands X, Y, Z, X && Y && Z will not execute statement Z if Y (or X) returns a non-zero exit code, whereas (for all intents and purposes) X ; Y ; Z will execute all three independent of command X and Y's exit codes.

3
  • I would love to say that this is very helpful (which I think it is) but is way over my head! Basically, is there a syntax that would do this?: If A fails, then run B, but if A succeeds, skip B and run C,d,e,f etc
    – R0tten
    Jun 5, 2015 at 19:23
  • Am I right about this: A && C || B .....C will only run if A succeeds, and B will only run if C fails.....? This doesn't seem to be what I'm trying to do? Thank so much for helping me.
    – R0tten
    Jun 5, 2015 at 19:29
  • No; A && C || B should perform as you originally desired. This stuff can get very confusing so I added a more concrete example to the answer. Hope it helps!
    – John E
    Jun 6, 2015 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .