当前位置:网站首页>Shell writing specifications and variables
Shell writing specifications and variables
2022-07-28 03:25:00 【IT.cat】
Catalog
1.1、shell What can a script do
1.2、 Statement shell The interpreter for the script
Two 、shell Variables and assignments
3、 ... and 、 Variable operation
5、 ... and 、 Pipe operator operation
One 、shell Concept
shell Is a command interpreter , He is on the outermost layer of the operating system , Responsible for direct dialogue with users , Explain user input to the operating system , And handle the output of various operating systems , Output to screen feedback to user , This kind of dialogue can be interactive or non interactive , The command we input is not recognized by the computer , At this time, the interpreter is needed to compile , Programming a language that a computer can recognize .
1.1、shell What can a script do
Automate the installation and deployment of software , Such as installation and deployment LAMP Architecture Services
Complete the management of the system automatically , Such as adding users in batch
Automate backup , Such as regular database backup
Automated analytical processing , Such as website visits
1.2、 Statement shell The interpreter for the script
If marked at the beginning of the file #!/bin/bash
#!/bin/env/bash
#!bin/sh
![]()
Two 、shell Variables and assignments
Variables are used to temporarily store data , And the data value can change , No language can do without variables , If something needs to be used more than once and will reappear , So you can use variables , If you need to modify, you can directly modify the variable value
2.1、 Custom variable
Format :
Variable name = A variable's value
# Variable name : A place where data is temporarily stored
# A variable's value : Temporary changeable data
![]()
echo The use of command
use echo View and reference the values of variables
By adding a leading symbol before the name “$”, You can reference the value of a variable , Use echo Command can view variables , Can be in one echo View multiple variable values at the same time in the command

When variable names are easily confused with other characters immediately following them , You need to add braces “{}” Enclose it , Otherwise, the correct variable name cannot be determined . For undefined variables , Will be displayed as a null value .

echo Options
echo -n Indicates no line feed output
Use echo -e Output transfer characters , Output the escaped content to the screen
The commonly used escape characters are as follows :
\c Don't wrap output , stay ”\c” If there are no characters after , The effect is equivalent to echo -n
\n Line break
\t After escape, it means to insert tab, That's the tab

