Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the difference between shell script programming under UNIX and Linux

Shulou Source: shulou.com Published: 2022-06-01 04:58:10 10月25日 Update

This article mainly introduces what is the difference between shell script programming in UNIX and Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Like other UNIX operating systems and Linux, IBM? AIX? The operating system provides several powerful tools that help system administrators, developers, and users perform day-to-day tasks, making their or their customers' jobs easier. In UNIX, one of these tools can automate tasks by writing shell scripting, simplifying difficult, lengthy, or tedious tasks.

Although some people who have used UNIX in the past two years may have tried shell scripting, they are likely to only study the details of the operating system and are not proficient in shell scripting. This article is for readers who want to learn more about shell scripting and start writing more advanced scripts. This article provides the basics of scripting, including how to simplify scripts, how to keep scripts as flexible as possible, how to write clean scripts, how to write comments within scripts, and how to debug scripts.

Shell script programming keeps it simple

One of the problems that people often encounter when learning how to write shell scripts is to repeat what they have already done in another script. Instead of copying the original script and modifying several hard-coded values, they just create a function to handle the duplicate parts of the two scripts. Creating centralized functions can also promote standardization and help create unified scripts. If a function works well in one part of the script, it will work elsewhere in the script.

For example, the script shown in listing 1 should be condensed and simplified to a simpler, cleaner program.

Listing 1. Examples of scripts that can be simplified

#! / usr/bin/ksh if [[$#-lt 2]] then echo "Usage: ${0clients /} 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! Thankfully, it's just an example. This shell scripting should be condensed as much as possible. From an easy-to-read point of view, listing 2 provides a cleaner version.

Listing 2. A condensed version of the script in listing 1

#! / usr/bin/ksh exit_msg () {[[$#-gt 1]] & & echo "${0benchmark] /} (${1})-${2}" exit ${1V lt 0}} [[$#-lt 2]] & & exit_msg 0 "Usage: ${0mm Usage /} 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

Notice the difference between the two? This shell scripting adds a simple function to display a message and exit with the appropriate return code, and transfers all operations to a for loop, which makes the script look cleaner and easier to understand.

Thank you for reading this article carefully. I hope the article "what is the difference between shell script programming under UNIX and Linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Scripts scripting programming checklists work cleanliness functions articles systems operating systems help concentration under tasks just as far as possible tools versions knowledge examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi OPPO Reno Shulou Technology macOS