Different performance of shell script programming in UNIX and Linux

Source: Internet
Author: User

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

 
 
  1. #!/usr/bin/ksh  
  2. if [[ $# -lt 2 ]]  
  3. then  
  4. echo "Usage: ${0##*/} <file name #1> <file name #2> 
  5. exit 0  
  6. fi  
  7. if [[ ! -f "${1}" ]]  
  8. then  
  9. echo "Unable to find file '${1}'"  
  10. exit 1  
  11. fi  
  12. if [[ ! -r "${1}" ]]  
  13. then  
  14. echo "Unable to read file '${1}'"  
  15. exit 2  
  16. fi  
  17. gzip ${1}  
  18. ls -l ${1}.gz  
  19. if [[ ! -f "${2}" ]]  
  20. then  
  21. echo "Unable to find file '${2}'"  
  22. exit 1  
  23. fi  
  24. if [[ ! -r "${2}" ]]  
  25. then  
  26. echo "Unable to read file '${2}'"  
  27. exit 2  
  28. fi  
  29. gzip ${2}  
  30. 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

 
 
  1. #!/usr/bin/ksh  
  2. exit_msg() {  
  3. [[ $# -gt 1 ]] && echo "${0##*/} (${1}) - ${2}"  
  4. exit ${1:-0}  
  5. }  
  6. [[ $# -lt 2 ]] && exit_msg 0 "Usage: ${0##*/} <file name #1> <file name #2> 
  7. for _FNAME in $@  
  8. do  
  9. [[ ! -f "${_FNAME}" ]] && exit_msg 1 "Unable to find file '${_FNAME}'"  
  10. [[ ! -r "${_FNAME}" ]] && exit_msg 2 "Unable to read file '${_FNAME}'"  
  11. gzip ${_FNAME}  
  12. ls -l ${_FNAME}.gz  
  13. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.