当前位置:网站首页>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边栏推荐
- Work and leisure suggestions of old programmers
- In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
- Glidefast consulting was selected as the elite partner of servicenow in 2022
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- PHP implements sensitive word filtering system "suggestions collection"
- Fix the problem that easycvr device video cannot be played
- Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
- [PHP foundation] realize the connection between PHP and SQL database
- From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
- Gameframework eating guide
猜你喜欢

Yolov5 practice: teach object detection by hand

Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?

From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl

Countdownlatch blocking wait for multithreading concurrency

Samba basic usage

What are the legal risks of NFT brought by stars such as curry and O'Neill?

The method of real-time tracking the current price of London Silver

Domestic spot silver should be understood

DNS

This is the latest opportunity of the London bank trend
随机推荐
In depth Research Report on China's disposable sanitary products production equipment industry (2022 Edition)
Redis master-slave realizes 10 second check and recovery
Kia recalls some K3 new energy with potential safety hazards
What are the six steps of the software development process? How to draw software development flow chart?
Small exercise -- subnet division and summary
MFC obtains local IP (used more in network communication)
MySQL -- explain performance optimization
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
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
JDBC: deep understanding of Preparedstatement and statement[easy to understand]
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
About selenium element positioning being overwritten
Wechat applet blind box - docking wechat payment
Kernel stray cat stray dog pet adoption platform H5 source code
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
Yolov5 practice: teach object detection by hand
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes