当前位置:网站首页>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 $?
ls
Output 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 directory
Example 2: No, set -e
#!/bin/sh
# set -e
ls /not_exist_dir/leo
echo $?
ls
Output 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.sh
in 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
fi
test :
【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
边栏推荐
- EasyCVR设备录像出现无法播放现象的问题修复
- Slider verification code identification gadget display
- Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
- Why should you consider using prism
- MySQL -- explain performance optimization
- 股票万1免5证券开户是合理安全的吗,怎么讲
- [C supplement] [string] display the schedule of a month by date
- June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
- Enter wechat applet
- Penetration practice vulnhub range Nemesis
猜你喜欢
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
Source code of new campus errand / campus task platform on mutual station
Yuancosmos game farmersworld farmers world - core content of the second conference in China!
Thinkphp6 - CMS multi wechat management system source code
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
The method of real-time tracking the current price of London Silver
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
Penetration practice vulnhub range Keyring
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
随机推荐
transform. Forward and vector3 Differences in the use of forward
Review Net 20th anniversary development and 51aspx growth
ISO 27001 Information Security Management System Certification
麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
Domestic spot silver should be understood
Can hero sports go public against the wind?
Sword finger offer II 105 Maximum area of the island
Why should you consider using prism
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
Develop those things: easycvr cluster device management page function display optimization
Roewe rx5's "a little more" product strategy
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
Three dimensional anti-terrorism Simulation Drill deduction training system software
This is the latest opportunity of the London bank trend
Common design parameters of solid rocket motor
Fix the black screen caused by iPhone system failure
Extract the compressed package file and retrieve the password
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?