How can shell scripting be used better? Especially in UNIX and Linux operating systems. Next, let's take a look at how to use shell script programming.
Like other UNIX operating systems and Linux systems, IBM? AIX? The operating system provides several powerful tools to help system administrators, developers, and users perform their daily tasks, making their work or their customers easier. In UNIX, one of these tools can automatically execute tasks by programming shell scripts, which simplifies hard, long, or tedious work.
Although some people who have used UNIX in the past two years may have tried shell script programming, they may only study the details of the operating system and are not proficient in shell script programming. This article is intended for those who want to learn more about shell script programming and start to write more advanced scripts. This article provides the basics of script programming, including how to simplify the script, how to maintain the flexibility of the script as much as possible, how to write a clean script, writing notes in the script, and debugging the script.
Keep shell script programming simple
When people learn how to program shell scripts, they often encounter a problem: Repeat the work they have done in another script. In fact, they do not need to copy the original script and modify several hard-coded values. They only need to create a function to process the duplicate parts of the two scripts. Creating a centralized function also promotes standardization and helps you create a unified script. If a function works normally in one part of the script, it also works normally elsewhere in the script.
For example, the script shown in Listing 1 should be concentrated and simplified into a simpler and cleaner program.
Listing 1. Simplified script example
- #!/usr/bin/ksh
- if [[ $# -lt 2 ]]
- then
- echo "Usage: ${0##*/} <file name #1> <file name #2>
- exit 0
- fi
- if [[ ! -f "${1}" ]]
- then
- echo "Unable to find file '${1}'"
- exit 1
- fi
- if [[ ! -r "${1}" ]]
- then
- echo "Unable to read file '${1}'"
- exit 2
- fi
- gzip ${1}
- ls -l ${1}.gz
- if [[ ! -f "${2}" ]]
- then
- echo "Unable to find file '${2}'"
- exit 1
- fi
- if [[ ! -r "${2}" ]]
- then
- echo "Unable to read file '${2}'"
- exit 2
- fi
- gzip ${2}
- ls -l ${2}.gz
This script looks terrible! Thank God, it is just an example ). This shell script programming should be concentrated as much as possible. From the perspective of ease of reading, the version provided in Listing 2 is cleaner.
Listing 2. versioning the script in Listing 1
- #!/usr/bin/ksh
- exit_msg() {
- [[ $# -gt 1 ]] && echo "${0##*/} (${1}) - ${2}"
- exit ${1:-0}
- }
- [[ $# -lt 2 ]] && exit_msg 0 "Usage: ${0##*/} <file name #1> <file name #2>
- for _FNAME in $@
- do
- [[ ! -f "${_FNAME}" ]] && exit_msg 1 "Unable to find file '${_FNAME}'"
- [[ ! -r "${_FNAME}" ]] && exit_msg 2 "Unable to read file '${_FNAME}'"
- gzip ${_FNAME}
- ls -l ${_FNAME}.gz
- done
Have you noticed the difference between the two? This shell script program adds a simple function to display a message and exit with an appropriate return code. It also transfers all operations to a for loop, this makes the script look cleaner and easier to understand.