A must-see for detailed explanation of shell programming specification variables! ! ! (Pipe symbol and redirection, shell variable type)

Preface

  Shell is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language.
  The execution of Linux commands must rely on the Shell command interpreter. Shell is actually a special program running in the Linux operating system. It is located between the operating system kernel and the user. It is responsible for receiving and interpreting commands entered by the user, and passing the operations that need to be performed to the system kernel for execution. Acting as a "translator" between the kernel and the core. When the user logs in to the Linux system, a Shell program is automatically loaded to provide the user with an operating system that can enter commands.

One: Shell script overview

1.1: The concept of shell scripts

  • Save the commands to be executed to a text file in order
  • Give the file executable permission for easy operation + an x ​​execution permission
  • Can be combined with various shell control statements to complete more complex operations

1.2: Shell script application scenarios

  • Repetitive operation
  • Batch transaction processing
  • Automated operation and maintenance management
  • Server operation status monitoring
  • Scheduled task execution
  • ……
    The role of the shell-----Command interpreter, "translator"Insert picture description here

1.3: The user's login shell

  • Shell program used by default after login, generally /bin/bash
  • The internal commands and operating environment of different shells will be different
[root@server1 ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

1.31: type command-display the specified command type

[root@shuai opt]# type cd
cd 是 shell 内嵌
[root@shuai opt]# type mkdir
mkdir 是 /usr/bin/mkdir
[root@shuai opt]# type bash
bash 是 /usr/bin/bash
[root@shuai opt]# type which
which 是 `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' 的别名

1.4: Writing the first shell script

1.41: Writing script code

  • Use vi text editor
  • One Linux command per line, written in order of execution
  • The script suffix is ​​.sh
vim cendy.sh
#!/bin/bash           #shell脚本固定格式
cd /boot              #切换到boot目录
pwd                   #查看当前所在位置
ls -lh vml*           #看vml开头所有文件
:wq                   #ESC切换到命令模式  保存退出
[root@server1 test]# ls -lh
总用量 4.0K
-rwxr-xr-x. 1 root root 38 9月  18 14:10 cendy.sh

1.4.2 Grant executable permissions

[root@server1 test]# chmod +x cendy.sh

1.5: Execute script file

  • Method 1: Script file path (absolute path and relative path)
[root@server1 test]# ls -lh
总用量 8.0K
-rwxr-xr-x. 1 root root 38 9月  18 14:10 cendy.sh
-rw-r--r--. 1 root root 25 9月  18 14:25 jerry         #无执行权限
[root@server1 test]# ./jerry.sh
-bash: ./jerry.sh: 权限不够 
[root@server1 test]# ./cendy.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 8月  26 17:42 vmlinuz-0-rescue-34c5caefc7194359afe5202e8cdfd9fe
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64
  • Method 2: sh script file path
[root@server1 test]# sh jerry.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 8月  26 17:42 vmlinuz-0-rescue-34c5caefc7194359afe5202e8cdfd9fe
-rwxr-xr-x. 1 root root 5.7M 8月  23 2017 vmlinuz-3.10.0-693.el7.x86_64

Two: redirection and pipeline operation

2.1: Interactive hardware device 0 1 2

  • Standard input: accept user input data from the device
  • Standard output: output data to the user through the device
  • Standard error: report execution error information through this device
Types of Device file File Description Default device
Standard input /dev/stdin 0 keyboard
Standard output /dev/stdout 1 monitor
Standard error output /dev/stder 2 monitor

2.2: Redirection operation

Types of Operator use
Redirect input < Read data from the specified file instead of inputting from the keyboard
Redirect output > Save the output result to the specified file (overwrite the original content)
Redirect output >> Append the output result to the specified file
Standard error output 2 > Save the error information to the specified file (overwrite the original content)
Standard error output 2>> Append the error information to the specified file
Mixed output & > Save the contents of standard output and standard error to the same file

2.3: Pipeline operation symbol "|"

  • Output the result of the command on the left as the processing target of the command on the right
[root@server1 test]# grep "bash$" /etc/passwd        #查看以bash结尾的登录账号
root:x:0:0:root:/root:/bin/bash
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
qy:x:1000:1000:qyf:/home/qyf:/bin/bash
#以冒号分隔,输出第1,7个字段    ##如果不写-F 就是指标符号跟空格
[root@server1 test]# grep "bash$" /etc/passwd | awk -F: '{print $1.$7}'
root/bin/bash
amandabackup/bin/bash
qyf/bin/bash

Awk is a type of regular expression

大部分情况下  
grep:过滤关键字  grep egrep 
sed 老二 按行读取
awk 老三 按列读取数据
sed: 按行读取
awk:按列读取数据
$1,$7:位置变量
[root@shuai opt]# df -Th
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        50G  5.0G   46G   10% /
devtmpfs                devtmpfs  3.6G     0  3.6G    0% /dev
tmpfs                   tmpfs     3.6G     0  3.6G    0% /dev/shm
tmpfs                   tmpfs     3.6G   13M  3.6G    1% /run
tmpfs                   tmpfs     3.6G     0  3.6G    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  179M  836M   18% /boot
/dev/mapper/centos-home xfs       242G   33M  242G    1% /home
tmpfs                   tmpfs     726M  4.0K  726M    1% /run/user/42
tmpfs                   tmpfs     726M   48K  726M    1% /run/user/0
/dev/sr0                iso9660   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64

####提取第一个跟第六个
[root@shuai opt]# df -Th | awk '{print $1,$6}'
文件系统 已用%
/dev/mapper/centos-root 10%
devtmpfs 0%
tmpfs 0%
tmpfs 1%
tmpfs 0%
/dev/sda1 18%
/dev/mapper/centos-home 1%
tmpfs 1%
tmpfs 1%
/dev/sr0 100%
##过滤以/结尾 第一列跟第六列
[root@shuai opt]# df -Th | grep "/$" | awk '{print $1,$6}'
/dev/mapper/centos-root 10%

Three: Detailed explanation of shell script variables

3.1: The role and type of variables: Variables are the most common type of programming

A way to temporarily access data in memory

3.11: The role of variables

To provide specific parameters for flexible management of the limux system, it has two meanings.
Variable name: use a fixed name, preset by the system or defined by the user.
Variable value: can change according to user settings and changes in the system environment

3.12: Types of variables

Custom variables: defined, modified and used by the user.
Environment variables: maintained by the system and used to set the working environment.
Location variables: pass parameters to the script program through the command line.
Predefined variables: a type of variable built in Bash that cannot be modified directly

3.2: Custom variables

3.21: Define a new variable and view the value of the variable

Define a new variable-custom variable = echo
variable name starts with subtitles or underscore, case sensitive, all uppercase is recommended.
Variable format variable name = variable value
View variable value
echo $ variable name

3.22: Variable naming rules

命名只能使用英文字母,数字和下划线,首个字符不能用数字开头’
中间不能有空格,可以用下挂线—
不能使用标点符号
不能使用bash的关键字【可用help命令查看保留关键字】

VFDA=2
age=90 整型##here= is called assignment symbol
name='baisn' string
score=88.8 [single precision floating point 4 bytes] shuai=8.88 [double precision floating point 8 bytes]

[root@server1 test]# dd=20
[root@server1 test]# $dd=20
bash: 20=20: 未找到命令...
[root@server1 test]# echo $dd 20
20 20
[root@server1 test]# echo $dd 
20
[root@server1 test]# name=dd
[root@server1 test]# echo $name
cc
[root@server1 test]# echo $name dd
cc dd

3.23: Cancel the variable unset

[root@server1 test]# name=aa
[root@server1 test]# echo $name 
aa
[root@server1 test]# unset name    #取消变量
[root@server1 test]# echo $name 

#输出为空

3.24: Use quotation marks when assigning values

  • Double quotation marks
    allow other variable values ​​to be referenced by symbols, that is, in the case of representing a string, if there is a symbol to reference other variable values, that is, in the case of representing a string, if there is a symbol to reference other variable values, that is, it can represent In the case of a string, it can also represent a scalar if it exists
  • apostrophe

It is forbidden to quote other variable values. It is a normal character, which is regarded as a string, and variable symbols are not recognized. Backtick: command substitution, extract the output result after the command is executed. Variable=backtick is equivalent to variable=()

[root@server1 test]# siri=10
[root@server1 test]# echo $siri 
10
[root@server1 test]# siri2="50 $siri"
[root@server1 test]# echo $siri2
50 10
[root@server1 test]# siri=`ps aux | wc -l`
[root@server1 test]# echo $siri
204
[root@server1 test]# siri='ps aux | wc -l'
[root@server1 test]# echo $siri
ps aux | wc -l

3.25: Input content from the keyboard to assign read

[root@server1 test]# vim weather.txt
#!/bin/bash
read -p "请输入今天的天气:" weather
echo "今天天气是: $weather"
#命令模式  :wq 保存退出
[root@server1 test]# chmod +x weather.txt 
[root@server1 test]# ./weather.txt 

3.26: Set the scope of the variable export settings are globally available

  1. Format 1: export variable name
  2. Format 2: export variable name=variable value
  3. The two formats can be mixed.
    Export can define the variable as a global variable. In this case, the variable can be used whether it is switching the bash environment or switching users.
[root@server1 test]# tom=shu
[root@server1 test]# echo $tom 
shu
[root@server1 test]# bash           #设置bash环境
[root@server1 test]# echo $tom
#输出为空
[root@server1 test]# exit           #退出bash环境
exit
[root@server1 test]# echo $tom 
shu
[root@server1 test]# export tom     #设置为全局变量
[root@server1 test]# bash
[root@server1 test]# echo $tom      #引用全局变量
shu

3.27: Operation expr of integer variables

expr variable 1 operator variable 2 [operator variable 3].. . . . .
Common operators

  1. Addition operation: +
  2. Subtraction operation:-
  3. Multiplication operation: * Multiplication must add \, because in the shell language, * represents a wildcard symbol
  4. Division operation: /
  5. Modulus (remainder) operation:%
[root@server1 test]# expr 1 + 2
3
[root@server1 test]# expr 5 -  2
3
[root@server1 test]# expr 5 \*  2
10
[root@server1 test]# expr 2 / 2
1
[root@server1 test]# expr 5 %  2
1
[root@server1 test]# vim test1.sh
#!/bin/bash
read -p "第一天学习了几个小时:" time1
read -p "第二天学习了几个小时:" time2
time=`expr $time1 + $time2`
echo "两天学习时间总和为$time " 
[root@server1 test]# chmod +x test1.sh 
[root@server1 test]# ./test1.sh 
第一天学习了几个小时:12
第二天学习了几个小时:12
两天学习时间总和为24  

Four: Special shell variables

4.1 Environment variables

  • Created in advance by the system to set up the user’s working environment
  • Configuration files: /etc/profile, ~/.bash_prolile

4.2 Common environment variables

  • PWD\、PATH
  • USER、SHELL、HOME
[root@server1 test]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server1 test]# echo $PWD 
/root/test
[root@server1 test]# vim test2.sh
[root@server1 test]# sh test2.sh 40 20
第一个值 40
第二个值 20
减运算结果为20

4.3: Predefined variables

  1. $#: The number of position variables in the command line
  2. $*: the contents of all location variables
  3. $?: The status returned after the last command is executed. When the return status value is 0, it means normal, and a non-zero value means abnormal execution or error.
  4. $0: currently executing process/program name
  5. Analysis script
[root@server1 test]# vim test3.sh
#!/bin/bash
t=b-`date +%s`.tgz                  #设置变量名称,+%s表示从1970年至今的秒数
tar zcvf $t $* &> /dev/null       #压缩到 /dev/null    
echo "已执行$0个脚本"
echo "共完成$#个对象的备份"
echo "具体内容包括:$*"
[root@server1 test]# chmod +x test3.sh 
[root@server1 test]# ./test3.sh /etc/passwd /etc/shadow
已执行./test3.sh个脚本
共完成2个对象的备份
具体内容包括:/etc/passwd /etc/shadow
#/dev/null:黑洞
%Y表示年
%m表示月
%d表示日
%H表示小时
%M表示分钟
%S表示秒
%s表示从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数,相当于time函数
%w表示一周中的第几天。

[root@server1 test]# date
2020年 09月 20日 星期日 22:04:22 CST
[root@server1 test]# date +%Y%D$%H:%M
202009/20/20$22:03
[root@server1 test]# date +%s      #从1970年1月1日到现在的秒数
1600610688

Guess you like

Origin blog.csdn.net/qyf158236/article/details/108663831