Shell programming notes

Source: Internet
Author: User

Shell programming notes
Shell programming:
Bash variable type:
Environment Variable
Local variable)
Location variable
Special variables (built-in)

Local variable:
Varname = value: applies to the entire bash process;
Variable naming rules:
1. It can only contain letters, numbers, and underscores and must start with a letter or underline.
2. It is best not to name the same environment variable as the existing one in the system.
3. Learn what you mean

Local variables:
Local varname = value: the scope is the current code segment;

Environment variables: the scope is the current shell process and its sub-processes, and cannot affect its parent process;
Export varname = value "export". If the variable has been defined, you can use the variable name export varname, that is
1. export varname = value
2. varname = value
Export varname
The script starts a sub-shell environment variable when executing the command:
The scripts automatically executed by the system (not started by the command line) Need to customize the required environment variables;


Location variable:
$1, $2, $3 ,......


Special variables:
$? : Returned value of the previous command execution status:

Program execution may return two types of values:
1. program execution results
2. Is the program status returned (0-255)
0 indicates that the execution is correct.
1-255 execution error (127, system reserved );


Bash:

Reference variable: $ {varname}. Brackets can be omitted.


Undo variable:
Unset varname

View the variables in the current shell:
Set
Including environment variables and local variables

View the environment variables in the current shell:
1. printenv
2. env
3. export

Condition determination:
If the user does not exist:
Add a user to the password and display that the user is successfully added;
Otherwise
The user already exists and is not added;

Condition determination in bash:
Condition test type:
1. Integer Test
2. Character Testing
3. File Test


Conditional test expression:
1. [expression]
2. [[expression]
3. test expression
Integer comparison:
-Eq: test whether two integers are equal. For example, $ A-eq $ B. If the values of A and B are equal, 0 is returned. Otherwise, A non-0 value is returned.
-Ne: test whether two integers are unequal. equal to false, unequal to true
-Gt: test whether one number is greater than the other. If the value is greater than true, otherwise the value is false.
-Lt: test whether a number is smaller than zero. If the value is smaller than true, the value is false.
-Ge: test whether a number is greater than or equal to the other number. If it is greater than or equal to true, otherwise it is false.
-Le: test whether a number is less than or equal to the other number. If it is less than or equal to true, otherwise it is false.

The logical relationship between commands:
Logic and :&&
When the first condition is false, the second condition will not be executed.
When the first condition is true, the second condition is executed as the result.
Logic or: |
If 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 the result.


1. if statement for a single branch:
If judgment condition; then
Statement1
Statement2
......
Fi
2. if statement for dual Branch:
If condition judgment; then
Statement1
Statement2
......
Else
Statement1
Statement2
......
Fi

If then and if are not in the same row, then ";" can be omitted. if then is in the same row, it cannot be ignored. Fi must exclusively occupy one row

If a user exists, the user is displayed; otherwise, the user is added.
Id user1 & echo "user1 exist" | useradd user1

If the user does not exist, add the user; otherwise, it is displayed as already exists.
! Id user1 & useradd user1 | echo "user1 exists ."

If the user does not exist, add and set the password. Otherwise, it is displayed as follows:
! Id user1 & useradd user1 & echo "user1" | passwd -- stdin user1 | echo "user1 exists ."

Shell script exercises:
Exercise 1 requirements:
1. Add five users, user1, user2 ,......, User5;
2. The password of each user is the same as the user name. The execution result of the passwd command is not displayed after the password is added;
3. display the total 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 2 requirements:
If the UID of a user is 0, it is displayed as the administrator user; otherwise, it is displayed as a common user;
NAME = user1
USERID = 'id-u $ name'
[$ USERID-eq 0] & echo "root" | echo "common user ."



Arithmetic Operation:
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. There must be spaces between the operands and operators in the expression, and commands must be used to reference

C = 'expr $ A + $ B'

Bash-n filename: test whether the script file has a syntax error.
Bash-x filename: displays each step of execution.

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.