Linux - Some Skills for Writing Shell

Posted by ryanb on Mon, 17 Jun 2019 02:12:21 +0200

1. Add comments to scripts

Annotations in scripts can help you or others understand what different parts of the script do when they look through your script.

Annotations are defined with #.

2. Exit the script when it fails to run

Sometimes even if some commands fail, bash may continue to execute the script, affecting the rest of the script (which eventually leads to logical errors). Exit script execution in the following line when a command fails:

# If the command fails to run, let the script exit execution
set -o errexit 
# or
set -e

Give an example

#!/bin/bash
# If the command fails, let the script exit
set -o errexit 
# Let the script exit if unset variables are used
set -o nounset
echo "Names without double quotes" 
echo
names="Tecmint FOSSMint Linusay"
for name in $names; do
  echo "$name"
done
#Define an incorrect command
failure_cmd=$(pwde)
echo
echo "Names with double quotes" 
echo
for name in "$names"; do
  echo "$name"
done
exit 0

Execute scripts:

xgj@root>./test.sh 
Names without double quotes

Tecmint
FOSSMint
Linusay
./test.sh: line 12: pwde: command not found

3. Exit the script when Bash uses undeclared variables

Bash may also use undeclared variables that can cause logical errors. So inform bash in the following line to exit the script execution when it tries to use an undeclared variable:

# Let the script exit if there are unset variables
set -o nounset
# or
set -u

Example: VAR_NO_DEFINE is undefined and used in scripts

#!/bin/bash
# If the command fails, let the script exit
set -o errexit 
# Let the script exit if unset variables are used
set -o nounset
echo "Names without double quotes" 
echo
names="Tecmint FOSSMint Linusay"
for name in $names; do
  echo "$name"
done
echo $VAR_NO_DEFINE
echo
echo "Names with double quotes" 
echo
for name in "$names"; do
  echo "$name"
done
exit 0

Execute scripts:

xgj@root>./test.sh 
Names without double quotes

Tecmint
FOSSMint
Linusay
./test.sh: line 12: VAR_NO_DEFINE: unbound variable

4. Use double quotation marks to refer to variables

When quoted (using the value of a variable), double quotation marks help prevent word splitting due to spaces and unnecessary matching due to recognition and extension of wildcards.

#!/bin/bash
# If the command fails, let the script exit
set -o errexit 
# Let the script exit if unset variables are used
set -o nounset
echo "Names without double quotes" 
echo
names="Tecmint FOSSMint Linusay"
for name in $names; do
  echo "$name"
done
echo
echo "Names with double quotes" 
echo
for name in "$names"; do
  echo "$name"
done
exit 0

Execute scripts:

xgj@root>./test.sh 
Names without double quotes

Tecmint
FOSSMint
Linusay

Names with double quotes

Tecmint FOSSMint Linusay

5. Use functions in scripts

Except for very small scripts (only a few lines of code), always remember to use functions to modularize the code and make the scripts more readable and reusable.

The grammar for writing functions is as follows:

function check_root(){
  command1; 
  command2;
}
# or
check_root(){
  command1; 
  command2;
}

When writing a single line of code, termination symbols are used after each command:

check_root(){ command1; command2; }

6. Use = instead of = when comparing strings==

Note that == is a synonym for = so only a single = is used for string comparison, for example:

value1="aaa"
value2="bbbb"
if [ "$value1" = "$value2" ]

7. Replace with $(command) instead of the old command

Command substitution replaces the command itself with the output of the command. Command substitution is done with $(command) instead of quoted command.

This approach is also recommended by the shell heck tool, which displays warnings and suggestions for shell scripts. For example:

user=`echo "$UID"`
user=$(echo "$UID")

8. Declare static variables with readonly

Static variables do not change; their values cannot be modified once defined in the script:

readonly passwd_file="/etc/passwd"
readonly group_file="/etc/group"

9. Environment variables are named in uppercase letters, while custom variables are named in lowercase.

All bash environment variables are named in uppercase letters, so use lowercase letters to name your custom variables to avoid conflicting variable names:

# Define custom variables in lowercase and environment variables in uppercase
nikto_file="$HOME/Downloads/nikto-master/program/nikto.pl"
perl "$nikto_file" -h  "$1"

10. Always debug long scripts

If you're writing bash scripts with thousands of lines of code, debugging can become a nightmare. In order to make it easy to correct some errors before the script is executed, some debugging is needed. Learn this technique by reading the following guidelines

Debugging mode with Shell script enabled in Linux

Execute grammar checking debugging mode in Shell scripts

Tracking the execution of debugging commands in Shell scripts

Topics: shell REST Linux