Linux Shell study notes 7

Source: Internet
Author: User
The eighth Condition Statement 10.1.1 is a simple if statement $ if 3> 2; then Echo 3; else Echo 2; FI # This is the form of $ cat> test in the command line. sh #! /Bin/shif 3> 2; thenecho 3 elseecho 2fi10. 1.2 use the test format: Test expression simple call method: [expression] # Note that there must be two spaces on both sides of expression. The expression types that test can understand are divided into three types: 1. file Test 2. string comparison 3. number comparison 1. file test file test expression checks whether a file meets certain special rules. The common syntax for file testing is: Test option file or [Option file] For example: $ if [-D/home/Ranga/bin]; then Path = "$ path: /home/Ranga/bin "; check whether the fi test directory/home/Ranga/Bin exists. If yes, add it to the variable path. Test command file test option-B file if the file exists and is a special block file, it is true. -C file: true if the file exists and is a special character file. -D file: if the file exists and is a directory, it is true. -E file is true if the file exists. -F file: if the file exists and is a rule file, it is true. -H file is true if the file exists and is a symbolic link. -R file: true if the file exists and is readable. -W file: true if the file exists and can be written. -X file is true if the file exists and is executable. -O file is true if the file exists and is owned by a valid user ID. 2. string comparison the test command also supports simple string comparison 1) check whether the string is empty 2) Check whether two strings are equal to the string comparison option in the test command-Z string if the string length is 0, true-N string if the string length is not 0, true string1 = string2 if the two strings are equal, true string1! = String2 if the two strings are not equal, it is true for example: if [-z "$ fruit"]; thenecho "your fruit is empty"; elseecho "Your friut has the following fruit: $ fruit "The number comparison operator int1-EQ int2 of the fitest command if int1 is equal to int2 is true int1-ne int2 if int1 is not equal to int2, true int1-lt int2 if int1 is smaller than int2, true int1-Le int2 if int1 is smaller than or equal to int2, true int1-GT int2 if int1 is greater than int2, true int1-ge int2 if int1 is greater than or equal to int2, the true composite expression is as follows: $ if [-z "$ dthome"] & [-d "/usr/dt"]; then dthome =/usr/dt; fi10.2 case Sentence case word inpattern1) list1; pattern2) list2; esac here ";" is equivalent to the break in C and PHP, for example :#! /Bin/shfruit = kiwicase "$ fruit" inapple) echo "apple"; banana) echo "bannana"; kiwi) echo "the value is Kiwi ";; esac uses pattern to be a string containing rule characters and special wildcards. Pattern determines whether a matching exists. Case "$ term" in * term) Term = exterm; Network | dialup | unknown | VT [0-9] {1, 3}) term = VT100 ;; esac if the string ends with a string "term", $ term is assigned an xterm value. Otherwise, $ term is compared with the string network, Dialup, unknown, VT [0-9] {1, 3, if one of them matches, $ term is set to VT100
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.