Essential skills for improving work efficiency shell script (recommended collection for easy viewing)

shell programming

quick get start

First acquaintance

shell is the language that we communicate with the operating system through the command line.

shell scripts can be executed directly on the command line, or a set of logic can be organized into a file for reuse.

There are many common shell scripts in Linux:

  • Bourne Shell(/usr/bin/sh or / bin/sh)
  • Bourne Again Shell(/bin/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • zsh

However, bash is generally used by default in Linux systems

Script format

1. The script needs to be #/ Start with bin/bash

2. The script needs executable permission

Common execution methods of scripts

Method 1: enter the relative path or absolute path of the script

The prerequisite is that you must add executable permissions to the script

Mode 2: sh(bash) + script

This can be done without giving the script executable permission

Create the first Shell script

# Create a file
vim hello.sh

# Content edited in vim
#!/bin/bash
echo "hello world"

# Two modes of operation
# The first way
sh hello.py
# The second way
chmod u+x hello.sh
./hello.sh # Here you can use relative paths or absolute paths

shell variable

Introduction to shell variables

1. Variables in Linux shell are divided into system variables and user-defined variables

2. System variables: $HOME $SHELL $UESR, etc

3. Display the variables used in the current shell: set command

Definition of shell variables

Basic grammar

1. Define variable: variable = numeric value. Note: no spaces can be added on both sides of the equal sign

1.1 3 methods of variable definition

name1='zhy'  # Single quote definition string
name2="zhy"	 # Double quotation mark definition string
name3=zhy    # You can also express a string without quotation marks

1.2 using variables

When using variables, you need to add the $symbol or ${}. Curly braces are optional, mainly to help the interpreter identify the boundary of variables.

name=zhy    
echo $name      # zhy
echo ${name}    # zhy
echo ${name}ALG # zhyALG
echo $name ALG  # zhy ALG
echo $nameALG   # error

2. Delete variable: unset variable

name=zhy
unset name
echo $name # Output null

3. Declare static variables: readonly variable name or declare -r variable name. Note: static variables cannot be unset

name=zhy
readonly name   # Writing method 1
declare -r name # Writing method 2

name=abc        # report errors

4. To assign the return value of an instruction / command to a variable, you can use backquotes or $()

case

1. Define variable A and assign it as 666, print variable A and cancel variable A

2. Define variable B, assign value to 888, print and cancel

#!/bin/bash 
A=666
echo A=$A # Output A=666
echo "A=$A" # Both output modes are OK
unset A
echo A=$A # Output A = (no output here)

readonly B=888
echo B=$B # Output B=888

3. Assign the return value of the date instruction to the variable C

#!/bin/bash
# Two ways
C=`date`
C=$(date)

Setting environment variable of variable type

There are two types of variables:

1. User defined type variable (local variable)

Variables that cannot be accessed by child processes

2. Environment variables (global variables)

Variables accessible to child processes

Basic grammar

1. Change user-defined variable to environment variable

1.1.export variable name = variable value (set shell environment variable / global variable), or declare -x variable name

1.2.source configuration file (make the modified configuration file effective immediately)

name=zhy        # Define variables
# The first way
export name     # It can also be written directly in one line, that is, export name=zhy
# The source file name is required here

# The second way
declare -x name 

2. Change environment variable to user-defined variable

2.1 declare + X variable name

export name=zhy # Create an environment variable
declare +x name # Set as custom variable

case

1. Create a file HomePath in / home and set the environment variable HomePath

2. View HOMEPATH environment variable

3. View the environment variables in the shell program

cd /home
vim homepath

#!/bin/bash
export HOMEPATH=/home # Setting environment variables

source homepath # Refresh profile

echo $HOMEPATH # View environment variables

# Viewing environment variables in a shell script
vim a.sh
echo HOMEPATH=$HOMEPATH
sh a.sh

shell string

In fact, all variables in the shell are strings, so you need to know how to use strings and precautions

1. Single quotation marks and double quotation marks can be used for strings. The difference is that the content in the middle of single quotation mark '' will not be executed and will not be escaped. The content in the middle of the double quotation mark '' will be executed and escaped( As mentioned earlier, variables can be defined without quotation marks, and the effect without quotation marks is the same as that with double quotation marks)

name=zhy
echo 'hello $name \"ALG\"'  # hello $name \"ALG\"
echo "hello $name \"ALG\""  # hello zhy "ALG"
echo hello $name \"ALG\"    # hello zhy "ALG"

2. Get string length, ${# variable name}

name=zhy
echo ${#name}  # 3

3. Extract string, ${variable name: left:right} (extract characters from left to right)

name="hello zhy!"
echo ${name:6:9}  # zhy

shell variable definition specification

1. Variable names can consist of letters, numbers and underscores, but cannot start with numbers. 5A= 200(x)
2. There shall be no spaces on both sides of the equal sign
3. Variable names are usually capitalized, which is a standard that we can abide by

shell comments

  • Single-Line Comments

    # I'm a single line note
    
    
  • multiline comment

    :<<!
    I am
     One
     multiline comment 
    !
    
    

Default variable - file parameter variable

First acquaintance

When we execute a shell script, if we want to get the parameter information of the command line, we can use the location parameter variable
For example,. / myshell.sh 100 200, this is a command line that executes the shell, and the parameters can be obtained in the myshell script
information

Basic grammar

When executing a shell script, you can pass parameters to the script$ 1 is the first parameter, $2 is the second parameter, and so on. In particular, $0 is the file name (including the path).

parameterexplain
$nn is a number. In particular, $0 is the file name (including the path), $1 ~ $9 represents the first parameter to the ninth parameter. Parameters above ten need to be contained in braces, such as
$#Represents the number of parameters passed in from the file
$*Represents all parameters in the command line, $* regards all parameters as a whole
$@Represents all parameters in the command line, $@ treats all parameters differently as separate parameters
$$ID of the process the script is currently running
$?Exit status of the previous command. 0 indicates normal exit, and other values indicate error
$(command)Return the stdout of the command
commandReturn stdout of command, the same as $(command)

$n, n is a number. In particular, $0 is the file name (including the path), $1 ~ $9 represents the first parameter to the ninth parameter. Parameters above ten need to be contained in braces, such as

$*, representing all parameters in the command line, $* regards all parameters as a whole

$@, representing all parameters in the command line, $@ treats all parameters differently as separate parameters

$#, indicating the number of parameters in the command line

case

Write a shell script pos.sh to obtain various parameters on the command line in the script

vim pos.sh

#!/bin/bash
echo "file name:"$0
echo "First parameter:"$1
echo "Second parameter:"$2
echo "All parameters:"$*
echo "All parameters:"$@
echo "Number of parameters:"$#

sh pos.sh 1 2 3

:<<!
Output content
 file name:pos.sh
 First parameter:1
 Second parameter:2
 All parameters:1 2 3
 All parameters:1 2 3
 Number of parameters:3
!

array

First acquaintance

Arrays in the shell can store multiple different types of values, but only one-dimensional arrays are supported. There is no need to specify the size of the array during initialization. And like other languages, the subscript of the array starts from 0.

Basic grammar

1. Definition:

There are two ways:

1.1. Arrays can be directly represented by parentheses, and elements are separated by spaces. For example:

array=(123 abc "zhy")

1.2. You can also directly define the value of an element in the array without continuous reference space.

array[0]=123
array[1]=abc
array[2]="zhy"
array[666]='ALG' # A value of 'ALG' can be defined directly where the subscript is 666

2. Read the value of an element in the array:

Syntax:

${array[index]}

For example:

array=(123 abc "zhy")
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}

3. Read the entire array

Syntax:

${array[@]} # The first way
${array[*]} # The second way

For example:

array=(123 abc "zhy")

echo ${array[@]} # The first way
echo ${array[*]} # The second way

4. Get the length of the array

Syntax:

${#array[@]} # The first way to write
${#array[*]} # The second way to write

For example:

array=(123 abc "zhy")

echo ${#array[@]} # The first way to write
echo ${#array[*]} # The second way to write

operator

First acquaintance

Learn to calculate variables in the shell, you need operators

Basic grammar

There are three ways:

Method 1: $((expression))

Note that the outside () cannot be omitted

echo $((1 + 2) + 3)

Mode 2: $[expression]

This method is relatively simple and recommended

echo $[(1 + 2) + 3]

Mode 3: expr expression (note that the expression here is different from that above)

expr command

First acquaintance

The expr command asks the user to find the value of the expression. The syntax is as follows:

expr expression

Precautions for expressions:

1 space is required between expr operators, and the calculated value must be returned with ` (backquote)

2. Place a backslash in front of a shell specific character

3. Strings containing spaces and other special characters should be enclosed in quotation marks

4.expr will output the results in stdout. If it is a logical relational expression, the result is true, stdout is 1, otherwise it is 0.

5. Exit code of expr: if it is a logical relationship expression, the result is true, the exit code is 0, otherwise it is 1.

6. Operators \ * (multiply, pay attention to one \, \ (divide), + (add), - (subtract)

String expression

1.length STRING

You can return the length of a string

str="hello world"
echo `expr length "$str"`

2.index STRING CHAR

The first character position of any single character in CHAR in the STRING, and the subscript starts from 1. If there is no character in CHAR in STRING, 0 is returned.

str="hello world"
echo `expr index "$str" l` # l the first position that appears is 3

3.substr STRING POSITION LENGHT

Returns the substring of a STRING with a maximum LENGTH of LENGTH starting from POSITION (subscript starts from 1). If POSITION or LRNGHT is negative, 0 or non numeric, an empty STRING is returned.

str="hello world"
echo `expr substr "%str" 1 5` # The subscript starts with 1 and outputs hello

Integer expression

expr supports ordinary arithmetic operations. Arithmetic expressions have lower priority than string expressions and higher priority than logical relationship expressions.

1. + - addition and subtraction. The parameters at both ends will be converted to integers. If the conversion fails, an error will be reported.

2. \ * /% multiplication, division and modulo operation. The parameters at both ends will be converted to integers. If the conversion fails, an error will be reported.

3. \ (\) the priority of this table can be, but it needs to be escaped with a backslash

For example:

a=1
b=2

echo `expr $a + $b`
echo `expr $a - $b`
echo `expr $a \* $b`   # *Need escape
echo `expr $a / $b`
echo `expr $a % $b`
echo `expr \( $a + $b \) \* 3` # () need escape, calculate (a+b)*3

Logical relation expression

1. | if the first parameter is not empty and non-0, the value of the first parameter is returned; otherwise, the value of the second parameter is returned (short circuit principle). However, the value of the second parameter is also required to be non null or non-0, otherwise 0 is returned. If the first parameter is non null or non-zero, the second parameter is not evaluated.

2. & if both parameters are non empty and non-0, the first parameter is returned; otherwise, 0 is returned. If the first parameter is 0 or empty, the second parameter is not evaluated.

3.< <= = == != >= > Compare the parameters at both ends. If it is true, it returns 1, otherwise it returns 0. "=" Is a synonym for "=" expr "first try to convert the parameters at both ends into integers and make arithmetic comparison. If the conversion fails, make character comparison according to the sorting rules of the character set( (it needs to be escaped or separated by single quotation marks)

4. () the priority of this table can be, but it needs to be escaped with a backslash

a=1
b=2

echo `expr $a \> $b`   #0
echo `expr $a '<' $b`  #1
echo `expr $a \>\= $b` #0
echo `expr $a '<=' $b` #1

c=0
d=3

echo `expr $c \& $d` # 0
echo `expr $d \& $a` # 3
echo `expr $c \| $d` # 3
echo `expr $d \| $a` # 3

case

1. Calculate (1 + 2) * 3 in three ways

#!/bin/bash
# Mode 1
ANS1=$(((1 + 2) * 3))
echo "ans1=$ANS1"

# Mode II
ANS2=$[(1 + 2) * 3]
echo "ans2=$ANS2"

# Mode III
TEMP=`expr \( 1 + 2 \) \* 3`
echo "TEMP=$TEMP"

2. Find the sum of the two parameters in the command line

#!/bin/bash
ANS=$[$1 + $2]
echo ANS

read command

The basic grammar is introduction

The read command is used to read single line data from standard input. When the end of the file is read, the exit code is 1, otherwise it is 0.

When read encounters the file terminator, it will end automatically. In the while loop, you can use ctrl + d to represent the file terminator.

read name
echo $name

Optional parameters:

-p: Prompt information can be followed

-t: Followed by the number of seconds, which defines the waiting time of the input character. After the waiting time is exceeded, this command will be ignored automatically

case

Define a variable name input, set the prompt statement and the maximum waiting time, and finally output it on the terminal.

read -p "Please Input into your name:" -t 30 name 
echo "hello ${name}!"

echo command

Common usage

echo is used to output strings. The syntax is as follows:

echo STRING

1. Display string

Just echo STIRNG directly. You can use double quotation marks or no quotation marks. Note: do not use quotation marks

echo "hello world"
echo hello world

2. Show escape string

Escape characters can be output in double quotation marks or without quotation marks. However, if you want to use more escape characters such as \ n \t, you need to use the echo-e option.

echo " \"hello world\" " # Output "hello world"“
echo \"hello world\" # Output "hello wordl"

3. Display variables

Define variables, and then use variables

name=zhy
echo "hello $name"

4. Show wrap

Use echo -e to output the escape character of \ n, and you must use double quotation marks to use it.

echo -e "hello \n world"

5. Show no line breaks

Use the echo -e output \ c option, and you must use double quotes to use it.

echo -e "hello\c "
echo "world"
# Output hello world

6. Show results redirection

You can use the > or > > method for string input redirection and append redirection

echo "hello world" > hello.txt # Overwrite the contents of the string into the hello.txt file
echo "Hello World" >> hello.txt # Add the contents of the string after the hello.txt file
# Finally, you get a file hello.txt
# The content of the file is hello world Hello World

7. Output the string as it is without escaping or fetching variables

As mentioned above, you can use double quotes or unquoted strings or escape strings. If you use single quotes, you can output the string itself.

name=zhy
echo '$name \n' # Output '$name' \ n '

8. Displays the execution result of the command

Use ` ` or $() to get the result of the command

echo `date` # Output date

printf command

Similar to the printf function in c language, it is also used to format the output, but there is no need to add parentheses.

Basic grammar

printf format [arguments...]

printf "%d\n" 123 # Output 123
printf "%s\n" zhy # Output zhy
printf "%d * %d = %d\n" 2 3 `expr 2 \* 3` # Output 2 * 3 = 6

test command and judgment symbol []

Logical operators & & and||

&&Means and, | means or

Both have short circuit principle:

  • Expr1 & & expr2: when expr1 is false, expr2 is ignored directly. Expr2 is executed only when expr1 is true

  • Expr1 | expr2: when expr1 is true, expr2 is ignored directly. Expr1 is executed only when expr1 is false

The exit code of the expression is 0, indicating true; Non zero indicates false.

Note: exit code is different from stdout. It usually ends with return 0 in the main function in C++/C, which indicates that the process ends normally. If the process does not end properly, it ends with a non-zero.

Supplement: you can use & & and | to judge the condition of if else

test -e test.txt && echo "exist" || echo "no exist"

test -e test.txt is a judgment made by using test. It will be introduced later. In fact, it is to judge whether the file test.txt exists. If the file exists, return 0 to indicate true, so echo "exist" will be executed; If the file does not exist, the return of 1 means false, so echo "exist" will not be executed. Then, because the previous is false, echo "noexist" will be executed finally. This is to use test + & & 𞓜 to use a judgment similar to if else.

test command

The test command is used to determine the file type and compare variables.

The test command returns the result with exit code instead of stdout. 0 means true and non-0 means false.

File type judgment

Use the test command to determine the type of file

Format:

test option filename

optionexplain
-eDoes the file exist
-fIs it a file
-dIs it a directory

File permission judgment

Determine the permissions of a file or directory.

Format:

test option filename

optionexplain
-rIs it readable
-wIs it writable
-xExecutable
-sIs it a non empty file

Comparison and judgment of integers

Use the test command to judge the relationship between two integers

Format:

test $a Comparator $b

Comparatorexplain
-eqIs a equal to b
-neIs a not equal to b
-ltIs a less than b
-gtIs a greater than b
-leIs a less than or equal to b
-geIs a greater than or equal to b

Comparison and judgment of strings

Use the test command to judge the comparison between two strings.

parameterexplain
test -z strDetermine whether str is empty
test -n strDetermine whether str is not empty
test str1 == str2Determine whether str1 and str2 are equal
test str1 != str2Determine whether str1 and str2 are not equal
test str1 \> str2Determine whether str1 > STR2
test str1 \>\= str2Determine whether str1 > = STR2

Multiple conditional judgment

Use the test command to judge the true and false of the two conditions at the same time, which is similar to & & in c/c + +||

Format:

test Condition 1 option condition 2

optionexplain
-aJudge whether the two conditions are true at the same time
-oJudge whether at least one of the two conditions is true
!Reverse

[] judgment command

[] is a command, which is similar to the test command. It is more often used in if statements. In addition, [[]] is an enhanced version of [], which supports more features.

For example:

[ 2 -t 3]
echo $? # Output the exit status of the previous process. 0 is output here

be careful:

  • Each item in [] should be separated by a space because [] is a command
  • Variables in brackets are best enclosed in double quotation marks
  • Constants in brackets are best enclosed in single or double quotation marks

Judgment statement

if judgment

Single branch if

Syntax format:

if condition
then 
	sentence
fi

For example:

if [ 1 -lt 2] && [ 3 -gt 2]
then 
	echo 1 minimum
fi
# Output 1 min

Single branch layer if else

Syntax format:

if condition
then
	sentence
else
	sentence
fi

For example:

a=10
b=20

if [ "$a" -ge "$b" ] || [ "$b" -le "$a" ]
then
	echo "$a"greater than"$b"
else
	echo "$a"less than"$b"
fi

Multi branch judgment if elif else

Syntax format:

if condition
then
	sentence
elif
then
	sentence
else
	sentence
fi

For example:

a=10

if [ $a -eq 1 ]
then
	echo $a==1
elif [ $a -eq 2 ]
then
	echo $a==2
else
	echo $a unknown
fi

case judgment

Syntax format:

case $Variable name in
	...)
		sentence
		;;
	...)
		sentence
		;;
	*) # Indicates default
		sentence
		;;
esac

for example

a=1
case $a in
	1)
		echo "$a == 1"
		;;
	2)
		echo "$a == 2"
		;;
	*)
		echo "$a == $a"
		;;
esac

Circular statement

There are three kinds of loop statements, four of which are written in Chinese

for ... in ... do ... done

Assign the variables after in to the variables before in one by one, similar to the for loop in python

for var in var1 var2 var3
do
	echo $i
done

For example:

Example 1:

for i in 1 a &
do
	echo $i
done
# Output 1 A&

Example 2:

You can also output the return value of the command in `. Output all file names under the current path, one line for each file name:

for i in `ls`
do
	echo $i
done

Example 3:

You can use the seq command to output a range of numbers.

for i in `seq 1 10`
do 
	echo $i
done

Example 3:

In text, you can use syntax output such as {1.. 10} instead of seq, and support letters.

Output a~z

for i in {a..z}
do
	echo $i
done

for ((..., ... , ...)) do ... done

There is another way to write the for loop

for ((expression; condition; expression))
do
    sentence
done

For example:

for ((i = 0; i <= 10; i ++))
do 
	echo $i
done

while ... do ... done

Syntax format:

while condition
do
    sentence
done

For example:

Always enter name until the end of the file is encountered, ctrl + d

while read name
do
	echo $name
done

untill... do ... done

The until loop is opposite to the while loop. If the condition is false, it continues, and if it is true, it stops.

Syntax format:

until condition
do
	sentence
done

For example:

The input can be stopped only when you enter yes

until [ "$word" == yes ]
do
	read -p "please input yes:" word
done

break

Jump out of the current layer of loop. Note that unlike C/C + +, break cannot jump out of the case statement.

For example:

Output 1 ~ 10, but stop at 5

for ((i = 1; i <= 10; i ++))
do 
	if [ $i -eq 5 ]
	then 
		break
	fi
	echo $i
done

continue

Jump out of the current cycle.

For example:

Enter an odd number between 1 and 10

for ((i = 1; i <= 10; i ++)) 
do 
	if [ $[$i % 2] == 0 ]
	then 
		continue
	fi
	echo $i
done

Deal with dead cycle

1. You can use the top command to find the process number PID of the dead loop

2. Then use kill -9 PID to forcibly end the process

function

Basic grammar

The functions in bash are similar to those in C/C + +, but the return value of return is different from that in C/C + +. It returns exit code, with a value of 0-255. 0 indicates the normal end.

If you want to get the output result of the function, you can output it to stdout through echo, and then through $(function)_ Name) to get the results in stdout.

The return value of the function can be through $? To get it.

Syntax format:

func_name () {
	sentence
	...
}

For example:

func() {
	name=zhy
	echo $name
}
func # Instead of adding (), the calling function writes the function name directly like a command

Get the return value and stdout value

If return is not written, the default return is 0.

For example:

func() {
	name=zhy
	echo "hello $name"
	return 100
}

output=$(func) # Use $() to return stdout of nc function
returnVal=$?   # Use $? Returns the return value of func

echo "output = $ouput"
echo "returnVal = $returnVal"

Input parameters to function

Within the function, $1 represents the first input parameter, $2 represents the second input parameter, and so on.

Note: the $0 in the function is still the file name, not the function name.

For example:

func() {
	for ((i = 0; i <= $1; i ++))
	do
		echo $i
	done
}

echo $(func 10)

Local variables within functions

Local variables can be defined within the function, and the scope of action is only within the current function.

You can define local variables in recursive functions.

Syntax format:

local Variable name=Variable value

exit command

The exit command is used to exit the current shell process and return an exit status; Use $? This exit status can be received.

The exit command can accept an integer value as a parameter to represent the exit state. If not specified, the default status value is 0.

Exit exit status can only be an integer between 0 and 255, where only 0 indicates success and other values indicate failure.

if [ $# -eq 1 ]
then 
	eixt 0
else
	exit 1
fi
# If you pass in a parameter, you will return 0 to exit. If the number of parameters passed in is not 1, you will return 1 to exit

Folder Redirection

Each process opens 3 file descriptors by default:

  • stdin standard input, read data from the command line, and the file descriptor is 0

  • stdout standard output, output data to the command line, and the file descriptor is 1

  • stderr standard error output, output data to the command line, and the file descriptor is 2

You can use file redirection to redirect these three files to other files.

commandexplain
command > fileRedirect stdout to file
command < fileRedirect stdin to file
command >> fileRedirect stdout to file as an append

For example:

echo "hello world" > tmp.txt
read str < tmp.txt
echo $str

Introducing external scripts

Similar to the include operation in C/C + +, bash can also introduce code from other files.

Syntax format:

. filename
 perhaps
source filename

For example:

Create a test1.sh

#! /bin/bash
str=hello world

Create another test2.sh

#! /bin/bash
source test1.sh
echo $str # Output hello world

Tags: Linux vim

Posted on Sun, 05 Sep 2021 02:43:51 -0400 by dungareez