COMMAND
GROUPING
We can join commands on a command line in such a way
that the second command executes only if the first command has executed
successfully.
For eg, we can use a first command to check whether a
file exists and a second command to perform an operation on it if it exists.
NOTE:
1.
To make one command conditional on another,
we can join the commands using a double ampersand (&&). The command
after the && symbol executes only if the command before the &&
symbols produces a zero exit status – in other words, if its executes
successfully.
For eg:
$ ls userlist && sort userlist
In this eg, the ‘ls’ command checks whether the
‘userlist’ file exists. Because it does exist, the ‘ls’ command executes
without errors – so its exist state is zero. This causes the ‘sort’ command to execute.
Also, if we delete the ‘userlist’ file and run the
command again, the ‘ls’ command encounters an error – so its exit state is
non-zero. Because the ‘sort’ command is
conditional, the shell doesn’t attempt to execute it.
2.
We can use double pipe (||) to make a
command conditional on the unsuccessful execution of the previous command.
In such case, the second command
executes only if the first command has non-zero exit state.
For eg:
$ ls userlist || touch userlist
In this example, the ‘ls’ command
looks for a file called ‘userlist’. If it fails to find the file, the ‘touch’
command creates it.
Also, if the ‘ls’ command executes
successfully, this means that the file already exists. In this case, the ‘touch’
command doesn’t execute.
3.
Can group commands using braces ({}). The
shell treats any command block enclosed in braces as if it were a single
command.
This allows us to redirect input and
output to and from a group of commands.
For eg:
$ {sort | grep ‘che’}
In this example, the braces group the
‘sort’ and ‘grep’ commands into a code block so that the shell sorts input and
then extracts any lines containing the word ‘che’.
4.
Can redirect input and output to a command
block as if it were a single command.
For eg:
$ {sort | grep ‘Mexico’} mex_flights
In this example, the code specifies
Flight --->
file as input
Mex_flights --->
file as output
5.
Can group commands using round brackets –
Often called parentheses – instead of braces.
This causes the shell to spawn a subshell and execute the command
block in the subshell.
6.
Commands that execute in a subshell do not
affect what’s happening in the main shell.
This allows you to define variables
that exist only for the lifetime of the subshell, and to change the working
directory within the subshell without affecting the parent shell.
For eg:
$ (sort | grep ‘Mexico’) < massfile
> mex_info
Exercise:
Want to create a file named hostname
and containing the text server1.example.com. However, you don’t want to
overwrite any existing file by that name.
Ans: $ cat hostname || echo
server1.example.com > hostname
Justification: The use of the ||
ensures that the code that writes the output from the echo command to the
hostname file will only execute if the attempt to list the hostname file fails.