This article is the fourth chapter of Learning the bash Shell 3rd Edition, Basic Shell Programming, but we will not limit it to this.
Run the shell script program
A script containing shell commands is a shell program, such as. bash_profile. We can create shell scripts in two ways: 1,Source
2. Press enter to enter the file name. This method is more convenient. We need to place the file in the command search path (learn in Linux Bash (6): Set Environment Parameters
Otherwise, you must specify the absolute path. For example, in the current directory, use./,
In addition, we also need to set the File Permission to an executable file, using$ Chmod + x
+ X indicates that the execution permission is added.
The two methods are different. Using source is the same as typing a command on terminal. In the latter way, the system runs a sub-process, which is copied to the shell, called subshell. The main shell waits until the sub-shell stops running. In addition, we set the sub-shell to run in the background, that is
&
.
This difference is also reflected inExport
For example, if we set aa = hello, world, this variable is valid in the command line, but is invalid in the script file, you need to useExport aa = 'hello, world
'
It is also valid in subprocess, that is, it can be used in script files.
Function Functions
There are two reasons for using the function: 1. The function is stored in the system memory, so the calling is more efficient. 2. It is better to organize long bash to make it modular. Define a function in two ways:
Function
Functname
{
Shell commands
}
Functname
(){
Shell commands
}
There is no difference between the above two methods. We can useUnset-fFunctname
To delete the function definition. When we define a function and store its name and definition in the memory, we can call it like calling a shell script. We passDeclare-f
To view the current defined function. If we only view the function name, useDeclare-F
. You can input a function in the command line to check whether the function is defined in a file. For example, in file a, you can useSource
Is effective. If we set a as an executable file, the function definition is only valid in this script. If we need to make it still valid, keep the environment, use $.
./
, Not just $./
.
As a whole, a Function is not divided into sub-processes. In addition, the priority of a Function is higher than that of a script. If there are duplicate names, the priority levels are as follows:
- Aliases
- Keywords, such as function, if,
- Functions
- Built-ins, such as cd and type
- Search scripts and executable files in the command PATH
If we need to check which command is used, useTypeName
For example, aa is alias (pwd), and we also define it as a function. According to the priority level, aa is alias, type aa, we can get aa is aliased to 'pwd', which can be usedType-a (or-all)Name
To view all meanings of aa. If you use a non-highest priority or redefine the priority order, in chapter 1 of the book, let's leave this topic empty. The above command will display the details, you can useType-tName
View type. alias | keyword | function | builtin | file is returned.Type-pName
Used to view the file path. If the type is not file, no results are returned.Type-PName
The file path is forcibly searched. For example, a duplicate name is alias and an executable file under the PATH directory. -P is not returned,-P is the absolute path of the returned file.
Location parameters
In script commands, parameters are sometimes included. These parameters can be obtained through location variables. For example, we have a script file named test, which is executed with parameters $./test param1 param2 param3. We can obtain the parameter value in the script. Use $ N, where $0 is. /test, which indicates the name of the script to be executed. The rest is the included parameter, $1 is param1, $2 is param2, and so on. If N is greater than the actual number of parameters, is empty. Generally, the location variable starts from $1.
$ *
A string composed of all parameters. In the preceding example, It is param1 param2 param3. The interval between these parameters is the first letter of IFS, that is, space. IFS includes tabs, spaces, line breaks, and other characters.
$ @
Equivalent to "$1" "$2"... "$ N ".
$ #
Indicates the number of parameters. In the preceding example, 3 is used.
These location variables are read-only and cannot be assigned values. $ * Is very similar to $ @. Generally, the output is the same. $ * Is separated by IFS characters. We can redefine the IFS characters, which will lead to different output results. We define in the scriptIFS =,
The output is separated by commas (,). The script is named a and runs. /a h1 h2, then $ # = 2, indicates there are two parameters, $ @ is h1 h2, $ * isH1, h2
The output results are different.
These location variables can also be used in the function and reflect the function parameters, that is, local, which belongs to the function, but $0 is an exception, which indicates the Script Name, this parameter is gobal. The following is an example. Script a contains the following content:
Hello ()
{
Var = "hello ";
Echo "hello"
Echo "Hello: Param num is $ #"
Echo "Hello: $ @"
Echo "Hello: $ *"
Echo "Hello: $0 $1 $2"
Echo "Hello: Var = $ Var ";
}
Var = "hello ";
Echo "Main: Param num is $ #"
Echo "Main: $ @"
Echo "Main: $ *"
Echo "Main: $0 $1 $2"
Echo "Main: Var = $ Var ";
Hello H1 H2
Run./a a1 a2 a3
Main: Param num is 3
Main: A1 A2 A3
Main: A1 A2 A3
Main:./A A1 A2
Main: Var = Main
Hello
Hello: Param num is 2
Hello: H1 H2
Hello: H1 H2
Hello:./A H1 H2
Hello: Var = Hello
Except $0, all location variables are local, that is, they are valid in the function. Similarly, other User-Defined variables are also local, that is, they are valid within the nearest. See the user variable var in the preceding example. We can add local to the var defined in the function to indicate that this is a local parameter. In the above example, the function hello can be used.LocalVar = "Hello"
.
Definition of variables to avoid ambiguity
In fact, we use $ varname, which is$ {Varname}
. Sometimes we cannot use abbreviations. For example, we need to get 10th parameters and use $10. This is actually the first parameter with "0" and ${10 }. For example, if we want to display the process number and only follow _, letters, or numbers, we use echo $ UID _ to see UID _ as a whole, that is, echo $ {UID _}. Therefore, it is empty. You need to use echo $ {UID} _. If the process number is 1000, It is 1000 _. If a variable is followed by _, letters, or numbers, use braces for safety.
Related links:
My articles on Linux operations