Shell Learning Notes VI (Test and judgment)

Source: Internet
Author: User
Tags logical operators

Test and judgment tests use the $ after command execution to determine if the command is performing properly. $?==0? Normal: Error test structure: 1, test expression 2, [expression] #注意表达式两侧的空格 mode 2 increases the readability of the code, and more easily with I        F,case,while the keywords associated with these conditions. File test: 1, Test file_operator file 2, [File_operator file] Example: Test-e/var/l                Og/message echo $?             #文件存在返回0, does not exist return non 0 value [-e/var/log/message] echo $?                File Comparator-B file exists and is a block file-C file exists and is a character device-e file or directory exists with a-D file exists and is a directory                -F file exists and is an ordinary file-X file exists and is an executable file-W file exists and is a writable file-R file exists and is a readable file -l file exists and is the connection file-p ...                Pipe file-S ... socket file-s size not 0 FILE1 nt FILE2 FILE1 than FILE2 new return True FILE1 ot FILE2 FILE1 than FILE2 old return True example: Test file read, write, execute properties #!/bin/bash read-P "input a filename which you want to test:" FileName if [!                    -E "$filename"];then echo "the file doesn ' t exist."                Exit 1 fi if [-W ' $filename "];then echo" the file is writeable. "                fi if [-r] $filename "];then echo" the file is readable. "                fi if [-X] $filename "];then echo" The file is executable "fi                               Note: There is a space string test between if and [] in if [expression]: string comparison (dictionary order) is mainly greater than, less than, equal to, not equal to, empty, not empty > < = =-z-n Example: str= "" Test-z        "$str" echo $?        #0 test-n "$str" echo $?        #1 test-n $str echo $? #0, preferably with double quotation marks ("") str= "Hello" [-Z "$STR "] echo $?        #1 [-N "$str"] echo $? #0 str1= "123" str2= "234" ["$str 1" = "$str 2"] #test $str 1 = $str 2, pay attention to spaces, or keep returning        Back to 0 echo $?                #1 ["$str 1"! = "$str 2"] echo $?                ["$str 1" \> "$str 2"] echo $?                ["$str 1" \< "$str 2"] echo $?        [["$str 1" > "$str 2"]] #不使用转义字符 echo $?            Integer comparison-eq =-lt <-gt >-le <=-ge >=-ne! =                1, Test num1 num_operator num2 2, [NUM1 num_operator num2] Example: num1=10                num2=10 num3=9 num4=11 #测试相等 [$num 1-eq $num 2]                echo $?            Test $num 3-eq $num 2 echo $? Note: Be sure to note the space, otherwiseThere is a possibility that the error logic and logical operators are logical with, logical, or logical non--a-o!     Non:!expression with: Expression1-a expression2 or: Expression1-o expression2 &&     ||               ! [-e/var/log/message] && [-e/var/log/message01] =[-e/var/log/message-a-e/var/log/message01 ] [-e/var/log/message] | |        [-E/VAR/LOG/MESSAGE01] =[-e/var/log/message-o-E/VAR/LOG/MESSAGE01] Notice two different ways to judge            If statement if Expression;then Command1 Command2 ... fi                Example: Judging student achievement #!/bin/bash Echo-n "input a score of a student:" #-n means no line break                Read score if [$score-lt];then echo "D" fi If [$score-lt 80-a $score-ge];then echo "C" fi if [$score-lt 90-a $score-ge];then echo "B" fi if [$score-le 100                    -A $score-ge];then echo "a" fi if [$score-gt];then echo "The number is invalid" Fi has learned high-level language, is actually learning shell grammar and programming features, high-level language focus on large projects, and shell focus on the work            Make the work Automation If/else statement if expression; then command else command                Fi Example: Determine if a file exists #!/bin/bash echo-n "input a filename:"                Read filename If [-e $filename];then echo "file exists."                else echo "file doesn ' t exists." Fi if/elif/else Multiple If/else Statement Example: Modify student score script #!/bin/bash Echo-n "in Put a score of a student: "Read score if [$score-lt];then echo "D" Elif [$score-lt];then echo "C" elif                 [$score-lt];then echo "B" Elif [$score-le];then echo "A"             elif [$score-gt];then echo "The number is invalid" fi case                    Equivalent to Switch...case structure: Case VAR in var1) command;;                    VAR2) command;;                    VAR3) command;;            .. *) command;; Note: The var1, Var2, VAR3, and so on can only be constants or regular expressions, and can be regular, good example: detect the type of current system #!/bin/bash type= ' una                         Me-s ' #print The kernel name case $type in l*) echo "Linux"                    ;;                    SunOS) echo "SunOS";;    Darwin)                    echo "Darwin";;                *) echo "other";;                ESAC detects if the user input contains uppercase letters, lowercase letters, or numbers #!/bin/bash echo-n "input a string:" Read STR case $STR in *[[:lower:]]*) echo "the string                    Contains lower letters. ";;                        *[[:upper:]]*) echo "The string contains upper letters."                    ;;                        *[[:d igit:]]*) echo "The string contains digit."                    ;;                *) echo "other";; Esac

Shell Learning Note VI (Test and judgment)

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.