Getting Started with Shell scripting--Next

Source: Internet
Author: User
Tags logical operators stdin

Getting Started with Shell scripting--Next

This article introduces the following: Logical operators (&, | 、!、 ^), test commands, script execution procedures

1. Logical operators

(1) & and && short circuit and

True, False

1 0

And:

1 & 1 = 1

1 & 0 = 0

0 & 1 = 0

0 & 0 = 0

For &, if the left condition is false, the value of the right condition is also evaluated. Features as long as a false is assumed to be false

Short Circuit with:

False && = False

True && false = False

True && true =true

Short-circuit calculation, refers to the system from left to right of the calculation of the logical expression, once the calculation results have been determined, the calculation process is terminated. For &&, if the condition on the left is false, the right-hand condition is not computed, which is known as a short-circuit phenomenon.

(2) | OR and | | Short Circuit or

Or:

1 or 1 = 1

1 or 0 = 1

0 or 1 = 1

0 or 0 = 0

for | , the value of the right condition is also computed if the left condition is true. Features as long as a true is considered true

Short Circuit or:

True && = True

False && True =true

False && false= False

for | | , if the condition on the left is true, the condition on the right is not evaluated, which is known as a short-circuit phenomenon.

(3) No! : Take counter

! 1 = 0

! 0 = 1

XOR: ^

XOR two values, same as false, different for true

2 , test command:

Test EXPRESSION

[EXPRESSION]

[[EXPRESSION]]

Note: You must have a white space character before and after EXPRESSION

Conditional execution operator:&&, | |

A &&b execute B when a is executed successfully

C | | D does not execute if C fails.

Examples of long formats:

Test "$A" = = "$B" && echo "Strings is equal"

Test "$A"-eq "$B" && echo "integers is equal"

Examples of shorthand formats:

["$A" = "$B"] && echo "Strings is equal"

["$A"-eq "$B"] && echo "Integers is equal"

Bash Numerical test:

-v VAR: variable var is set

Numerical test:

-GT is greater than

-ge is greater than or equal to

-eq is equal to

-ne is not equal to

-lt is less than

-le is less than or equal to

String test:

= = is equal to

> ASCII code is greater than ASCII code

< is less than

! = is not equal to

=~ whether the left string can be matched by the pattern on the right

Z "string" string is empty, empty is true, not empty is false

-N "string" string is not empty, not empty is true, empty is false

Note: (1) The operands used for string comparisons should use quotation marks

  

3, the execution of the script process
    

         (3-1) When the judgment block operation is finished, there are still actions to be performed: The remaining operations are executed sequentially until the last command is complete, exiting

         (3-2) When the judgment block operation is finished, no statements in the script need to be executed, exit the script directly.


4. Practice
    
  To write a script/root/bin/createuser.sh, the execution syntax of the script must be: (1) createuser.sh-u username-m Password, options and parameters can support multiple spaces, but cannot be reversed in order.    (2) When the correct option or parameter is not specified, exit the script with the error output prompt "Createuser.sh-u username-m password". (3) The user name must start with a letter, which can include numbers and _. Otherwise it is illegal. Prompt the user with the error output "user name contains only alphabetic data and underscores" (4) when the user name detection is legitimate, determine whether the user name is already present, if present, and then determine whether the user has set a password, if set password, direct exit, not set, the password is set to the specified password after the correct output mode display " Username Password has been updated after exiting "(5) when the user name does not exist, the user is created and the specified password is set for the user to display the" user username created and updated password "(6) requires that no other output results that are not required during script execution appear. The script should be returned in a non-correct way to return the parameter to a value other than 0.  ------------------------------------------------------- The answers are as follows---------------------------------------------------------------------------------------------vim createuser.sh#!/bin/bash[$ = "-U"-A $ $ = "-M"] &>/dev/null && (      & nbsp echo $ |egrep "\<^[[:alpha:]" ([[:d igit:]]|_|[ [: Alpha:]]) *$\> "&>/dev/null && (                 ' id $ &>/dev/null ' && (                         [' Getent Shadow $ |cut-d:-f2 ' = '! '] && (                                  Echo $4 |passwd--stdin $ &>/dev/null &&echo user ${2} password updated; exit 0   & nbsp;                            ) | | (echo password already exists; exit)                ) | | (' Useradd $ '; Echo $4 |passwd--stdin $ &>/dev/null &&echo user ${2} The password has been created and updated; exit 0)    & nbsp;   ) | | (The Echo user name contains only alphabetic data and underscores; exit)) || (Echo createuser.sh-u username-m password && exit 1)    #ExplatioN: #判断脚本格式是否正确 (correct to further determine whether the user name is legitimate) #       determine whether the user name is valid (legal to further determine whether the user already exists) #                determine if the user exists (if present, further determine if the password has been set) #                         determine if the password has been set (if you have not set a password, update the password, prompt and exit) #                        If password is set, exit #    when prompted             user does not exist, create user and set password, exit and prompt #        username is illegal, exit and prompt # script does not meet the requirements, exit and prompt


  

Getting Started with Shell scripting--Next

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.