Monday, August 27, 2018

Working with multiple commands

In Shell scripting, we need to check if the last command has successfully executed or not. For example, whether a file or directory is present or not. As per the result, our Shell script will continue processing.

The status of the last command execution is stored in ?. The range of numerical value stored in ? will be from 0 to 255. If successful in execution, then the value will be 0; otherwise, it will be non-zero

echo $?

The $ symbol tells that it is a command, not a normal text.

the backward quote such as "`". This key is normally situated below the Esc key. If we place text between two successive back quotes, then echo will execute those as commands instead of processing them as plane text.

Alternate syntax for $(command) is the backtick character "`", which we can see as follows:
$(command) or `command`

Use proper double inverted commas:
$ echo "Hello, `whoami`."
$ echo "Hello, $(whoami)."


A command line can consist of multiple commands. Each command is separated by a semicolon, and the command line is terminated with a newline. The exit status is that of the last command in the chain of commands.

The first command is executed, and the second one is started as soon as the first one has finished.
$ w; date

$ w ; date > sampleoutputfile
Output of the date command will be redirected to the sampleoutputfile file.

Commands may also be grouped so that all of the output is either piped to another command or redirected to a file.
$ ( ls; pwd; date ) > outputfile
The output of each of the commands is sent to the file called outputfile. The spaces inside the parentheses are necessary.

Logical operators:

Command1 & command2
The first command is started in the background to continue until it has finished;immediately after starting first command, the second command is started and it will run in the foreground.

$ find / -name "*.z" & ls

In the preceding example, first command such as find will start running in the background and while the find command is running in background, the ls command will start running in foreground.

Command1 && command2

The second command is only started if the first command is successful. To achieve this, the shell checks the exit (return) status of the first command and starts the second command only if and when that exit status is found to be "0".
$ ls /home/ganesh && echo "Command executed successfully"
Since we are working as user ganesh,
$ ls /root && echo "Command executed successfully"
Since we are working as a normal user, we cannot access the /root directory.Therefore, nothing will be printed on screen.

Command1 || command2
The second command is only started if the first command fails. The shell checks the exit status of the first command and starts the second command only if that exit status is not equal to "0".
$ ls /root || echo "Command execution failed"
Example:
$ ls || echo "command ls failed"

In the preceding example, if ls runs successfully, then echo will not be called. If the ls command fails such as $ ls /root and if user is not root, then ls will fail and the echo command will print command ls failed.
When && or || are used, the exit status of the first command is checked first, then the decision to perform the next will be taken.
$ [[ "a" = "b" ]] && echo ok

In this case, the [[ ]] expression will evaluate to false. As the first expression is false, the "&&" operator will not proceed to execute the next command.
In this case, ok will be printed only if [[ ]] is true.

Pipes: is a tool for inter-process communication.
$ command_1 | command_2

In this case, the output of command_1 will be send as an input to command_2. The limitation is that the communication is half duplex. This means the data can flow in only one direction. Normally for inter-process communication, you need to open files then get the file descriptor. This will be used to write to the pipe file. Again, we need to create a Fifo file by special commands. The preceding technique simplifies all this process. We only need to insert "|" in between the two processes. The operating system creates one intermediate buffer. This buffer is used for storing the data from one command and will be used again for the second command.

A simple example is as follows:
$ who | wc

The preceding simple command will be carrying out three different activities.First, it will copy the output of the who command to the temporary file. Then the wc command will read the temporary file and display the result. Finally, the temporary file will be deleted.

Normally, there will be two processes. The first command is the writer process. The second process is the reader process. The writer process will write to temp_file and the reader will read from temp_file. Examples of writer processes are ps, ls, and date. Examples of reader processes are wc, cat, and sort.

No comments:

Post a Comment