当前位置:网站首页>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边栏推荐
- Apk signature process introduction [easy to understand]
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
- Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
- Function, condition, regular expression
- Small exercise -- subnet division and summary
- Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
- At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
- Is online stock account opening safe? Is it reliable?
- Cassette helicopter and alternating electric field magnetic manometer DPC
- To improve the efficiency of office collaboration, trackup may be the best choice
猜你喜欢
![Htt [ripro network disk link detection plug-in] currently supports four common network disks](/img/e4/c06b94243cdbe391b8b2124ff30789.jpg)
Htt [ripro network disk link detection plug-in] currently supports four common network disks
![[PHP foundation] realize the connection between PHP and SQL database](/img/eb/c8953eddfe3b19b0adb5529957d275.jpg)
[PHP foundation] realize the connection between PHP and SQL database

Kia recalls some K3 new energy with potential safety hazards

Penetration practice vulnhub range Tornado

Intelligent operation and maintenance practice: banking business process and single transaction tracking

Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes

ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
![[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)](/img/e8/f43f5583e330fbc0cb6c0188711707.jpg)
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
随机推荐
反射型XSS漏洞
【Try to Hack】vulnhub DC4
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
Depth first traversal and breadth first traversal [easy to understand]
Oracle TRUNC function processing date format
How to write good code - Defensive Programming Guide
Length of learning and changing
ZABBIX alarm execute remote command
Kia recalls some K3 new energy with potential safety hazards
Can hero sports go public against the wind?
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
MySQL + JSON = King fried
What are the legal risks of NFT brought by stars such as curry and O'Neill?
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
The latest software scheme of the intelligent information management system of the armed police force
JS how to convert a string with a delimiter into an n-dimensional array
ArrayList扩容详解
Fix the problem that easycvr device video cannot be played
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
Irradiance, Joule energy, exercise habits