Build a basic shell script

In my sophomore year, I learned shell script programming. This helped me a lot in the later use of the Linux system. It allowed me to complete some tasks more quickly. This is also a review series on the details of shell programming. I refer to the "Linux Command Line and Shell Script Programming Compendium 3rd Edition". I used the 2nd edition back then. Time flies so fast.

1. How does a simple script run

1. Create an empty file:

Insert picture description here

2. Write script content:

Insert picture description here

  • The second line starts with #, which means it is a comment and will not be executed.
  • The exclamation point after the first line # tells the shell which shell to use to run the script.

3. Modify file permissions:

Insert picture description here

  • Must have permission to run.

4. Run the script in the current directory:

Insert picture description here

  • Use ./script name to run the script in the directory where the script is located.
  • The script can be run in all directories, and the directory where the script is located must be added to the PATH variable.

Through such a simple script, we can obtain the effect of executing multiple shell commands, and the degree of customization is still high. It allows us to quickly perform a series of operations and obtain the required content. This is the shell script The advantages.

Second, the build script

1. Add your own text message to tell the script user what the script is doing, through the echo command

1. Add the echo statement to any place where additional information needs to be displayed in the shell script:
Insert picture description here
Insert picture description here
2. Use -n to display the text and command output on the same line:
Insert picture description here
Insert picture description here

2. Use variables

1. Use environment variables:
Insert picture description here
Insert picture description here

  • Add a dollar sign ($) before the variable name to use this variable.
  • The environment variable in the echo command will be replaced with the current value when the script is running.
  • Use the dollar sign and use \ to escape.

2. Use user variables:
Insert picture description here
Insert picture description here3. Extract information from the command output and assign it to variables:

  • Method 1: testing='date'
  • Method 2: testing=$(date)
    Insert picture description here
    Insert picture description here

The today variable is assigned to the output of the formatted date command. This is a technique commonly used to extract date information to generate log file names. The +%y%m%d format tells the date command to display the date as a combination of two-digit year, month, and day.

3. Redirect input and output

You can redirect the output of the command to another location (such as a file). Redirection can be used for input and output, and can redirect files to command input.

1. Output redirection: command> outputfile
Insert picture description here

  • Append redirection command >> outputfile

2. Input redirection: command <inputfile
Insert picture description here

  • The wc command can count the text in the data: the number of lines of the text, the number of words of the text, and the number of bytes of the text.

4. Pipeline

The process of using the output of one command as the input of another command is called piping: command1 | command2.
You can use any number of pipes in one command. You can continuously pipe the output of the command to other commands to refine the operation:
Insert picture description here
you can also use redirection and pipe to save the output to a file.

5. Perform mathematical operations

The ability to perform mathematical operations is very important to any programming language, but it must be inconvenient in the shell and requires the assistance of two commands.
1. The expr command:
ARG1 | ARG2 If ARG1 is neither null nor zero, return ARG1; otherwise, return ARG2.
ARG1 & ARG2 If no parameter is null or zero, return ARG1; otherwise return 0
ARG1 <ARG2 If ARG1 is less than ARG2, Return 1; otherwise return 0
ARG1 <= ARG2 If ARG1 is less than or equal to ARG2, return 1; otherwise return 0
ARG1 = ARG2 If ARG1 is equal to ARG2, return 1; otherwise return 0
ARG1 != ARG2 If ARG1 is not equal to ARG2, return 1; Otherwise return 0
ARG1 >= ARG2 If ARG1 is greater than or equal to ARG2, return 1; otherwise return 0
ARG1> ARG2 If ARG1 is greater than ARG2, return 1; otherwise return 0
ARG1 + ARG2 Return the arithmetic operations of ARG1 and ARG2 and
ARG1-ARG2 Return ARG1 and arithmetic difference ARG2 of
ARG1 * ARG2 return ARG1 and ARG2 arithmetic product of
ARG1 / ARG2 return ARG1 is ARG2 other arithmetic commercially
ARG1% ARG2 return ARG1 is ARG2 arithmetic remainder divided by
STRING: REGEXP If REGEXP matched to a STRING in Pattern, return the pattern matching
match STRING REGEXP If REGEXP matches a pattern in STRING, return the pattern matching
substr STRING POS LENGTH returns a substring starting at POS (counting from 1) and a length of LENGTH characters
index STRING CHARS returns the position where the CHARS string is found in STRING; otherwise, returns 0
length STRING returns the string STRING Numeric length

  • TOKEN interprets TOKEN as a string, even if it is a keyword
    (EXPRESSION) returns the value of EXPRESSION
    Insert picture description here
    2, using square brackets
    square brackets is more elegant than the expr command: when assigning a mathematical operation result to a variable, you can use the dollar sign and Square brackets ($[ operation ]) enclose mathematical expressions:
    Insert picture description here

But there is still a big problem: the bash shell mathematical operators only support integer operations. To perform any actual mathematical calculations, this is a huge limitation.
3. Floating point calculation The
most common solution is to use the built-in bash calculator, called bc: variable=$(echo “options; expression” | bc)
Insert picture description here
to list multiple expressions in a command line to perform a large number of calculations, It will be a little troublesome, but it can:
Insert picture description here
6, perfectly exit the script
every command running in the shell uses an exit status code (exit status) to tell the shell that it has run to completion.
For shell scripts, the script ends when the last command is run. Similarly, a status code is passed from the command to the shell when the command ends, and this value can be captured and used in the script to make the script exit more elegant.
The $? variable allows us to view the exit status code:
Insert picture description here

  • 0 command ended successfully
  • 1 General unknown error
  • 2 Unsuitable shell commands
  • 126 Command not executable
  • 127 Command not found
  • 128 Invalid exit parameter
  • 128+x Serious error related to Linux signal x
  • 130 Command terminated by Ctrl+C
  • 255 Exit status code outside the normal range

Insert picture description here

At this point, you can customize your own shell script, especially the combined output of some information and record-type operations, which can be completed by your script. If you want to add more execution logic to the script, you can see the next page. Articles.

Guess you like

Origin blog.csdn.net/dangfulin/article/details/107888086