The key to shell scripting is to enter multiple commands and process the results of each, even passing the results of one command to another.

The shell allows you to string multiple commands together and execute them all at once. Format: Multiple commands in one line, using semicolons to each other; Separated. For example, output the time and login user: date +%Y%m%d; who

qinwanlideMacBook-Pro:~ qinwanli$ date +%Y%m%d; who 20203210/27/20 qinwanli console Oct 27 10:08 qinwanli ttys000 Oct 27 10:31 qinwanlideMacBook-Pro:~ qinwanli$Copy the code

The above date + % % m % d Y; Who puts together a simple script that uses only two bash shell commands. If multiple commands are separated by semicolon (;), the maximum number of characters on the command line is 255, and you need to re-enter each time. So, we put complex scripts in a text file, and we just rerun the text file each time.

shellScript file creation and running

To create a shell script file, we need to know how to create a file using the editor, see the Shell File editor. You also need to know the formatting requirements for shell file contents.

When you create a shell script file, you must specify the shell to be used on the first line of the file. The format is:

#! /bin/bash
Copy the code

Usually in shell script files, # is used to comment lines, but the first line of shell script files is an exception, after the pound sign! Specifies which shell to run the script on. (It is also possible to write scripts using the bash shell and specify other script execution, such as #! / bin/ZSH).

In shell script files, you can put multiple commands on the same line and use each other; Or you can write on a separate line. The shell executes the commands in the order they appear in the file.

To run a shell script file, we need to ensure that the shell can find our shell script file. There are two ways:

  • willshellThe directory where the script file resides is added toPATHEnvironment variables;
  • The absolute or relative file path is referenced at the promptshellScript file;

Next, we will date +%Y%m%d; This simple script is written to a shell script file and executed, while going through an important step of modifying the file permissions:

#1. Create a file
vim first_script
#first_script file contents:
  #! / bin/bash:
  # First 'shell' script used for testing
  #date +%Y%M%D
  #who

#2. Modify file permissions
chmod u+x first_script
#3. Execute the script using relative paths
./first_script
# 4. Output
20201410/27/20
qinwanli console  Oct 27 10:08 
qinwanli ttys000  Oct 27 10:31
Copy the code

When the script is running, you can use the echo command to output some text information to tell the user what the script is doing.

When the echo command outputs text, it displays the text without using quotation marks by default.

qinwanlideMacBook-Pro:~ qinwanli$ echo good job
good job
Copy the code

In special cases, the string contains quotation marks. In this case, use single or double quotation marks. Use one of them to quote the information to be output by echo, and use the other to output the quoted text. You can’t use the same quotation marks.

qinwanlideMacBook-Pro:~ qinwanli$ echo 'Rich says "scripting is easy".'
Rich says "scripting is easy".
qinwanlideMacBook-Pro:~ qinwanli$ echo "Rich says 'scripting is easy'."
Rich says 'scripting is easy'.
qinwanlideMacBook-Pro:~ qinwanli$ 
Copy the code

Add a text message to our script file:

 #! / bin/bash:
 # First 'shell' script used for testing
 echoThe system date is displayedechoWho is logging in to whoCopy the code

Output after execution:

The time of the output system on Tuesday, October 27, 2020 at 11:34:00 and 06:00 CST who is logging in qinwanli Console Oct 27 10:08 Qinwanli TTys000 Oct 27 11:20Copy the code

The text message and the command execution result are in the same line :echo -n MSG: modify the script :echo -n system time:; Date Execute output: System time: Tuesday, October 27, 2020 11:37.20 CST

Use the variable

The environment variable in the echo command is replaced with the current value when the script is run, variable substitution. There are two ways to use environment variables in shell script files:

  • When the variable name is clearly identifiable: Precede the environment variable$.
 #! /bin/bash
