The shell file contains
The shell can also contain external scripts in the following syntax format:
. FileName
Or
SOURCE filename
For example, create a two shell script.
Script 1:test1.sh
url = "Www.baidu.com"
Script 2:test2.sh
. ./test1.sh
echo "$url"
You can see the results by executing the test2.sh.
Shell input and output redirection
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 |
redirect files with file descriptor N to file |
N>>file |
Redirects files with file descriptor n appended to file |
N>&m |
Merges the output file m and n |
N<&m |
Merges the input file M and n |
<<tag |
Enter the content between tag tag and end tag as input |
Note: File descriptor 0 is typically standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR)
Redirect in-depth explanation
In general, each Unix/linux command will open three files when it is run:
- Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.
- Standard output file (stdout): StdOut file descriptor for 1,unix program defaults to stdout output data
- Standard error file (stderr): The stderr file descriptor for the 2,unix program writes an error message to the stderr stream.
By default, Command>file redirects stdout to File,command<file to redirect stdin to file.
If you want stderr to be redirected to file, you can write:
Command 2>file #2表示标准错误文件
Command 2>>file #追加 2 indicates a standard error file
If you want to redirect stdout and stderr to file after merging, you can write
Command > File 2>&1
Command >> file 2>&1
Shell functions
Format
[Function] funname [()]
{
Action
[Return int;] #return后跟数值n (0-255)
}
Example
Demofun () {
echo "This is my first Shell function"
}
echo "--start--"
Demofun
echo "--end--"
Example: With return
Funwithretun () {
echo "Enter first number"
Return Anum
echo "Enter a second number"
Return Bnum
Return $ (($aNum + $bNum))
}
Funwithreturn
echo "The sum of the two numbers entered is $?" #函数返回值在调用该函数后通过 $? To get
Example: function parameters
Funwithparam () {
echo "The first parameter is $" #$1 represents the first argument, and the value of $ is the second argument, obtaining a value greater than or equal to 10 parameters requires ${n}
echo "Fifth parameter is $ $"
echo "Tenth parameter is ${10}"
echo "14th parameter is ${14}"
echo "All Parameters $*"
}
Funwithparam 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Parameter handling |
Description |
$# |
The number of arguments passed to the script |
$* |
Displays all parameters passed to the script in a single string |
$$ |
The current process ID number for the script to run |
$! |
ID number of the last process running in the background |
[Email protected] |
Same as $*, but quoted when used, and returns each parameter in quotation marks |
$- |
Displays the current options used by the shell, same as the SET command function |
$? |
Displays the exit status of the last command. 0 means no error, and any other value indicates an error |
Shell Process Control
If Else
Format:
If condition
Then
Command1
Command2
Fi
If else-if Else
Format
If Condition1
Then
Command1
Elif Condition2
Then
Command2
Else
CommandN
Fi
For loop
Format
For Var in item1 item2 ... itemn
Do
Command1
...
CommandN
Done
Example
For loop in 1 2 3 4 5
Do
echo "The value is: $loop"
Done
While
Format
While condition
Do
Command
Done
Example
Int=1
while (($int <=5))
Do
Echo $int
Let "int++"
Done
Infinite loops
While:
Do
Command
Done
While True
Do
Command
Done
Until cycle
Until condition
Do
Command
Done
Case
Case value in
Mode 1)
Command1
Command2
...
CommandN
;;
Mode 2)
Command1
Command2
...
CommandN
;;
Esac
Jump out of the loop
Break
Continue
Shell Scripting Lesson II ·