Linux--Shell Programming--Specifications and Variables


Preface

  • As Linux systems are used more and more in enterprises, automated management of servers has become more and more important
  • In the automatic maintenance of Linux servers, in addition to the setting of scheduled tasks, the application of shell scripts is also a very important part
  • What is the difference between shell and shell script? To be precise, Shell is a command line interpreter, its role is to follow a certain grammar to interpret the input commands and pass them to the system
  • The shell script is a program file that puts various commands into a file in advance, which is convenient for one-time execution. It is extremely efficient.

1. Shell script programming specification

1. Concept

  • In some complex Linux maintenance tasks, a large number of repetitive input and interactive operations are not only time-consuming and labor-intensive, but also error-prone
  • And write an appropriate shell script program, which can be processed in batches and complete a series of maintenance tasks automatically , which greatly reduces the burden on the administrator
  • Save the commands to be executed to a text file in order
  • Give the file executable permissions
  • Can be combined with various Shell control statements to complete more complex operations

2. Application scenarios

  • To summarize the above: Shell script is to save the commands to be executed to a text file in order, and give the file executable permissions to facilitate one-time execution of a program file , which is convenient for administrators to set or manage, and can be combined with various Shell control statements to complete more complex operations
  • Common scenarios are as follows:
    • Repetitive operation
    • Interactive task
    • Bulk transaction processing
    • Service running status monitoring
    • Scheduled task execution

3. Role

  • Command interpreter, "translator"
  • Between the system kernel and the user, responsible for interpreting the command line
  • Receive and explain the operation instructions input by the user, and then pass the explanation that needs to be executed to the kernel for execution and output the result
  • The shell program is used by default after login, generally /bin/bash
  • The internal commands and operating environment of different Shells will be different