2.2、 Special operations
There are also some special assignment operations , You can assign values to variables more flexibly , So as to be suitable for your complex management tasks .
| Single quotation marks ( ' ) | When the content to be assigned contains $,",\ And other characters with special meaning , Should be enclosed in single quotation marks . Within single quotation marks , You will not be able to reference the values of other variables , Any character is treated as a normal character . |
| Double quotes ( “ ) | Double quotation marks are mainly used to define strings , Especially when the content to be assigned contains spaces , Must be enclosed in double quotation marks ; In other cases, double quotation marks can usually be omitted . |
| Apostrophe ( ` ) | The apostrophe is mainly used to replace , Allows you to assign the screen output result of executing a command to a variable . The command line that must be executable within the range enclosed by the apostrophe , Otherwise, there will be mistakes . |
2.3、 Predefined variables
Predefined variables
The predefined variables are created by Bash A class of special variables pre-defined by a program , Users can only use predefined variables , and Cannot create a new predefined variable , You can't assign values to predefined variables directly . Predefined variables use “$” A combination of a symbol and another symbol indicates
$#: Indicates the number of positional parameters in the command line .
$*: Represents the contents of all positional parameters , These contents as a whole
[email protected]: Indicates that all position parameters are listed , But it is listed in a single form
$?: Indicates the return status after the execution of the previous command , The return value is 0 The execution is correct , Return any non 0 Values indicate an exception in execution .
$0: Indicates the name of the currently executing script or program
$$: Indicates that the process number of the current process is returned
$!: Returns the process number of the last background process 3、 ... and 、 Variable operation
| Operation content | Add (+)、 reduce (-)、 ride (*)、 except (/)、 Remainder (%) |
| Operation symbol | $(()) and $[ ] |
| Operation command | expr and let |
| Computing tools | bc |

expr and let

Four 、 Redirect
| type | The operator | purpose |
| Redirect input | < | Read data from the specified file |
| Redirect output | > | Put the standard output result , Overwrite and save to the specified file |
| >> | Optimize the output result to the end of the specified file | |
| Standard error output | 2> | Put the error message , Overwrite and save to the specified file |
| 2>> | Put the error message preservation To the specified file , Don't cover the original content | |
| Mixed output | &> | Standard output , Standard errors are saved to the same file |
| 2>&1 | Redirect standard error output to standard output |
< Example
[[email protected] opt]# passwd --stdin yy < a.txt
Change user yy Password .
passwd: All authentication tokens have been successfully updated .
[[email protected] opt]#
>,>> Example
[[email protected] yy]# cat test.sh
123456
[[email protected] yy]# echo "123456" >> test.sh
[[email protected] yy]# cat test.sh
123456
123456
[[email protected] yy]# echo "aaaa" > test.sh
[[email protected] yy]# cat test.sh
aaaa
[[email protected] yy]#
2>,2>> Example
[[email protected] yy]# cat test.sh
aaaa
[[email protected] yy]# asdadf 2> test.sh
[[email protected] yy]# cat test.sh
bash: asdadf: Command not found ...
[[email protected] yy]# sssss 2>> test.sh
[[email protected] yy]# cat test.sh
bash: asdadf: Command not found ...
bash: sssss: Command not found ...
[[email protected] yy]#
example 4, Mixed output
&> You can save the error and correct prompt information to the same file
[[email protected] yy]# cat test.sh
123456
[[email protected] yy]# echo "111111" &> test.sh
[[email protected] yy]# cat test.sh
111111
[[email protected] yy]# aaaaa &> test.sh
[[email protected] yy]# cat test.sh
bash: aaaaa: Command not found ...
[[email protected] yy]#
5、 ... and 、 Pipe operator operation
The pipe symbol "|" The command output on the left , As input to the command on the right ( Deal with people ), Multiple pipes can be used in the same command line .
stay shell Script , Pipeline operation is usually used to filter the required customs information .
Example
[[email protected] yy]# netstat -natp |wc -l
15
边栏推荐
- redis源码分析(谁说C语言就不能分析了?)
- 机器人开发--丝杠与导轨
- golang gorm查询任意字段的组装方法
- [SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
- Redis communication protocol -- resp protocol
- More than 50 interviews have been summarized, and notes and detailed explanations have been taken from April to June (including core test sites and 6 large factories)
- IronOCR for .NET 2022.8
- CF 7月25日-7月31日做题记录
- 响应式高端网站模板源码图库素材资源下载平台源码
- What if the word selection box of win11 input method is missing?
猜你喜欢

Uniapp——拨打电话、发送短信

Redis经典面试题总结

Softek Barcode Reader 9.1.5

Defect detection of BP SVM system design of leaf defect detection

数据湖(十七):Flink与Iceberg整合DataStream API操作

Redis memory recycling

My approval & signature function of conference OA project

Thread Foundation

max_pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType

RBD块存储设备的扩容以及缩容操作(六)
随机推荐
VMware虚拟机网络设置
「运维有小邓」网络设备监控
CF 7月25日-7月31日做题记录
图文并茂:JVM 内存布局详解
QT official example: Fridge Magnets example
Summary of concurrent programming interview questions
IronOCR for .NET 2022.8
【R语言】环境指定删除 rm函数
VI command details
C#实现弹出一个对话框的同时,后面的form不可用
工程地质实习-工程地质 题集
Full of dry goods, hurry in!!! Easy to master functions in C language
如何卸载干净zabbix服务?(超详细)
Pytoch correlation gradient echo
RBD块存储设备的扩容以及缩容操作(六)
Uniapp - make phone calls and send text messages
20条心灵鸡汤唯美句子,句句温情暖心!
redis源码分析(谁说C语言就不能分析了?)
Win11如何重命名音频设备
Redis通信协议--RESP协议