About trap and expr in shell

Source: Internet
Author: User
I really don't know how to write this article name about trap and expr in shell, so I just wrote such a strange name. It is about trap and expr. Trap and expr are two commands in shell, which should be familiar to everyone, that is, the four arithmetic operations, and the first...
I really don't know how to write this article name about trap and expr in shell, so I just wrote such a strange name. It is about trap and expr. Trap and expr are two commands in shell, which should be familiar to everyone, that is, four arithmetic operations. the previous command may not be familiar to anyone who did not write shell scripts. I will discuss its simple functions below. Www.2cto.com, but here, we may wonder why I want to write these two commands, that is, why these two commands are linked together, this is mainly because of a problem I encountered when writing scripts at work. These two seemingly unrelated commands are linked together. First, let's talk about what the trap command is. Traptrap is a powerful tool for writing shell scripts. it is usually used to handle various exceptions in shell, similar to catch (try .. catch ). Before talking about what the trap command is, let's talk about its syntax. the trap syntax is like this: trap [COMMANDS] [SIGNALS, the trap will capture the signal in [SIGNALS]. after the signal is captured, the COMMANDS command will be executed. For example, you can let the trap capture the SIGHUP signal and execute the command you wrote when the corresponding signal is captured. Trap is very useful, but I usually capture ERR instead of capturing signals. this ERR refers to the execution of all shell commands that do not return 0 values. In The shell Specification, after the command is correctly executed, the return values of the shell should be 0. if the value is not 0, the shell is not correctly executed. Therefore, if a non-zero response is captured, the corresponding processing is performed on all the shells that are not successfully executed. For example, you can write the trap echo failed ERR command to directly execute this sentence when you encounter any errors and directly echo failed. Of course, you can do better, such as printing out the wrong line number, which depends on your free use. Therefore, the trap command is very useful for exception capture and handling in shell scripts. You can go to this link and see that this link is not bad. http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html If expr is good and trap is finished, let's talk about expr. In fact, expr is nothing to talk about. it is just a four-digit arithmetic operation. I will post the command out and you will understand it: the $ expr 1/11 command is used to calculate the number of equal to 1 divided by 1. Well, since it is such a simple command, how does it relate to trap. Let's take a look at the following command: $ expr 1/20: This calculation is divided by 1 by 2. because expr can only be used for integer calculation, so it is equal to 0, and there is no problem. this command seems no problem, because dividing 1 by 2 is indeed equal to 0, but the problem is that the return value of this command. Let's take a look at caojiandong @ naruto .~ /$ Expr 1/20caojiandong @ naruto .~ /$ Echo $? 1 www.2cto.com you can see that the return value of this command is 1, and the return value of a normal command should be 0. For example, caojiandong @ naruto .~ /$ Expr 3/21caojiandong @ naruto .~ /$ Echo $? 0. As for why 1/2, the return value of expr is 1. I haven't studied it carefully yet, because the results of the visual test are okay. if you know this, I am still tired of providing advice. Here, we should know why expr is associated with trap, that is, the returned value. When I write a script, I execute an expr with a result of 0. the visual test is OK. As a result, his return value is 1, so it is caught by trap, the result throws an exception... Depend on expr 1/2. if the divisor is greater than the divisor, the result is 0. why is the return value 1 .... Nnd. after positioning it, I didn't use expr any more. I used $ () to assign values to the variable. Well, the article has been written, and there is no technical content, but I don't know what trap can do, because this command is really useful when writing shell. Source http://www.log4myself.info/archives/318
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.