Getting started with linux shell scripting

1. Introduction to SHELL

1.1 What is a shell

  • shell script

1. Save the commands to be executed to a text file in order

2. Give the file executable permissions to facilitate a one-time execution of a program file

3. Various shell control statements can be combined to complete more complex operations

1.2 Shell script application scenarios

1. Repetitive operation

2. Interactive tasks

3. Batch transaction processing

4. Service running status monitoring

5. Timing task execution

1.3 Composition of shell script

1. Script declaration (interpreter): If the first line is "#!/bin/bash", it means that the code statement below this line is interpreted and executed by the /bin/bash program.

2. Comment information: Statements starting with "#" are indicated as comment information, and the commented statements will not be executed when running the script.

3. Executable statement: such as the echo command, used to output the string between " "


1.4 Execution mode of shell script

1. ./script file path (you must first give x permission to directly chmod 777 script path)

2. Source script file path

3. Bash script file path

4. sh script file path

1.5 The role of the shell -------- command interpreter ("translator")

         A special application program, between the operating system kernel and the user, acts as a "command interpreter", responsible for receiving and interpreting the operation instructions (commands) input by the user, passing the operations that need to be performed to the kernel for execution, and Output the execution result.

The first shell script hello world

# 创建一个Helloword.sh 文件
[root@localhost opt]# touch Helloword.sh

# 编辑Helloword.sh 文件
[root@localhost opt]# vim Helloword.sh
[root@localhost opt]# cat Helloword.sh 
#!/bin/bash
# This is ower first shell
echo "hello world"
[root@localhost opt]# ll Helloword.sh 
-rw-r--r-- 1 root root 85 Sep 20 22:26 Helloword.sh

# 赋予执行权限
[root@localhost opt]# chmod +x Helloword.sh 

# 运行helloword.sh 脚本
[root@localhost opt]# ./Helloword.sh 
hello world

2. Pipeline and Redirection

redirect

  • Standard input: Receive user-entered data from this device
  • Standard output: output data to the user through this device
  • Standard error: report execution error information through this device
Redirect input 
 
< read data from the specified file
redirect output 
 
> Save the standard output result to the specified file and overwrite the original content
redirect output 
 
>> Append the standard output result to the end of the specified file without overwriting the original content
standard error output   
 
2> Save the error message to the specified file and overwrite the original content
standard error output    
 
2>> Append the error message to the end of the specified file without overwriting the original content
mixed output 
 
&> Save standard output, standard error to the same file
mixed output 2>&1 redirect standard error output to standard output,

The pipe operator "|"

The output result of the command on the left side of the pipe symbol "|" is used as the input (processing object) of the command on the right side, and multiple pipes can be used in the same command line.

3. Script variables

3.1 Custom variables

  • format variable name=variable value
  • View variable value echo $variable name

Variable name naming convention

  1. The built-in commands of linux are not applicable as variable names

  2. Do not use special characters as variables (eg:$,@, can start with "_")

  3. Generally not applicable in Chinese

  4. Define variable names by specifying English names

  5. case sensitive

Using quotes " " when assigning values
​​allows other variable values ​​to be referenced via $

' 'It is forbidden to quote other variable values, $ is regarded as an ordinary character

` ` (backtick) Extract the output after the command is executed (execute the command first and then assign the value)

weak and strong references

"$name" Weak reference, where the variable reference will be replaced by the variable value

'$name' strong reference, the variable reference will not be replaced by the variable value, but keep the original string

The read command gets the input

1.read -p "prompt information" variable name

echo $ variable name

2.echo -n "prompt information"

read variable name

echo $ variable name

3. read -a array input

read -a array name

Operations on integer variables
expr variable 1 operator variable 2 [operator variable 3] ...

Operations on non-integer variables

scale is a special variable used to specify the precision of decimals. It is commonly used for mathematical calculations in the Bash shell, such as calculating floating point numbers or percentages, etc.

Commonly used operators
●Addition: +
●Subtraction:-
●Multiplication: *
●Division: /
●Modulo (remainder) operation: %

i++ i=$(($i+1)) Assign value first, then add i=1 i=1+1 i=2+1 (add 1 each time)

i-- assign first and then subtract (decrease by 1 each time)

i+=2 Assign value first, then add (add 2 each time)

++i first add and then assign (add 1 each time)

--i first subtract and then assign (decrease by 1 each time)

[root@localhost ~]# expr 1 + 1
2
[root@localhost ~]# expr 1 \* 3
3
[root@localhost ~]# expr 4 / 3    除法只取整
1
[root@localhost ~]# expr 4 % 3    余数为1
1
使用echo算数
[root@localhost ~]# echo $[5+5]
10
[root@localhost ~]# echo $((1+2))
3
加入let混合示例
[root@localhost ~]# a=5
[root@localhost ~]# let a++
[root@localhost ~]# echo $a
6
[root@localhost ~]# let a--
[root@localhost ~]# echo $a
5

[root@localhost ~]# echo $a
5
[root@localhost ~]# echo $[a++]
5
[root@localhost ~]# echo $a
6
[root@localhost ~]# echo $[++a]
7
[root@localhost ~]# echo $a
7
bc计算小数
[root@localhost ~]# echo "1.1+1.2" | bc
2.3
[root@localhost ~]# echo "scale=3; 10/3" | bc   取小数点后三位
3.333

[root@localhost ~]# echo $[3**8]      计算3的8次方
6561
[root@localhost ~]# echo $[3^8] | bc     计算3的8次方
6561
示例求半径为2的圆的面积,去小数点后两位
[root@localhost ~]# pai=3.14
[root@localhost ~]# r=2
[root@localhost ~]# echo "scale=2;$pai*$r^2" | bc
12.56

3.2 Predefined variables

$*, $@: Indicates the parameters to be processed by the command or script.

"$*": Return all parameters as a whole string (single string) separated by spaces, representing "$1 $2 $3 $4".

"$@": Add double quotes to separate each parameter into a parameter list of n parts, each parameter is returned as a string, representing "$1" "$2" "$3" "$4".

$0: Indicates the name of the currently executing script or command.

$#: Indicates the number of parameters to be processed by the command or script.

$?: Indicates the return status code after the execution of the previous command or script. The return value of 0 means that the execution is correct, and any return value other than 0 means that the execution is abnormal. It is also often used in shell scripts to return the exit function and return the exit value.

$* 

 $@

 $* $@

 $#

 $?

 3.3 Position variables

When executing command line operations, the first field represents the command name or script program name, and the rest of the string parameters are assigned to positional variables in sequence from left to right.

$n: n is a number, $0 represents the command itself, $1-$9 represents one to the ninth parameter, and more than ten parameters need to be expressed in braces, for example, the tenth parameter is ${10}

3.4 Environment variables 

$USER represents the user name

$HOME represents the user's home directory

$LANG represents the language and character set

$PWD indicates the current working directory

$PATH represents the default path for executable user programs


 

Guess you like

Origin blog.csdn.net/weixin_42054864/article/details/131611082