Bash working features:
1. Support Command history, command completion
2. Support Pipeline, redirect
3. Support Command aliases
4. Support for command line editing
5. Support Command line deployment
6. Support File name wildcard
7. Support for using variables
8. Support Programming
Command editing:
Cursor Jump: Ctrl + A: Skip to command line home
Ctrl+e: Skip to end of command line
Ctrl+u: Delete the cursor to the beginning of the command line
CTRL+K: Delete the cursor to the end of the command line
Ctrl+l: Clear Screen clear
Command history: Bash automatically logs commands that have been executed in the past and is saved to the in-memory cache
↑ or History
History-w: Saves the history of the command in the cache to the history file. Bash_history
History-c: Emptying command history
The use of command history tips:
!n: Execute nth command in command history
!-n: Executes the last nth command in the command history
!! : Executes the previous command
!string: The most recent command in the command history that starts with the specified string
!$: Reference the last parameter of the previous command
Press ESC to release and then press. Point
Press ALT +. Point
Quotes supported by bash:
": Command substitution
"": weak reference, can implement variable substitution
': Strong reference, can not implement variable substitution
File name wildcard:
*: matches any character of any length
?: any single character
[]: matches any character in the specified range
[A-z] [A-za-z] [A-z0-9]
[^]: matches any character outside the specified range
[^0-9]
Shell Basic Syntax
Variable
An expression
Judgment statement
An If expression
Shell Basics:
#!/bin/bash-->shbang-explaining the absolute path of the shell
Function: Shows what shell the Post command performs these
Command. If you do not refer to the shell, use the current shell as the executing shell
Two types of annotations:
#
;
Shell programs usually end with SH
To create a shell execution procedure
First step: Create a Shell file that contains the command and control structure.
The second step: Modify the permissions of this file so that it can be executed.
Using chmod u+x
Step Three: Execute
Method 1:[[email protected] shell]#./expr.sh
Method 2: Use absolute path [[email protected] ~]#/root/shell/expr.sh
Method 3:[[email protected] ~]# bash/root/shell/expr.sh
method need to give X permission, Method 3 No, Direct bash
=======================================================================
Shell variables--memory space, naming
Variables are a way for the shell to pass data. A variable is a symbolic name used to represent each value.
The Shell has two types of variables: temporary variables and permanent variables.
Temp variable: It is defined internally by the shell program and is scoped to the program that defines it and is not visible to other programs.
Permanent variable: Is an environment variable whose value does not disappear with the execution of the shell script.
=======================================================================================
Variable naming rules:
1. Can only contain letters, numbers and underscores, and cannot begin with a number
2. You should not duplicate the existing environment variables in the system
3. See the meaning of the name
Environment variables
Definition: Scope is the current shell process and its child processes
To view environment variables:
Echo $PATH
Env
When running a command, a command or file cannot be found locally and will be found in the directory of the Declaration.
Export Varname=value
The script starts a child shell process when it executes
Scripts that are started on the command line inherit the current shell environment variable
Scripts that are automatically executed by the system (not command-line startup) need to be self-defined to require environment variables
User-defined variables--local variables
Set Varname=value: Scope entire bash process
Preceded by a letter or underscore. Consists of letters, numbers, or underscores, and the uppercase and lowercase letters have different meanings. Variable name length No Limit
When using variable values, precede the variable name with the prefix "$"
Local variables:
Local Varname=value: Scope is the current code snippet
Variable assignment: The assignment number "=" should have no spaces on either side.
[[email protected] ~]# a= ' Date '
[Email protected] ~]# echo $A
Fri APR 17:18:46 CST 2015
Path= $PATH:. Add current path to environment variable--unsafe
[[email protected] ~]# P=${path} assigns the value of the PATH variable to P
[Email protected] ~]# echo $P
[[email protected] ~]# t=$ (date) command--action
[[email protected] ~]# t= ' Date ' ditto
[Email protected] ~]# echo $T
The execution results in the script are not transmitted outside, it is independent
The script executes with a new child shell, Independent
You can use variables and other characters to compose a new string.
[Email protected] ~]# mydir=/home/test01
[Email protected] ~]# echo "$MYDIR/ttt"
/home/test01/ttt
[Email protected] ~]# echo Today is $DAYday
Today is
[Email protected] ~]# echo Today is $DAY day
Today is Mon day
[Email protected] ~]# echo Today is ${day}day
Today is Monday
List all the variables:
Set command
[[Email protected] ~]# Set | grep Day
Day=mon
Assign multiple words to a variable:
[Email protected] ~]# name= "Hh-ll"
[Email protected] ~]# echo $NAME
Hh-ll
[[email protected] ~]# name= ' gg KK '
[Email protected] ~]# echo $NAME
GG KK
[Email protected] ~]# name= "JJ UU $NAME"
[Email protected] ~]# echo $NAME
JJ UU HH LL
[Email protected] ~]# name= ' JJ uu $NAME '
[Email protected] ~]# echo $NAME
JJ UU $NAME
The difference between single and double quotation marks:
The contents of the single quotation mark are assigned unchanged to the variable.
Double quotation marks cancel the function of the space, and the meaning of the special symbol is preserved.
Positional variables: When the shell interprets the execution of a user's command,
Use the first word of the command line as the command name, and other words as arguments
。 The parameters that are determined by the location that appears on the command line are called positional parameters.
Positional variables: Using $n to represent
$1,$2,....
[Email protected] test]#/example.sh file1 file2 file3
$ A file name for this program example.sh
$n the nth parameter value of this program, N=1..N
Special variables:
Some variables are set at the beginning of script execution and cannot be modified, but we do not call it read-only
System variable, and it is called a special variable. These variables are available when a program is executed.
The following are some of the variables:
$?: previous command execution status return value:
After the program executes, there may be two types of return values
Execution results of the program
Program Status return codes (0-255)
0: Correct execution
1-255: Error execution. 1,2,127 System Pre-
$* all parameters of this program
$# the number of parameters for this program
$$ the PID of this program
$! Perform the PID of the previous daemon
[email protected]: parameter list for this program
Reference variable: ${varname} {} can sometimes be omitted
Undo Variable:
Unset VARNAME
View variables in the current shell
Set
View environment variables in the current shell
Printenv
Env
Eport
Special variables, positional variables cannot be viewed
===========================================================================
Read command:
Function: Read data from the keyboard, assign to variables
[email protected] ~]# read a b C
8 9 12
[Email protected] ~]# echo $a $b $c
8 9 12
Using in shell scripts
[email protected] ~]# read a B
1 2 3
You have new mail in/var/spool/mail/root
[Email protected] ~]# echo $b
2 3
[email protected] ~]# read a b C
1 2
[Email protected] ~]# echo $c
Expr command
Function: The arithmetic operation of the shell variable:
Expr command: Arithmetic operations on integer variables
Syntax: There are spaces between expr expressions #注意 operators
+: Expr 3 + 5
-:expr $var 1-5
*:expr $var 1 \* $var 2
/: Expr $var 1/$var 2
Complex operations:
Var4=8
Expr ' expr 5 + one '/$var 4
How do I make conditional judgments in bash?
Condition Test Type:
Integer test
Character test
File test
Expressions for Conditional tests:
[Expression]
[[Expression]
Integer comparison:
-EQ: Tests whether two integers are equal
$A-eq $B
echo $?
0 or 1
-ne: Test for two integers: unequal to true, equal to False
-GT: Tests if one number is greater than the other: greater than true
-LT: Tests whether one number is less than the other: less than true
-ge: greater than or equal to
-le: Less than or equal to
String test:
= =: equivalent comparison, etc is true
! =: Unequal comparison, not equal to True
: Big
<: Small
-N string to test whether the specified string is empty, empty is true, and not empty is false
-S string test Specifies whether the string is not empty, true, empty is false
File test:
-E File: whether the files exist
-F File: Test files are normal files
-D FILE: Tests whether the specified path is a directory
-R File: Tests whether the current user has read access to the specified file
-W FILE: ......... ............ Write permissions
-X FILE: ........ .......... Execute permissions
-S file: Test whether the files are empty
The logical relationship between commands
Logic with:&& or-a
When the first condition is false, the second condition is no longer judged and the final result is already there;
When the first condition is true, the second condition must be judged again
ID ylion &>/dev/null && echo "Hello Ylion
! ID user1 && Useradd user1
Logical OR: | | Or-O
Logical OR. Two conditions have a set up, the result is true
ID User1 | | Useradd user1
Non -:!
Take counter
===============================================================
Process Control
Order
Choose
Cycle
Select the IF statement for the structure
Single Branch if statement
if judgment condition; Then
Statement1
Statement2
.........
Fi
Dual-Branch If statement
if judgment condition; Then
Statement1
Statement2
....
Else
Statement3
Statement4
....
Fi
Multi-Branch If statement:
if judgment condition 1; Then
Statement1
....
Elif judgment Condition 2; Then
Statement2
.....
Elif judgment Condition 3; Then
Statement3
.....
Else
Statement4
......
Fi
How to perform arithmetic operations in the shell
1.let Arithmetic operation expressions
Let c= $a + $b
Echo $c
2.$[arithmetic operation expression]
c=$[$a + $b]
3.$ ((arithmetic expression))
c=$ (($a + $b))
4.expr arithmetic expression expressions have spaces between operands and operators, and use a command reference
C= ' expr $a + $b '
Defining script Exit Status codes
Exit: Exit Script
Defining script Execution Status results
Exit # 0-255
If the script does not explicitly define the exit status code. Then the best way to execute the exit code for a command is the script exit status code
1. Pinpoint a valid IP address
2. Write a script to complete the following requirements
(1) Add 3 users Test1, Test2, Test3, and set a password. However, to determine whether the user exists or not exist, add
(2) shows how many users are in the current system
3. Write a script to complete the following requirements
Specify an existing user and save it with a variable
(1) If the UID is 0, the user is displayed as an administrator
(2) Otherwise, it is displayed as a normal user
Linux-shell script Programming-variables-arithmetic expressions-Judging Statements-if Branch statements