Page 36 - Raspberry Pi as a Foundation for Boosting Computer and Technology Literacy
P. 36
3 Fundamentals of the Linux system and terminal usage
is also frequently denoted by the jargon ‘piping’ in various online edu-
cational materials. To see it in action, consider the following command:
ls -S | head -n 5
The ls command is given the flag -S, which tells it to sort the cur-
rent directory contents by size. Next, its stdout is ‘piped’ (i.e. redirect-
ed) to the head command as its stdin. The head command then takes
the first five lines (i.e. five largest files and folders) and prints them out
in the terminal (Figure 3.8).
Chaining operators
Getting to know a few operators for connecting commands, such as
‘|’ and ‘>’, is certainly useful, and there are many more. However, we
will only discuss three more very frequently used chaining operators,
specifically the semicolon ‘;’, ‘&&’ (AND), and ‘||’ (OR).
Essentially, chaining operators allow us to create sequences of com-
mands by combining them. However, they differ in their execution be-
haviour. For instance, the semicolon ‘;’ character allows us to combine
multiple commands into one line, which will be sequentially executed
regardless of whether any of the commands fail or succeed. Consider
the following example:
pwd ; ls /non-existent/directory/ ; whoami ;
uname
The first, third, and fourth commands are successfully executed, and
their outputs are displayed in the terminal. The second command fails,
with its stderr also displayed, but this does not interrupt the execution
flow (Figure 3.9).
Next, consider the following ping and echo commands chained
together:
ping -c 1 google.com && echo -e “\nConnected to
the internet!”
Figure 3.9 Example of using the semicolon chain operator
36