April 25, 2017

Relevant Linux Features: Control Operator

Control Operator


A Control Operator is a token that performs a control function.  It is one of the following symbols: || & && ; ;; ( ) | |& <newline>. We will focus on only && and || control operators in this article.

On occasion you might need to group Docker commands. Let's see a few ways to do this in Linux with three of the control operators.

Control operators Description
; Semicolon - delimits commands in a sequence
Used to run multiple commands one after the other
Similar to hitting ENTER after each command:

$ docker run --rm -it debian bash -c "ls /var; sleep 1; ls /"

Run the container and execute the three commands one after the other, separated by ; (semicolon)
&& AND - runs commands conditionally, on success
has the form A && B where B is run if, and only if A succeeds, i.e. if A returns an exit status of zero.


$ apt-get update && apt-get install -y openssh-server

This runs the second command, apt-get install -y openssh-server, IF AND ONLY IF the first command, apt-get update succeeded.
|| OR - runs command conditionally, on failure
has the form A || B where B is run if, and only if A fails, i.e. if A returns a non-zero exit status


This runs the second command, IF AND ONLY IF, the first command fails. In this example, since the first command, false will always fail, i.e. return a non-zero exit status, the second command, true, runs and sets the zero exit status.

No comments:

Post a Comment