echoCurrent user:$USER Current user: Qinwanli
echo "User ID:$UIDOutput" User ID: 501 output
echo "The user's home directory:$HOME"User home directory: /Users/qinwanli
# note
# Single quotes, referenced variables, will not be read
echo User ID: $UID output User ID: $UID
Copy the code
  • When the variable name is not clearly identified:${variable}.
#! /bin/bash
echoThe user${USER}Has been turned onecho "User${USER}Already turned on."
Copy the code

Note: Echo uses single quotes to refer to variables in the output text. Variables are not read, only double quotes can be used.

In addition to environment variables, the shell allows users to define and use variables in scripts. Defining variables allows data to be stored temporarily and used throughout the script.

var1=hello; var2=world var3=56 var4="Hello world"
Copy the code

The shell script automatically determines the data type of the variable value. Variables defined in a shell script retain their values throughout the life of the script until they are deleted at the end of the shell script. Similar to system variables, user variables can be referenced by the dollar sign $, or ${variable}.

Note: use $when referring to a variable, not when assigning to another variable.

var1=hello; var2=worldecho $var1 $var2 #hello worldvar1=hello; var2=world var3=$var2
var4="$var3 !"When there is a space in a variable value, 'bash shell' treats the value as a separate command and must use quotation marks.
echo "$var1 $var4"#hello world !
Copy the code

Command substitution

Command substitution: Extracts information from command output and assigns it to a variable. There are two ways to assign command output to a variable:

  • Backquote character
#! /bin/bash
var=`date +%Y%m%d`
echo $var # Output: 20201027
Copy the code
  • The $()format
#! /bin/bash
var=$(date +%Y%m%d)
echo $var # Output: 20201027
Copy the code

Note: Command substitution creates a subshell to run the corresponding command.

Redirect input and output

The bash shell provides several operators that redirect the output of a command to another location (such as a file). Redirection can be used for input or output and can redirect files to command input.

Output redirection

The most basic redirection is to send the output of a command to a file, and the bash shell does this with the greater than sign (>), in the basic command format: command > outputfile.

# Time output to the desktop file1 file
date > ~/desktop/file1
# check
vim ~/desktop/file1
# showTuesday, October 27, 2020 at 16:02:52 CST# Log in user output to file1
who >  ~/desktop/file1
# check
vim ~/desktop/file1
# show
  qinwanli console  Oct 27 10:08
  qinwanli ttys000  Oct 27 15:44
Copy the code

From the above command execution, we find that when > redirects output to the same file, the contents of the file are overwritten.

If you do not want to overwrite the file, but want to append the output of the command to an existing file, use the double greater-than >> command.

# Append data to file1
date >> ~/desktop/file1
# check
vim ~/desktop/file1
# showQinwanli Console Oct 27 10:08 Qinwanli TTys000 Oct 27 15:44 2020 Tuesday October 27 16:14:11 CSTCopy the code

Input redirection

Input redirection redirects the contents of a file to a command. The operator is < <, and the format is command < inputFile.

Enter the file content into the command
wc < ~/desktop/file1
# output
 3      14     112 file1
Copy the code

The WC command outputs how many lines, words, and bytes of the input text file.

There is another method of input redirection, called inline Input redirection. Instead of using files for redirection, you simply specify on the command line the data to redirect the input.

The operator for inline input redirection: the double less than sign <<. In addition to this symbol, we must specify a text tag to divide the beginning and end of input data. Any string can be used as a text tag, but the text tags must be consistent at the beginning and end of the data. Format for:

command << Marker # can be any string, the beginning and end of which must be consistent with data Marker # can be any string. The beginning and end must be the same
Copy the code

Note: When using inline input redirection on the command line, the shell uses the sub-prompt defined in the PS2 environment variable. Specific use:

# enter
wc << Tag # step1: The prompt will continue to prompt for more input data until we enter the string as the text tag > tag # means that the input data is finished, and press enter will execute 'wc'
# output2, 3, 32Copy the code

The pipe

