Article reference PHP Chinese website
Table of contents
1. Introduction to the shell
The shell is a command line interpreter, which parses user commands into the actual execution of the operating system and realizes the interaction between the user and the operating system. When several commands need to be executed repeatedly, the commands can be combined and certain control statements can be added to become Shell script files, handed over to the shell for batch execution
2. first demo
-
Create the file vi test.sh
-
write output statement
echo "hello world" echo "first demo"
-
Authorize the script chmod +x ./test1.sh
-
Execute the script ./test1.sh
-
output
3. Shell variables
3.1 Variable definition
There can be no spaces between the variable name and the equals sign, which is probably not the case in any programming language you are familiar with. At the same time, the naming of variable names must follow the following rules:
- The first character must be a letter (az, AZ).
- There can be no spaces in between, and underscores (_) can be used.
- Punctuation cannot be used.
- Keywords in bash cannot be used (use the help command to view reserved keywords).
myname="huhaoming"
3.2 Using defined variables
To use a defined variable, just add a dollar sign before the variable name, such as:
myname="huhaoming"
echo $your_name
echo ${your_name} #{}可有可无,加花括号是为了帮助解释器识别变量的边界
Variables already defined, can be redefined
myname="tom"
echo $myname
myname="alibaba"
echo $myname
Read-only variables are modified with readonly
myname="tom"
readonly myname
myname="huhaoming"
delete variable
Use the unset command to delete variables, and the variables cannot be used again after being deleted. The unset command cannot delete read-only variables.
echo "huhaoming hello world"
myname="huhaoming"
readonly myname
echo $myname
unset myname
echo $myname
echo "huhaoming hello world"
myname="huhaoming"
echo $myname
unset myname
echo $myname
3.3 Shell strings
Restrictions on single-quoted strings:
- Any characters in single quotes will be output as they are, and variables in single quote strings are invalid;
- Single quotes cannot appear in a single quote string (not even after using escape characters for single quotes).
Advantages of double quotes:
- There can be variables in double quotes
- Escape characters can appear in double quotes
echo "huhaoming hello world"
myname="huhaoming"
str="Hello, I know your are \"$myname\"! \n"
echo $str
concatenate string
echo "huhaoming hello world"
myname="huhaoming"
greeting="hello, "$myname" !"
greeting_1="hello, ${myname} !"
echo $greeting $greeting_1
get string length
echo "huhaoming hello world"
myname="huhaoming"
echo ${
#myname}
extract substring
echo "huhaoming hello world"
myname="huhaoming"
echo ${myname:1:4}
3.4 Arrays
bash supports one-dimensional arrays (does not support multi-dimensional arrays), and there is no limit to the size of the array. Similar to the C language, the subscripts of array elements are numbered starting from 0. To obtain the elements in the array, use the subscript, which can be an integer or an arithmetic expression, and its value should be greater than or equal to 0.
define array
In the Shell, an array is represented by parentheses, and the elements of the array are separated by a "space" symbol. The general form of defining an array is: array name = (value 1 value 2 ... value n)
array_name=(value0 value1 value2 value3)
Separate definition
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen
read array
${数组名[下标]}
Use the @ symbol to get all the elements in the array, for example:
echo ${array_name[@]}
array length
# 取得数组元素的个数
length=${
#array_name[@]}
# 或者
length=${
#array_name[*]}
# 取得数组单个元素的长度
lengthn=${
#array_name[n]}
echo "huhaoming hello world"
myname=(0,1,2,3,4)
echo ${
#myname[*]}
echo ${
#myname[@]}
echo ${
#myname[1]}
4. Shell passing parameters
When executing a Shell script, pass parameters to the script, and the format for obtaining parameters in the script is: $n . n represents a number, 1 is the first parameter to execute the script, 2 is the second parameter to execute the script, and so on..., where $0 is the name of the executed file:
echo "huhaoming hello world"
echo "执行的文件名:$0"
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";
Several special characters are used to handle parameters:
Parameter handling | illustrate |
---|---|
$# | The number of arguments passed to the script |
$* | Displays all arguments passed to the script as a single string. For example, if "$*" is enclosed with """, all parameters will be output in the form of "$1 $2 … $n". |
$$ | The current process ID number the script is running under |
$! | The ID number of the last process running in the background |
$@ | Same as ∗, but use quotes and return each argument in quotes. Same as "*, but use quotes and return each argument within quotes. Like "∗ Same , but use quotes and return each argument in quotes . _ _ _ _ For example, if " @" is enclosed by """, use "$1" "2" ... " 2" ... "2"…Output all parameters in the form of " n". |
$- | Display the current options used by the Shell, which has the same function as the set command. |
$? | Display the exit status of the last command. 0 means no errors, any other value indicates errors. |
5. shell operator
|
| @ ∣ and @ | and@ ∣ Same as *, but used in quotes and returns each argument within quotes. For example, if "$@" is enclosed with """, use "$1" "2" ... " 2" ... "2"…Output all parameters in the form of " n". |
| $- | Display the current options used by the shell, which has the same function as the set command. |
| $? | Display the exit status of the last command. 0 means no errors, any other value indicates errors. |