[root@localhost ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

4. Start writing your first shell script

  • Write script code
    • Use vim
    • Each line of Linux command is written in order
    • Give executable permissions to the finished shell script file to make the script executable. The
      mark
      above editing content includes three commands to realize the view of the files starting with vml in the /boot directory, including..., to realize the "semi-automatic process"
      ".sh "The role of "is a kind of identification, which is convenient for you and others to identify this is a shell script file
    • Empowerment:
      mark
      Remember, it is generally necessary to empower, add executable permissions to ensure that the script can be executed normally

5. Three ways to execute script files

Take the above script as an example

  • Method 1 : Specify the path command, the file must have x permission. The
    script file path ( absolute path and relative path , and must be authorized)
chmod +x /root/first.sh

指定绝对路径:/root/first.sh 
指定相对路径:./first.sh

mark

  • Method 2 : Specify Shell to interpret the script, and the file does not require x permission
sh 脚本路径:sh first.sh
source 脚本路径:
. first.sh 
或
source first.sh

6. How to further improve your script

  • Add more user-friendly, friendly prompt information in it, it is easy for you to review, and others can read
    mark
    "#!/bin/bash is the default interpreter, there are other types of interpreters, such as python, etc."
    #: This symbol represents comment information and will not be executed. It is generally used to explain the role of this script, etc.
    mark
  • As you can see, there is a line of Chinese prompt followed by the output result, is it very comfortable to look at?
  • The execution method of ". Script file path" is to execute and perform the execution process without authorization, and the first two need to be authorized, and then only display the result without "executing"

7. Redirection and pipeline operation

  • Shell scripts are characterized by batch automated processing, and the operation process is located in the background, without any intervention by the user during the operation process
  • So how can you quickly find the information you want? This is often critical when troubleshooting
  • Therefore, it is very important to learn to extract and filter execution information!

7.1 Interactive hardware device

  • The Linux system uses files to describe various hardware and equipment and other resources. In the process of processing information from the operating system, users include the following three types of interactive hardware equipment
  • Standard Input : Receive user input data from the device
  • Standard output : output data to the user through the device
  • Standard error : report execution error information through the device
  • All of the above are the default use of keyboard and monitor as associated devices, interact with the operating system, complete the most basic input and output operations
  • Receive various command strings, auxiliary control and other information input by the user from the keyboard and output the results to the screen; if the command is wrong, it will also feedback error information to the screen
Types of Device file File description number Default device
Standard input /dev/stdin 0 keyboard
Standard output /dev/stdout 1 monitor
Standard error output /dev/stderr 2 monitor
  • You can change the direction of input and output content without using the default standard input and output devices (keyboard and display). This operation is called "redirection"

7.2 Redirection operation

Types of Operator use
Redirect input < Read data from the specified file instead of inputting it from the keyboard
Redirect output > Save the output result to the specified file (overwrite the original content)
/ >> Append the output result to the end of the specified file
Standard error output 2> Save the error information to the specified file (overwrite the original content)
/ 2>> Append the error message to the specified file
Mixed output &> Save the contents of standard output and standard error to the same file
/ 2>&1 Redirect standard error output to standard output
示例:
echo "123123" > pass.txt
##将123123定义到pass.txt文件中

passwd --stdin xcf123 < pass.txt
##读取pass.txt中的字符将其定义成xcf123用户的密码

##附:
ls -lh > log.txt 2>&1  
##等同于 ls -lh &> log.txt
本来ls -lh 直接输出的屏幕(等同于1指定屏幕)
执行>log后,是将标准输出结果保存到log.txt (1指向log.txt)
执行2>&1后,是将标准错误输出定义到标准输出信息中(2指向1,而1指向log.txt,因此2也指向了log.txt)

mark

  • Note: You may fail here, think about whether you have turned off the firewall and "setenfoce 0"?
    mark

7.3 Pipeline operation

  • Output the result of the command on the left as the processing target of the command on the right
  • Multiple pipes can be used in the same command line
  • In shell scripts, pipeline operations are often used to filter the key information needed
示例:
ps aux | wc -l        ###统计系统进程的总量
echo "123123" | passwd --stdin xcf1        ##将用户xcf1的密码改为123123

mark


Second, in-depth analysis of shell script variables

1. Role and type

  • Shell variables are used to store specific parameters (values) that the system and users need to use
  • Variable name: use a fixed name, preset by the system or user-defined
  • Variable value: can be changed according to the user's setting or the change of the system environment
Types of Explanation
Custom variable Defined, modified and used by the user
Environment variable Maintained by the system for setting up the working environment
Location variable Pass parameters to the script program through the command line
Predefined variables A type of variable built in Bash, cannot be modified directly

1. Custom variables

  • Custom variables are variables defined by system users themselves, which are only valid in the user's own shell environment, so they are also called local variables
  • When writing a shell script, some specific custom variables are usually set

1.1 Define new variables

  • Variable names start with a letter or underscore, and are case sensitive. All uppercase letters are recommended for easy identification
  • The basic format for defining variables is " variable name=variable value "
  • Note that there are no spaces on either side of the equal sign!
[root@localhost ~]# xcf=xuchengfei
##将右边的变量值赋予左边的变量名,这里可以理解成xcf=xuchengfei,但xuchengfei≠xcf
[root@localhost ~]# xu=1.0
##xu=1.0,但1.0≠xu
##可以理解为是单向的,只是赋值给对方

1.2 View and reference variable values

  • View the value of the variable
    Format: echo $variable name
[root@localhost ~]# echo $xcf
xuchengfei
[root@localhost ~]# echo $xu
1.0

mark

1.3 Special operations of variable assignment

  • Use quotes when assigning values
双引号:允许通过$符号弓|用其他变量值
单引号:禁止引用其他变量值,$视为普通字符
反撇号:命令替换,提取命令执行后的输出结果
'...'与$(...)作用一致

mark

1.4 Input from the keyboard to assign values ​​to variables -read

  • read command
从键盘输入内容为变量赋值
基本格式为:  
read [-p "提示信息"] 变量名
echo $变量名

mark

  • Experiment
    • Edit the script in the document to collect personal information
      mark
      mark

1.5 Set the scope of the variable

  • By default, newly defined variables are only valid in the current Shell environment, so they are called local variables. When entering a subroutine or a new sub-Shell environment, local variables can no longer be used
  • The specified variables can be exported as global variables through the internal command export, so that user-defined variables can continue to be used in all sub-Shell environments
格式1:export 变量名
格式2:export 变量名=变量值
  • Use the pstree command to view the Shell environment
    mark
  • Enter the bash command to enter the sub-Shell environment
[root@localhost ~]# bash
[root@localhost ~]# pstree

mark

  • Press the Ctrl+D key combination or enter the exit command to exit the sub-Shell environment, and test whether the set global variables take effect in the sub-environment
    mark

1.6 Operation of number system variables

格式:expr 变量1 运算符 变量2 [运算符 变量3]
Common operators Description
+ addition
- Subtraction
* multiplication
/ division
% Take the remainder
常用的运算表达式:
a=`expr 1 + 1 `
#注意空格!
echo $((10 / (1+1)))
#相当于
echo $[10 / (1+1)]
let i=12*5

相关概念补充:
i++      相当于   i=$[$i+1]
i--      相当于   i=$[$i-1]
i+=2     相当于   i=$[$i+2]

mark

  • I think $[] is the most convenient anyway
    mark

2. Special variables

2.1 Environment variables

  • Environment variables are created in advance by the system to set the user’s working environment
  • It can be used to change or set an environment variable for a long time. The
    global configuration file is /etc/profile, and the variables defined in this file apply to all users.
  • The user's own independent profile (~/.bash_profile), the variables defined by this file only apply to the current user

2.2 Commonly used variables and how to view

  • Use the env command to view the environment variables in the current working environment
  • Commonly used variables
USER   表示用户名称
HOME   表示用户的宿主目录
LANG   表示语言和字符集
PWD    表示当前所在的工作目录
PATH   表示可执行程序的默认搜索路径
echo $PATH					#查看当前搜索路径
PATH="$PATH:/root"			#将/root目录添加到搜索路径
export PATH="$PATH:/root"	#输出为全局环境变量
message.sh

mark
mark

2.3 Read-only variables

  • Used when the variable value is not allowed to be modified
readonly 命令设置只读变量
readonly PRODUCT				#设置为只读变量
echo $PRODUCT
PRODUCT=Python			        #只读变量不可以被重新赋值
unset PRODUCT					#只读变量不可以被删除,unset 命令用于删除变量,但无法删除只读变量,只能重启系统解决

mark

2.4 Location variables

  • When executing command line operations, the first field represents the name of the command or script program, and the remaining string parameters are assigned to the position variables in order from left to right
$n:n为数字,$0代表命令本身,$1-$9代表带一个到第九个参数,十以上的参数需要使用大括号表示,比如第十个参数为${10}

mark

  • Remember to empower
    mark

###2.5 Predefined variables

$*、$@:表示命令或脚本要处理的参数。
$*:把所有参数看成以空格分隔的一个字符串整体,代表"$1 $2 $3 $4"。
$@:把各个参数加上双引号分隔成n份的参数列表,每个参数是独立的,代表"$1" "$2" "$3" "$4"。

$0:表示当前执行的脚本或命令的名称。
$#:表示命令或脚本要处理的参数的个数。	
$?:表示前一条命令或脚本执行后的返回状态码,返回值为0表示执行正确,返回任何非0值均表示执行出现异常。也常被用于Shell脚本中return退出函数并返回的退出值。

示例:
vim mybak.sh
#!/bin/bash
time=backup-`date +%F`.tgz
tar zcf $time $* &> /dev/null	#/dev/null表示的是一个黑洞文件,通常用于丢弃不需要的数据输出
echo "已执行 $0 脚本,"
echo "共完成 $# 个对象的备份"
echo "具体内容包括: $*"

chmod +x mybak.sh
./mybak.sh /etc/passwd /etc/shadow

mark

  • There is a lot of content, find what we need to use here
    mark
    mark
    mark
    mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/111346534