Example 4

Concepts:
The if statement, the shift built-in bash command, and the grep command.

Text:
Implement a bash script that receives two parameters as command line arguments. The last parameter is a string containing the name of a command. The script must produce the list of all the running processes launched with a command containing the string passed to the script as a parameter. The produced list must contain the same fields reported by the program ps -ef. The first parameter (-h) is optional. If it is present, the bash script must print the header (i.e, the first line) produced by the command ps.

Example:
Here two possible outputs of the script:

$ ./es -h bash
UID        PID  PPID  C STIME TTY          TIME CMD
user      2454  2449  0 18:05 pts/2    00:00:00 bash
user      2816  2449  0 18:09 pts/4    00:00:00 bash
user      2944  2449  0 18:14 pts/5    00:00:00 bash
user      3249  2816  0 18:32 pts/4    00:00:00 bash
$ ./es bash
user      2454  2449  0 18:05 pts/2    00:00:00 bash
user      2816  2449  0 18:09 pts/4    00:00:00 bash
user      2944  2449  0 18:14 pts/5    00:00:00 bash
user      3249  2816  0 18:32 pts/4    00:00:00 bash

Solution:

bash_ex4.sh
# Implement a bash script that receives two parameters as command line arguments.
# The last parameter is a string containing the name of a command.
# The script must produce the list of all the running processes launched with a command containing
# the string passed to the script as a parameter.
# The produced list must contain the same fields reported by the program ps -ef.
# The first parameter (-h) is optional. If it is present, the bash script must print the header
# (i.e, the first line) produced by the command ps.
 
# Examples:
# Input: ./es -h bash
# UID        PID  PPID  C STIME TTY          TIME CMD
user        2454  2449  0 18:05 pts/2    00:00:00 bash
user        2816  2449  0 18:09 pts/4    00:00:00 bash
user        2944  2449  0 18:14 pts/5    00:00:00 bash
user        3249  2816  0 18:32 pts/4    00:00:00 bash
 
# Input: ./es bash
user        2454  2449  0 18:05 pts/2    00:00:00 bash
user        2816  2449  0 18:09 pts/4    00:00:00 bash
user        2944  2449  0 18:14 pts/5    00:00:00 bash
user        3249  2816  0 18:32 pts/4    00:00:00 bash
 
 
#!/bin/bash
 
HEADER="false"
PSCMD="/bin/ps -ef" # -e all the processes, -f full-format listing
if [ "$1" = "-h" ] ; then
    HEADER=true
    echo "Script parameters before shift: $*"
    shift
    echo "Script parameters after shift: $*"
fi
 
if [ -z "$1" ] ; then # This condition is tree if length of $1 is 0
    echo Usage: $0 [-h] process
    exit 1;
fi
 
if [ "$HEADER" = "true" ] ; then
    $PSCMD 2> /dev/null | head -n 1
fi
$PSCMD 2> /dev/null | grep "$1" | grep -v grep
 
exit 0

Comments:

The command shift shifts to the left of one position the values stored in the positional command line parameters (i.e., $0, $1, $2, …). The value $# is modified coherently by the shift command. For instance, the bash script:

./script.sh par1 par2 par3

generates the following mapping:

After the execution of the shift command, the parameters passed to the script (with the exclusion of $0) are shifted to the left of one position:

The command shift followed by a number shifts the values stored in the positional command line parameters of a quantity equal to the number.

The shift command makes it possible to easily manage scripts with different numbers of command arguments, as in the presented example.