当前位置:网站首页>Basic usage of shell script
Basic usage of shell script
2022-07-01 18:03:00 【RS don't forget your original intention】
Combined with daily use scenarios , Keep adding ~
1、 Stop the method immediately when there is a problem
set Ordered -e Parameters ,linux The description of the system is :"Exit immediately if a simple command exits with a non-zero status.", in other words , stay "set -e" Then the code appears , Once the return value is non-zero , The entire script will exit immediately , Then you can avoid some dangerous operations of scripts .
Let's look at two examples of comparison , It's easy to understand , Suppose the file name is demo.sh.
Example 1: Yes set -e
#!/bin/sh
set -e
ls /not_exist_dir/leo
echo $?
lsOutput is as follows , You can see that the program exits the first time you encounter an error , because /not_exist_dir/leo file does not exist , The error code returned at this time is not 0
$ sh demo.sh
ls: cannot access /not_exist_dir/leo: No such file or directoryExample 2: No, set -e
#!/bin/sh
# set -e
ls /not_exist_dir/leo
echo $?
lsOutput is as follows , You can see that all the orders are executed this time ,"ls /not_exist_dir/leo" The error code after executing the command is 2, But it did not hinder the implementation of the program .
ls: cannot access /not_exist_dir/leo: No such file or directory
2
AGILE_OUT
demo.shin addition , Directly returning the exit code can be used exit command , such as :
demo.sh
if [[ "$1" == "1" ]];then
echo 'your input is 1,OK'
exit 0
else
echo 'your input is not 1,not OK'
exit 1
fitest :
【1】 scene 1
$ sh demo.sh 2
your input is not 1,not OK
$ echo $?
1
【2】 scene 2
$ sh demo.sh 1
your input is 1,OK
$ echo $?
0边栏推荐
- Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
- transform. Forward and vector3 Differences in the use of forward
- DRF --- response rewrite
- MFC obtains local IP (used more in network communication)
- Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
- 线上开通ETF基金账户安全吗?有哪些步骤?
- Replace UUID, nanoid is faster and safer!
- How to use JMeter function and mockjs function in metersphere interface test
- [C supplement] [string] display the schedule of a month by date
- 开发那些事儿:EasyCVR平台添加播放地址鉴权
猜你喜欢

People help ant help task platform repair source code

Penetration practice vulnhub range Nemesis

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)

DNS
![[C supplement] [string] display the schedule of a month by date](/img/9c/5fcc6bfc8fe0f433c0d1eba92b5c3e.jpg)
[C supplement] [string] display the schedule of a month by date

New 95 community system whole station source code

Countdownlatch blocking wait for multithreading concurrency

New patent applications and transfers

Yolov5 practice: teach object detection by hand

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
随机推荐
Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
How to retrieve the password for opening Excel files
Subnet division and summary
JS how to convert a string with a delimiter into an n-dimensional array
How to write good code - Defensive Programming Guide
The latest software scheme of the intelligent information management system of the armed police force
【Try to Hack】vulnhub DC4
Development cost of smart factory management system software platform
深度优先遍历和广度优先遍历[通俗易懂]
反射型XSS漏洞
pyqt5中,在控件上画柱状图
麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
People help ant help task platform repair source code
D @ safety and dip1000
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
Function, condition, regular expression
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #