Article directory
1. Shell process control
- for loop
Similar to other programming languages, the shell supports for loops.
The general format of a for loop is:
- while statement
A while loop is used to continuously execute a sequence of commands, and is also used to read data from an input file. Its syntax format is:
The part of the loop is not difficult to understand, and you can use it now when you use it later.
2. Shell function
The linux shell can user-defined functions, and then they can be called casually in shell scripts.
The definition format of a function in the shell is as follows:
[ function ] funname [()]
{
action;
[return int;]
}
illustrate:
1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255
The following example defines a function and calls it:
demoFun(){
echo "这是我的第一个 shell 函数!"
}
echo "-----函数开始执行-----"
demoFun
echo "-----函数执行完毕-----"
The following defines a function with a return statement:
funWithReturn(){
echo "这个函数会对输入的两个数字进行相加运算..."
echo "输入第一个数字: "
read aNum
echo "输入第二个数字: "
read anotherNum
echo "两个数字分别为 $aNum 和 $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"
The return value of the function is obtained through $? after calling the function .
Note: All functions must be defined before use. This means that the function must be placed at the beginning of the script until it is first seen by the shell interpreter before it can be used. A function is called using only its function name .
- function parameters
In the shell, parameters can be passed to functions when they are called. Inside the function body, the value of the parameter is obtained in the form of $n , for example, $1 represents the first parameter, $2 represents the second parameter...
Example of a function with parameters:
funWithParam(){
echo "第一个参数为 $1 !"
echo "第二个参数为 $2 !"
echo "第十个参数为 $10 !"
echo "第十个参数为 ${10} !"
echo "第十一个参数为 ${11} !"
echo "参数总数有 $# 个!"
echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
Note that $10
the tenth parameter cannot be obtained, and ${10} is required to obtain the tenth parameter. When n>=10, you need to use ${n} to get the parameters.
In addition, there are several special characters used to process parameters:
3. Shell input/output redirection
Most UNIX system commands accept input from your terminal and send the output they produce back to your terminal. A command usually reads input from something called standard input, which happens to be your terminal by default. Likewise, a command usually writes its output to standard output, which is also your terminal by default.
The list of redirection commands is as follows:
output redirection
Redirection is generally achieved by inserting specific symbols between commands. In particular, the syntax of these symbols is as follows:
command1 > file1
The above command executes command1 and saves the output to file1.
Note that any existing content in file1 will be replaced by the new content. If you want to append new content at the end of the file, use the >> operator.
The command command comes from the English word "command", and its function is to call and execute the specified command. When the system defines the same function as the command at the same time, the command command ignores the Shell function and executes the corresponding Linux command.
Fourth, the Shell file contains
Like other languages, the shell can also contain external scripts. This can easily encapsulate some common code as an independent file.
The syntax format contained in the shell file is as follows:
. filename # 注意点号(.)和文件名中间有一空格
或
source filename
Learn from "https://www.runoob.com/linux/linux-shell-include-file.html"
Summarize
Schopenhauer's challenging life: Every move of each of us in life shows that he has a certain view of his own strength, ability, and from the beginning, he has no doubts about the difficulty or feasibility of his actions in any situation. Sexuality is already clearly understood. In other words, I am convinced that man's actions spring from his ideas. Please don't be surprised by this, because what our senses perceive is not the actual fact, but only a subjective image of it. That is, the reflection of the external world.