Simple comprehension: Use many tools to simplify complex steps in a shell script
Frame: Must have notes, written by others can understand
Beginning: #! The/bin/bash means that the file is using bash syntax
To use this method to run a shell script, if the script itself has execute permissions, you need to give the script an ' x ' permission. In addition, when using the SH command to execute a shell script, it is possible to add the-X option to view the execution of the script so that we can debug the script where there is a problem
chmod +x first.sh #加权限
. /first.sh #执行
Date + "%y-%m-%d%h:%m:%s" #这个例子很实用, view time
Addition of 1.shell scripts
Read-p "Please input a number:" X
Read-p "Please input another number:" Y
sum=$[$x + $y]
echo "The sum of the numbers is: $sum"
The else of logic judgment in 2.shell scripts
If judgment statement; then
Command
Fi
For example:
Read-p "Please input your score:" A
if (($a <60)); Then
echo "You didn ' t pass the exam."
Fi
($a <60) This form, which is a unique format in shell scripts, with a parenthesis or no error, remember this format
With Else
If judgment statement; then
Command
Else
Command
Fi
For example:
Read-p "Please input your score:" A
if (($a <60)); Then
echo "You didn ' t pass the exam"
Else
echo "You pass the exam.good!"
Fi
With Elif
If judgment statement one; then
Command
Elif Judgment Statement two; then
Command
Else
Command
Fi
For example:
Read-p "Please input your score:" A
if (($a <60)); Then
echo "You didn ' t pass the exam."
Elif ($a >=60) && (($a <85)); Then
echo "good! You pass the exam. "
Else
echo "Very good! Your Socre is very high! "
Fi
Use Case to judge:
Case variable in
value1)
Command
;;
value2)
Command
;;
VALUE3)
Command
;;
*)
Command
;;
Esac
For example:
Read-p "Input a number:" N
a=$[$n%2]
Case $a in
1)
echo "The number is odd."
;;
0)
echo "The number is even."
;;
*)
echo "It ' s not a number!"
;;
Esac
The IF in a 3.shell script often determines whether a file is an attribute, such as a normal file or a directory, and if it has read and write execution permissions. There are several options that are commonly used:
-E: Determine if a file or directory exists
-D: Determine if the directory is not present and whether it exists
-F: Determine if the file is normal and exists
-R: Determine if the document has Read permissions
-W: Determine if Write permission is available
-X: Determine if executable
If you use the If judgment, the format is: if [-e filename]; Then
[Email protected] sbin]# if [-f/root/test.txt]; then echo OK; Fi
Ok
Note:-lt (less than),-GT (greater than),-le (less than equals),-ge (greater than or equal),-eq (equals),-ne (not equal)
Loops in 4.shell
The basic structure of the For loop:
For variable name in loop condition; do
Command
Done
For example:
For i in ' SEQ 1 5 '; Do
Echo $i
Done
While loop basic structure:
while condition; do
Command
Done
For example:
A=5
While [$a-ge 1]; Do
Echo $a
a=$[$a-1]
Done
You can replace the loop condition with a colon so that you can do a dead loop
While:; Do
Command
Sleep 3
Done
function calls in 5.shell
Function name () {
Command
}
For example:
function sum ()
{
SUM=$[$1+$2]
Echo $sum
}
Sum $
Note: The function must be written in the front, not in the middle or the last, because the function is to be called.
A preliminary analysis of shell scripts