Linux Learning Notes

Source: Internet
Author: User

What is the criterion of if in the shell language for if else statements, after the IF command, if the exit status code is 0, executes the statement after then, otherwise executes the statement following the else.

Cases:

  
 
  1. #!/bin/bash
  2. #test the if statement
  3. if pwd
  4. then
  5. echo “means command ‘wpd’ output statement code 0
  6. else
  7. echo “pwd command dosen’t work”
  8. fi

And what is a status code? = = "In Linux each command runs and exits with an exit status code to the shell, which ranges from 0-255 to a number. A command runs successfully, then it exits with an exit status code of 0, otherwise the resulting exit status code is a positive number other than 0, as shown in 1.

In actual shell programming, the keyword if is not followed directly by the command, but if [condition]. Note the addition of brackets [] and no brackets are different. The
adds square brackets to indicate if test condition, and the command test tests the following condition condition, and returns a positive number if the condition is set to exit status code 0, otherwise the exit status code is not 0. shown in detail in 2.

in the diagram, Test Pwdd and PWDDFG with the test command (none of the 2 variables are assigned), but look at the exit status code, find the Body code 0,
in the diagram, test the test-f GDGA with the tests command; test-d Gdga, View exit status code is 1 (generic unknown error). The reason for this is that because test-f file;test-d file, it will take the following arguments as files and check if it is a file (-f) and check if it is a file directory (-D). And GDGA is not assigned at all. So it returns a non-0 status code. The
square brackets define the test condition, which is to replace the command test with []. In writing, you need to add a grid

after the first square bracket and the second square bracket.

 
   
  
  1. if [ condition ]
  2. then
  3. operation
  4. fi

In square brackets (Test command) You can determine the condition in 3:
1. Numerical comparison
2. Character comparison
3. File comparison
After the comparison, the result is a Boolean value (True and false), the square brackets (Test command) converts the Boolean result to the exit status code, and if the Boolean value is true, the square brackets (Test command) generates an exit status code of 0 for If-else. If the Boolean value is false, the square brackets (Test command) generates a non-0 exit status code for If-else.
In condition judgment, the main parameter is to determine whether the target is a file or a string. Specific 3 and Figure 4 are shown.

In the code in Figure 3, if the argument in square brackets is-D, the variable test is treated as a file, and if the = is considered to be a string, the string is compared.



Null

Linux Learning Notes

Related Article

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.