Shell Programming Note one

Source: Internet
Author: User
Tags arithmetic stdin

Shell Programming:
Bash Variable type:
Environment variables
Local variables (local variables)
        Positional variables
        Special variables (built-in)

Local variables:
Varname=value: Scopes are available for the entire bash process;
        Variable Naming conventions:
        1. Can only contain letters, numbers and underscores, and start with letters and underscores
        2. It is best not to duplicate the system's existing environment variables
        3. See the meaning of the name

Local variables:
Local Varname=value: Scope is the current code snippet;

Environment variable: The scope is the current shell process and its child processes and cannot affect its parent process;
Export Varname=value, if the variable is already defined, it can only be exported varname with the variable name, i.e.
        1. Export Varname=value
        2. Varname=value
Export VarName
        The script launches a child shell environment variable when the command is executed:
        Scripts that are automatically executed by the system (not command-line startup) require a self-defined environment variable;


Positional variables:
$1,$2,$3, ...


Special variables:
$?: The return value of the previous command execution state:

        There are two possible types of return values for program execution:
1. Program execution Results
                2. Does the program state return (0-255)
0 to perform the correct
1-255 Execution Error (1,2,127 system reservation);


Bash:

Reference variable: ${varname}, parentheses can be omitted


Undo Variable:
unset varname

To view variables in the current shell:
Set
        Include environment variables and local variables

To view environment variables in the current shell:
1. printenv
        2. Env
        3. Export

Condition Judgment:
If the user does not exist:
Add user, give password and display add success;
Otherwise
Show user already exists, not added;

In bash, conditions determine:
Condition Test Type:
1. Integer test
2. Character test
3. File Testing


Expressions for Conditional tests:
1. [Expression]
        2. [[Expression]]
        3. Test expression
Integer comparison:
-EQ: Tests whether two integers are equal, such as $A-eq $B, returns 0 if the value of a and B variable is equal, otherwise returns a non-0 value
        -ne: Test whether two integers are not equal;
        -GT: Tests whether one number is greater than the other, is greater than true, or false
        -LT: Tests whether a number is less than 01 digits, less than true, otherwise false
        -ge: Tests whether a number is greater than or equal to another number, is greater than or equal to true, otherwise false
        -le: Tests whether a number is less than or equal to another number; less than or equal to true, otherwise false

The logical relationship between commands:
Logic and:&&
The second condition is not executed when the first condition is false
                When the first condition is true, the second condition is executed as a result
        Logical OR: | |
When the first condition is true, the second condition is not executed and the result is true
                When the first condition is false, the second condition is executed as a result


1. Single-branch if statement:
        if judgment condition; Then
Statement1
                Statement2
                ......
        Fi
2. Dual-Branch If statement:
if condition judgment; Then
Statement1
                Statement2
                ......
        Else
Statement1
                Statement2
                ......
Fi

        If then and if are not on the same line, then ";" Can be omitted, if on the same line, it must not be omitted. Fi must have an exclusive line

If the user exists, the user is already present, otherwise the user is added
ID user1 && echo "user1 exist" | | Useradd user1

If the user does not exist, add it or show that it already exists
! ID user1 && Useradd user1 | | echo "User1 exists."

If the user does not exist, add and set the password, otherwise, it will show that it already exists:
! ID user1 && useradd user1 && echo "user1" | passwd--stdin User1 | | echo "User1 exists."

Shell Scripting Exercises:
Practice a requirement:
1. Add 5 users, User1, User2, ..., user5;
        2. Each user's password with the user name, and requirements, add the password after the completion of the passwd command does not display the results of information;
        3. Finally shows the number of users on the current system;
        #useradd user1
        #echo "User1" | passwd--stdin user1 &>/dev/null
        #echo "Add user1 successfully."
        ! ID user1 &>/dev/null && useradd user1 && echo "user1" | passwd--stdin user1 &>/dev/null | | echo "User1 exists."
        ! ID user2 &>/dev/null && useradd user2 && echo "user1" | passwd--stdin user2 &>/dev/null | | echo "User2 exists."
        ! ID user3 &>/dev/null && useradd user3 && echo "user1" | passwd--stdin user3 &>/dev/null | | echo "User3 exists."
        ! ID user4 &>/dev/null && useradd user4 && echo "user1" | passwd--stdin user4 &>/dev/null | | echo "User4 exists."
        ! ID user5 &>/dev/null && useradd user5 && echo "user1" | passwd--stdin user5 &>/dev/null | | echo "User5 exists."

        users= ' Wc-l/etc/passwd | cut-d:-f1 '
        echo "$USERS USERS"

Exercise two requires:
        Given a user, if its uid is 0, this is the administrator user, otherwise, it will be displayed as a normal user;
        Name=user1
        Userid= ' Id-u $NAME '
        [$USERID-eq 0] && echo "Root" | | echo "Common user."



Arithmetic operations:
A=4
B=4
1. Let arithmetic expression
Let c= $A + $B
        2. $[Arithmetic expression]
c=$[$A + $B]
        3. $ (arithmetic expression)
c=$ (($A + $B))
        4. Expr arithmetic expression, with a space between the operands and operators in the expression, and to use the command reference
c= ' expr $A + $B '

Shell Programming Note one

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.