当前位置:网站首页>Shell programming core technology II
Shell programming core technology II
2022-07-04 19:24:00 【Wu_ Candy】
Continue to share today Shell Programming core technology related knowledge , Let's continue with yesterday's content .
“ Shell Programming core technology 《 One 》” Students who haven't read this article , Please go to the official account to make up your classes ~ All right. , Start today's article sharing .
1
Use of special symbols
for example : Input x=(echo abc) The instruction sets the string abc Deposit in x variable , Enter echo x Instructions , You'll see
Another thing to note is the sequence , such as We want to get from 1~10 Get a sequence , You can enter echo {1..10} Command to print 1~10 The number of .
just so so Use seq 1 10, They are equivalent .
2
Variable type
stay Shell Variable types are not distinguished in , This and Python It's like , All variables are basic types , Only do dynamic parsing at runtime . Where the string 、 Numbers 、 Boolean is commonly used .
First, the string , Strings are often used for something like pinching 、 Decapitation 、 Replacement operation , You can practice by yourself after class , There is no more specific demonstration here .
Then Boolean types , The basic form of Boolean is true and false, It is important to note that in Shell There is a special usage in , If a command returns 0, It means that this process is working normally .
for example : Input ls /tmp/hello.txt ;echo $? Instructions , You can see that the return value is 0, Indicates that the process is running correctly , If other values are returned, it means that the process is running incorrectly .
for example : Input ls /tmp/hello.txtaaa ;echo $? Instructions Run a nonexistent file , Output is 1 Indicates a running error .
3
Judge
Judgment mainly includes arithmetic judgment , Logical judgment of and or not , Some more Shell There are three aspects of judgment built in . First, let's look at arithmetic judgment , Usually, arithmetic judgment can be used to compare the relationship between two variables , For example, the size of two numbers is compared , String matching relationship , wait .
In addition to simple conditional judgment ,Shell It also supports complex and 、 or 、 Illogical judgment .
besides ,Shell Some built-in judgments are also provided , such as :
- -e file Indicates if the file exists , The result is true ;
- -d file Indicates if the file is a subdirectory , The result is true ;
- -f file Indicates if the file is an ordinary file , The result is true ;
- -r file Indicates if the file is readable , The result is true ;
- -s file Indicates if the length of the file is not 0, The result is true ;
- -w file Indicates if the file is writable , The result is true ;
- -x file Indicates if the file is executable , The result is true .
Because arrays are not often used , No more details here , If you are interested, you can practice by yourself after class .
4
Logic control
After learning the relevant knowledge of variables , We continue to learn logic control , Some logic can be designed based on data , As shown below :
- conditional if;
- Branch judgment case、select, Different treatments are carried out according to different conditions ;
- loop for、while、until;
- break and continue, Exit loop .
You can see the whole logic control and Python It's very similar .
5
if Judge
Take a look first if conditional ,if First, check whether the judgment condition is true , If so, execute then Logic within a statement block ,else Execute the logic that the judgment is not tenable , also elif…if…, It is similar to Python, When the conditions are not met, judge the next condition .
6
for loop
And then there was for loop ,for The cycle is Shell There are two uses in . The first usage is similar Java or Python , from 1~10 Cycle through , It can be used at this time for((i=0;i<10;i++)) Realization , This usage is a judgment of the exact number of cycles based on the cardinality .
The second usage is for Traversal cycle , You can use for…in… Sentence block .
for example : Input for i in (seq 1 3 10) Instructions , And then in do Print in the statement block echo i Value ,do Statement block to done end .
You can see the print 1、4、7、10, This is for Use of loop traversal .
7
while loop
And finally while loop , and for The loop is very similar ,while First, judge the conditions , The condition holds in do Execute the operation in the statement block .
for example : Definition i=0, Then input while ((i<3));do((i=i+1));sleep 1;echo $i;done Instructions .
You can see , Output results every 1 Print one per second i Value , and i The value of increases gradually , Until it's equal to 3 The condition is no longer satisfied , Exit loop .
and while There is also a very common function , Namely adopt while read line Loop through every line of the file .
for example : Input while read line;do echo $line;done< /tmp/hello.txt Instructions , It will print out every line of information in the file .
besides , It is also possible to use pipes , Input cat /tmp/hello.txt | while read line;do echo $line;done Instructions , The output effect is the same .
8
Out of control
And there are some complex conditions that need to exit in time , At this time, we need to master the statements that control exit , such as :
- return The function returns ;
- exit The script process exits ;
- break Exit the current loop ;
- continue Jump out of current loop , Enter next cycle .
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 .
边栏推荐
- 神经网络物联网应用技术就业前景【欢迎补充】
- Scala基础教程--17--集合
- Lex and yacc based lexical analyzer + parser
- Using SSH
- Other InterSystems%net tools
- 2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
- Shell 编程核心技术《四》
- Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
- Scala basic tutorial -- 13 -- advanced function
猜你喜欢

Oracle with as ORA-00903: invalid table name 多表报错

Scala basic tutorial -- 13 -- advanced function

Scala basic tutorial -- 19 -- actor
Summary and sorting of 8 pits of redis distributed lock
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0

LeetCode第300场周赛(20220703)

升级智能开关,“零火版”、“单火”接线方式差异有多大?

Principle and application of ThreadLocal

Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file
随机推荐
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
Qt实现界面滑动切换效果
Basic tutorial of scala -- 16 -- generics
从实时应用角度谈通信总线仲裁机制和网络流控
Have you guys ever used CDC direct Mysql to Clickhouse
LeetCode 赎金信 C#解答
技术分享 | 接口测试价值与体系
使用FTP
[go ~ 0 to 1] read, write and create files on the sixth day
6.26cf simulation match B: solution to array reduction problem
876. 链表的中间结点
页面元素垂直水平居中、实现已知或者未知宽度的垂直水平居中。
2021 合肥市信息学竞赛小学组
神经网络物联网是什么意思通俗的解释
神经网络物联网平台搭建(物联网平台搭建实战教程)
PB的扩展DLL开发(超级篇)(七)
Use canal and rocketmq to listen to MySQL binlog logs
国元期货是正规平台吗?在国元期货开户安全吗?
IBM WebSphere MQ retrieving messages
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0