当前位置:网站首页>Shell programming core technology "I"
Shell programming core technology "I"
2022-07-04 19:24:00 【Wu_ Candy】
Today's main introduction Shell Programming core technology , That is, how to write Shell Script .
In our daily work, we often need to write some Shell Logical script to batch process some tasks , For example, read the input data for related processing , Put the task into a script for automatic operation and other scenarios , These all involve Shell Programming , Actually Shell Programming is also very simple , It's like Java、Python These popular programming languages are the same , It also has the characteristics of programming language , Let's see Shell Programming mainly involves several aspects .
- Variable
- Logic control
- Shell Environmental Science
- Script application
- automation
Variable
1
Variable definitions
Learn a programming technique , First, you need to master how to use variables , stay Shell Defining a variable in is very simple , It's related to Python Defining a variable in is very similar , You can define a variable in any position and assign a value to it , and Shell Than Python The more concise part is that you are not forced to enter single quotation marks or double quotation marks to indicate the content .
As shown in the figure above , Define a x=1 The variable of , Then input echo x Instructions , among x Indicates that this variable can be referenced .
As shown in the figure above , Input hello String assignment to variable x, And then print it echo $x.
If input x=hello world , At this time, the system will report an error , because x=hello It will be considered as a variable assignment , and world Will be considered another independent command .
When there are spaces in the variable value , We just need to put single quotation marks or double quotation marks on the string . Then print this variable echo $x That's all right. .
In the definition of variables , There are a few points that need our attention :
- Equal sign (=) There should be no spaces on the left and right ;
- If there are spaces in the content , Single or double quotation marks are required ;
- Double quotation marks support escape characters ,$ The first variable will be automatically replaced .
There are several methods to pay attention to in the use of variables ,echo a、echo “a” Can represent variable output , But if you want to use it more strictly, it is recommended to use double quotation marks , And when multiple strings are used in series , Use {} Enclose the content to indicate that the content is a variable , Avoid confusion with other characters that follow, resulting in an error .
2
Predefined variables
stay Shell Programming , The system also provides several predefined variables . such as :
- PWD Represents the current directory ;
- USER Represents the current user ;
- HOME Represents the home directory of the current user ,HOME You can also use ~ Abbreviation ;
- PATH Indicates all current executable programs ;
- RANDOM Can produce a random number .
among , We need to pay special attention to PATH Variable , Such as input echo $PATH Instructions , You can see PATH Variables define all executable programs , It's defined in PATH The program in can directly call the program name to execute without entering all the paths of the program .
For example, use which ls Instructions , You'll find that ls stay /bin/ls Under the table of contents , Print echo $PATH Variable , You will find the corresponding /bin Catalog , But if the program is not defined in PATH variable , You need to enter all the paths to execute the program .
3
Use of special symbols
In addition to these , You also need to master some special symbols , for example :
- Double quotation marks are used to enclose a string , Support $var Variable substitution of form ;
- Single quotation marks also indicate that its content is a string , But escape is not supported ;
- $’\n’ Express ANSI-C quote ;
- \ Backslash , In some cases, it means escape ;
- ((a=a+3)) Is an integer extension , Variables in double brackets are treated as integers ;
- $(ls) Execute the command and save the result as a variable , Shorthand for ``;
- {1..10} Equivalent to seq 1 10, Express 1~10 Numbers ;
- seq 1 3 10 Means to generate a step of 3 Of 1~10 The number of .
among , We need to focus on integer extension , You can put the mathematical expression in double brackets for corresponding calculation ,
Such as input echo $((100000/3)), Its result is 33333, Why is there no decimal place here ?
Because Shell Currently, only integers are processed in , If you need to be accurate to decimal places, you can use awk Instructions .Shell The integer calculation of does not use It's OK, too , But if you want to quote specific result values , You need to use .
The content shared today is quite practical , If you want to master these knowledge points , You still need to knock orders line by line , In this way, the impression will be more profound . Next issue will continue to share Shell The core technology of programming , Coming soon ~
Friendship tips :“ The way of immeasurable testing ” Original works , Welcome to exchange , It is forbidden for a third party to reprint the article without displaying its source .
边栏推荐
猜你喜欢
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file

大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行

MXNet对GoogLeNet的实现(并行连结网络)

Scala basic tutorial -- 15 -- recursion

Scala basic tutorial -- 19 -- actor

神经网络物联网平台搭建(物联网平台搭建实战教程)

BI技巧丨权限轴

Mxnet implementation of googlenet (parallel connection network)

Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii

Use canal and rocketmq to listen to MySQL binlog logs
随机推荐
信息学奥赛一本通 1336:【例3-1】找树根和孩子
IBM WebSphere MQ retrieving messages
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
Lex and yacc based lexical analyzer + parser
SSL证书续费相关问题详解
Scala basic tutorial -- 20 -- akka
From automation to digital twins, what can Tupo do?
The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
Summary and sorting of 8 pits of redis distributed lock
测试工程师如何“攻城”(下)
Using FTP
Detailed explanation of issues related to SSL certificate renewal
使用canal配合rocketmq监听mysql的binlog日志
Is the securities account opened by qiniu safe?
In flinksql, in addition to data statistics, is the saved data itself a state
Scala基础教程--17--集合
Qt实现界面滑动切换效果
神经网络物联网是什么意思通俗的解释