Linux Format

QUICK REFERENCE TO… MULTI COMMANDS

-

When using the command line, there are often occasions when you need to run several commands in sequence – this can also happen when using a GUI, but the solution is nowhere near as simple. The classic example of this is the standard method of compiling software from source, this requires you to run ./configure (possibly with some arguments) followed by make then

make install .

Each of these steps can take anywhere from a few seconds to many hours, depending on the complexity of the code and speed of your machine. Waiting for one to complete before running the next is inefficien­t, so instead you could do:

./configure; make; make install

The semicolons cause the commands to be executed in sequence, as if you had run each one individual­ly. Some of you may have already noticed a potential problem here: what happens if ./configure or make fails? Will the subsequent commands try to execute anyway? The answer is yes, which would hide any error messages in the following output, and you may not even realise there was an error until you try to use the program. A safer way of running these commands is:

./configure && make && make install

&& is a logical operator. This command line actually means ‘if ./configure is true and make is true and make install is true’. Fortunatel­y for us, the shell determines if a command is true by running it to see if it gives any errors. If a command fails, there is no point in running the next one because the test has already failed, so joining commands with && runs them in sequence, but stops as soon as one of them returns an error, meaning we no longer need to babysit the shell.

The companion command to && is || , which means ‘or’. So in

command1 || command2 command2 only runs if command1 fails. This is less useful in the interactiv­e shell, but is used often in shell scripts:

somecomman­d1 || echo “Something broke!”

Newspapers in English

Newspapers from Australia