In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes the relevant knowledge points of the common methods of dealing with errors in Bash scripts, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "what are the common ways to handle errors in Bash scripts".
The following editor will share with you some common ways to deal with errors in Bash scripts, how to obtain error codes, how to get detailed output when executing the script, how to handle debugging functions and error redirection, and so on, which can be referenced by friends in need.
Exit statu
In the Bash script, $? The exit status will be printed. If zero is returned, there are no errors. If it is not zero, the conclusion is that there may be some problems with the task.
The following is a simple example:
[root@localhost] # cat myscript.shemaking learningecho learningecho $?
If you run the above script once, it will print 0 because the directory does not exist, so the script will create it. If you run the script a second time, you will get a non-zero value, as follows:
[root@localhost ~] # sh myscript.sh0 [root@localhost ~] # sh myscript.shmkdir: error handling in cannot create directory 'learning': File exists1Bash scripts Best practices for error handling in Bash scripts
It is recommended that you enable debug mode by adding the set-x command to the shell script, as follows:
[root@localhost ~] # cat Test3. The mkdiir testing is called BASHSET-xecho "Hello World!
Then run the script to view:
[root@localhost ~] # sh test3.sh+ echo 'Hello Worldwaters Hello Worldwide + mkdiir testingtest3.sh: line 4: mkdiir: command not found
You can write the following debug function, which helps you call it at any time, using the following example:
[root@localhost ~] # cat debug. Shemale xa=2b=3c=$ DEBUG set + x (($a + $b)) DEBUG set + x
The output is as follows:
[root@localhost ~] # sh debug.shTesting Debugging+ axiom 2 + bau3 + cymbals 5 + DEBUG set + x +'['on = = on']'+ set + x2 + 3 = error handling in 5Bash script error handling error redirection in Bash script
All system errors can be redirected to a customization file using standard error output, which can be represented by the number 2. Execute it in Bash shell, as follows:
[root@localhost ~] # mkdir users 2 > errors.txt [root@localhost ~] # cat errors.txtmkdir: cannot create directory 'users': File exists
Most of the time, it is difficult to find the exact line number in the script. To print a line number with an error, use the PS4 option, where $LINENO is a predefined variable.
[root@localhost ~] # cat test3.shrunk raceme Bash Bash PS4 alarm alarm Lineno: 'set-xecho Hello World! "mkdiir testing
You can easily check the line number when reading an error:
[root@localhost ~] # sh test3.sh5: echo 'Hello WorldScripts Hello Worldwaters 6: mkdiir testingtest3.sh: line 6: mkdiir: command not foundBash error handling in Bash scripts about "what are the common ways to deal with errors in Bash scripts" this is the end of the introduction, more related content can be searched for previous articles, hope to help you answer questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.