Shell entry: Shell function and file redirection, shell redirection
Shell entry (5) Shell functions and file redirection Shell functions
Shell Function Definition:
[ function ] funname [()]{ action; [return int;]}
Description
You can use function fun () or fun () without any parameters. You can use return to return the parameter. If this parameter is not added, the result is run using the last command as the return value.
Eg1: simple functions
Define the fun1.sh File
#!/bin/bashechoFun(){ echo "hello world"}echo "fun start"echoFunecho "fun end"
Execute the fun1.sh File
./fun1.sh
Output:
fun starthello worldfun end
Eg2: return Function
Definition file fun2.sh
#!/bin/bashreturnFun(){ echo "input first num:" read firstNum echo "input second num:" read secondNum return $[ firstNum + secondNum ]}returnFunecho "$firstNum + $secondNum = $?"
Output:
[root@localhost zhang]# ./fun2.sh input first num:10input second num:2010 + 20 = 30
Function Parameters
In Shell, parameters can be passed to a function. Inside the function body, get the parameter value in the form of $ n. For example, $1 indicates the first parameter, and $2 indicates the second parameter.
Eg:
#!/bin/bashparamFun(){ echo "param1 : $1" echo "param2 : $2" echo "param4 : $4" echo "param10 : $10" echo "param10 : ${10}" echo "param count : $#" echo "param all : $*"}paramFun 1 2 4 8 16 32 64 128 256 512 1024
Output result:
param1 : 1param2 : 2param4 : 8param10 : 10param10 : 512param count : 11param all : 1 2 4 8 16 32 64 128 256 512 1024
Note: $10 cannot obtain 10th parameters. $ {10} is required to obtain 10th parameters }. When n> = 10 in $ n, use {n} to obtain the parameter.
Function parameters:
Parameter Processing |
Description |
$ # |
Number of parameters passed to the script |
$ * |
Display the parameters passed to the script with a single string |
$ |
ID of the current process that the script runs |
$! |
ID of the last process running in the background |
$ @ |
The value is the same as $ *, but the bootstrap number is added during use, and each parameter is returned in the Bootstrap. |
$- |
Displays the current options used by Shell, which has the same function as the set command. |
$? |
Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error. |
Shell input/output redirection
The redirection command list is as follows:
Command |
Description |
Command> file |
Redirect output to file |
Command <file |
Redirect input to file |
Command> file |
Redirects the output to file in append mode. |
N> file |
Redirects a file whose file descriptor is n to a file |
N> file |
Redirects an object whose file descriptor is n to a file in append mode. |
N> & m |
Merge output files m and n |
N <& m |
Merge input files m and n |
<Tag |
Enter the content between the start tag and end tag. |
Note: The file descriptor 0 is usually standard input, 1 is standard output, and 2 is standard error output.
Eg1: Output redirection command1> file
[root@localhost zhang]# echo "hello world" > file1.txt[root@localhost zhang]# cat file1.txthello world
Eg2: the output is appended. command> file
[root@localhost zhang]# echo "---shell---" >> file1.txt[root@localhost zhang]# cat file1.txthello world---shell---
Eg3: input redirection, command <file
[root@localhost zhang]# cat < file1.txthello world---shell---
Eg4: file descriptor output, n> file & n> file
File descriptors include 0, 1, and 2.
-0: Standard Input
-1: Standard output
-2: Standard Error output
Eg:
1> file & 1> file
[root@localhost zhang]# echo "abc" 1> file2.txt[root@localhost zhang]# cat file2.txtabc[root@localhost zhang]# echo "def" 1>> file2.txt[root@localhost zhang]# cat file2.txtabcdef
2> file & 2> file
[root@localhost zhang]# echo "error" 2> file2.txterror[root@localhost zhang]# cat file2.txt[root@localhost zhang]# echo "error 2" 2>> file2.txterror 2[root@localhost zhang]# cat file2.txt [root@localhost zhang]#
Eg: <tag
#!/bin/bashcat << ---abcdefghijklmnopqrstuvwxyz---
Output result:
[root@localhost zhang]# ./file4.sh abcdefghijklmnopqrstuvwxyz