当前位置:网站首页>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边栏推荐
- MySQL -- explain performance optimization
- Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
- Fix the black screen caused by iPhone system failure
- 网上股票开户安全吗?是否可靠?
- 麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
- (十六)ADC转换实验
- Product service, operation characteristics
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- 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
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
猜你喜欢

Kia recalls some K3 new energy with potential safety hazards

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

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

Thinkphp6 - CMS multi wechat management system source code

DNS

(16) ADC conversion experiment

Check log4j problems using stain analysis

(17) DAC conversion experiment

Good looking UI mall source code has been scanned, no back door, no encryption

(十七)DAC转换实验
随机推荐
Wechat applet blind box - docking wechat payment
Gameframework eating guide
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Kernel stray cat stray dog pet adoption platform H5 source code
JDBC: deep understanding of Preparedstatement and statement[easy to understand]
Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
Good looking UI mall source code has been scanned, no back door, no encryption
(十七)DAC转换实验
Function, condition, regular expression
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
C language implementation of sum of two numbers [easy to understand]
New 95 community system whole station source code
The difference and relationship between iteratible objects, iterators and generators
(17) DAC conversion experiment
[C supplement] [string] display the schedule of a month by date
D @ safety and dip1000
麦趣尔:媒体报道所涉两批次产品已下架封存,受理消费者诉求
Oracle TRUNC function processing date format
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
MySQL -- explain performance optimization