Sometimes you need to take the output of one command as the input of another. You can also do this with redirection, but it’s a little clunky, like:

# File information redirects output to file
ls -l > file
# Sort files for output
sort < file
# Output the sorted result
Copy the code

When the output of one command is used as the input of another command, you can directly redirect the information to another command without redirecting it to a file. Pipeline operators: |, format: command1 | command2.

Note: The two commands in the pipeline are not executed sequentially. The Linux system actually runs both commands at the same time, connecting them internally. While the first command produces output, it is immediately sent to the second command. The data is transferred without any intermediate files or buffers.

Specific use:


 # List the contents of the directory and sort them
qinwanlideMacBook-Pro:test qinwanli$ ls | sort
# output
AppDelegate.h
AppDelegate.m
Assets.xcassets
Base.lproj
Info.plist
SceneDelegate.h
SceneDelegate.m
ViewController.h
ViewController.m
main.m
Copy the code

When the amount of output data is relatively large, you can also execute more or less in a pipe to facilitate viewing:

ls | sort | more
Copy the code

Pipes are used in conjunction with redirected outputs

ls | sort > file
Copy the code

Mathematical operations

There are two ways to handle math in shell scripts:

  • exprCommand;
  • Use square brackets[]

exprPerform mathematical operations

exprTo perform mathematical operations, supported operators:

Terminal use:

expr 1 + 5
# Output: 6
expr 3 * 5
Expr: syntax error
# Solution, '*' with escape '\'
expr 3 \* 5
# Output: 15
Copy the code

Shell scripts using the expr command are complicated and require command substitution to get the output of the expr command:

#! /bin/bash
#samplevar1=3; var2=4echo "To perform an operation:$var1 * $var2"
var3=$(expr $var1A \ *$var2)
echoOutput:$var3
Copy the code

Execute script output:

Execute operation :3 * 4 output :12Copy the code

Use square brackets

The bash shell includes the expr command to remain compatible with the Bourne shell(sh), but it also provides an easier way to execute mathematical expressions: $[operation].

Terminal use:

echo$(1 + 5]# Output: 6
echo$(3 * 5)# Output: 15
Copy the code

Script execution:

#! /bin/bash
#samplevar1=3; var2=4echo "To perform an operation:$var1 * ($var21)"
var3=$[$var1 * ($var21)]echoOutput:$var3
Copy the code

Execute script output:

Execute operation :3 * (4-1) output :9Copy the code

One of the biggest limitations to using square brackets for math in the bash shell is floating-point calculations:

echo$[l]# output
1
Copy the code

Integer arithmetic is supported in bash.

It is worth mentioning that ZSH provides full floating-point arithmetic operations.

bashFloating-point arithmetic operations in

So how do you do floating-point arithmetic in bash? The answer is to use bash’s built-in calculator, using the command BC. Bash calculator (BC), which recognizes:

  • Numbers (integer and floating point)
  • Variables (simple variables and arrays)
  • Comment (in#Or C/ * * /The first line)
  • expression
  • Programming statements (such as if-then statements)
  • function

Use at the terminalbcTo perform an operation:

# enter ` BC `
qinwanlideMacBook-Pro:test qinwanli$ bc
# outputBC 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation Inc. This is free software with ABSOLUTELY NO WARRANTY. For detailstype `warranty6.28 # To exit 'BC', you must enter 'quit' quit # Return qinwanlideMacBook-Pro:test Qinwanli $Copy the code

To exit BC, you must enter quit.

Floating point operations are controlled by the built-in variable scale. We must set this value to the number of decimal places we want to keep in the result of the calculation, otherwise we will not get the desired result:

qinwanlideMacBook-Pro:test qinwanli$ bc
# Default scale=03.14/5 0# set scale = 23.14/5 scale = 2. 62# set scale = 4Scale =4 3.14/5.6280 quitCopy the code

The -q command enables you to start the bash calculator without displaying its verbose introduction.

bc -q
# Start typing3 * 4 to 12Copy the code

inbcUse variables in:

qinwanlideMacBook-Pro:test qinwanli$ bc -q
# Define variables
var1=3
var2=7
var3=5
# Define variable expressions
var4=var1*(var2-var3)
Print = print
print var4
6
quit
qinwanlideMacBook-Pro:test qinwanli$ 
Copy the code

Used in scriptsbc:

Using BC in a script relies on command substitution to assign the result of BC calculation to a variable defined in the script. The script uses the basic format of BC: variable=$(echo “options; Expression “| BC), unscramble: echo output string, enter through the pipe to the BC, after execution, the output, use the command substitution, the result variable assigned to variables. The BC script uses the following example:

#! /bin/bash
Use BC in scripts
echoThe following is an example of BC calculation in the script:echo1. Define the script variable VAR1 =12.25 var2=4.55echo2. Calculate var1* VAR2 usage'bc'I'm going to calculate it and I'm going to'bc'Output results, direct outputecho $(echo "scale=4;$var1*$var2" |bc)

echo3. Calculate VAR1 / VAR2, and then'bc'The result of the output, assigned to a variable var3 var3=$(echo "scale=2;$var1/$var2"|bc)

echoOutput var3 =$var3

echo4. Calculate VAR3 * VAR1, and then'bc'Output the result, assign a value to a variable var4, and print var4=$(echo "scale=4;$var3*$var1"|bc)

echoFinal output: VAR4 =$var4
Copy the code

Output after the script is executed

QinwanlideMacBook -Pro:desktop Qinwanli $./first_script The following is an example of BC calculation: 1. Define script variable 2. Calculate VAR1 * VAR2 use BC to calculate, and output the result of BC, directly output 55.7375 3. Calculate VAR1 / VAR2, assign the output of BC to a variable var3 and output VAR3 =2.69 4. Var4 =32.9525 qinwanlideMacBook-Pro: Desktop Qinwanli $. Var4 =32.9525 qinwanlideMacBook-Pro: Desktop Qinwanli $Copy the code

When using BC in scripts, there is a lot of computation involved, and it can be cumbersome to list many expressions in a single expression, such as:

var4=$(echo "scale=4; var5=$var3*$var1; var5*10"|bc)Copy the code

BC can recognize inline redirection, so you can use this. You can use inline redirection to input the expression that is evaluated into BC in the following format:

variable=$(bc << EOF
options
statements
expressions
EOF )
Copy the code

Use the sample

#! /bin/bash
Use BC in scripts
echoThe following is an example of BC calculation in the script:echo1. Define the script variable VAR1 =12.25 var2=4.55echoVar4 =$(BC <<)input
scale=3
var3=$var1*$var2
var5=$var1+$var2
var3+var5
input
)

echoFinal output: VAR4 =$var4
Copy the code

Perform output

QinwanlideMacBook -Pro:desktop Qinwanli $./first_script The following is an example of BC calculation: 1. Var4 =72.537 qinwanlideMacBook-Pro: Desktop Qinwanli $Copy the code

Exit the script

Exit script: Every command that runs in the shell uses an exit status code to tell the shell that it has finished running. The exit status code is an integer ranging from 0 to 255. By default, the status code for successful exit is 0:

QinwanlideMacBook -Pro:~ qinwanli$date 2020 October 27 Tuesday 20:57 12:31 CST qinwanlideMacBook-Pro:~ Qinwanli $date 2020 October 27 Tuesday 20:57 12:31 CST qinwanlideMacBook-Pro:~ Qinwanli $echo $?
0
Copy the code

Use echo $? You can view the exit status code of a command. By default, shell scripts exit with the exit status code of the last command in the script. We can also proactively add an exit status code to our script, making sure it is in the range 0 to 255, or specify variables

#! /bin/bash
var=4
exit $var
# Specify a number
exit 0
Copy the code

The resources

Linux command line and shell script programming