19. Self-learning Linux: The logic of bash conditions and character testing of bash programming

Source: Internet
Author: User

pre-Class review : To find the sum of divisible by 3 divisible by 200

# !/bin/bash  -Isum= 0 for with {1..200};d o  if [$[$i%3]-eq 0]; then let Su M+="Thesum is: $sum"

Bash's knowledge points :

Conditional testing: Making a logical operation on a condition

            with : Condition 1 && Condition 2

            or : Condition 1 | | condition 2

           non- :! Conditions

      example :#userName =root

#id $userName && echo "userName exist"

If the former fails, the latter will not execute because the final result of the operation is already available.

If the former succeeds, the latter still needs to be executed to determine whether the latter is true before the result can be obtained.

      

Classic Conditional judgment :#id #userName &>/dev/null && echo "$userName exist" | | echo "$userName not exist"

parsing :

When condition 1 is satisfied, and because statement 2 is the output statement, it must be true, so the condition 1 and statement 2 are true, and then the statement 3 does or operations, condition 3 can not be executed.

When the condition 1 is not satisfied, because the condition 1 with the statement 2 is with the operation, so there is no need to do with the operation, the first two conditions are false, at this time, the 3rd statement must be executed.

The effect of this command is: if the condition 1 satisfies the execution statement 2, if the condition 1 is not satisfied, then executes the statement 3.

Bash Programming : character Testing

      Binocular operator :

: Greater Than

<: Less than

= =: The left side is a string, the right is a pattern, determine the left side of the string, can be matched by the pattern on the right

Format: usually only used in [[]], the pattern can use the beginning of the line, the end of the row anchor characters, such as metacharacters, but the pattern can not be quoted

      single-Mesh operators :

            - N $stringVar: string is not empty, not empty is true, empty is false

            - Z $stringVar: Whether the string is empty, empty is true, and not empty is false

             

Example :  

#stringA ="root"#stringB="hello"  $stringA"$stringB"  ] #echo $?  "stringa" "$stringA" =~ OT]]: pattern matching

Exercise 1: Determine if a given user is a version of the shell

#userName =root#[[' grep ' ^ $userName \> "/etc/passwd  |  cut-d:-f7 ' =~ sh$]] #echo $?

  

Exercise 2: Find the shell that can be landed and output

 for  in ' cut-d:-f1/etc/passwd ', do if"^ $userName \>" /etc/ passwd  |  cut-d:-f7 ' =~"login Shell: $userName"else"  nologin User: $userName"  fidone  

 

19. Self-learning Linux: The logic of bash conditions and character testing of bash programming

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.