loop control and functions
Break loop control, continue loop control.
Loop control character
The break loop control can be used to exit the entire loop;
After exiting the loop, the following loops are executed, using the Continue loop control;
#!/bin/bash
While True
Do
echo "Please enter a operation {1copy| 2delete|3backup|4exit} "
Read-p "Please enter Youroperation:" Op
Case $op in
1)
Continue
echo "Copy"
;;
2)
echo "Delete"
;;
3)
echo "Backup"
;;
4)
echo "Exit"
;;
*)
echo "error, try enter {1 | 2 |3 |4} again "
Esac
Done
function explanation
Basic knowledge of the definition of 1-2-1-functions
Invocation of the 1-2-2-function
Basic knowledge of function definition
If you run into a long path, make a variable.
Encounter a lot of repetitive operations, do a function can
Basic format of the function:
Name ()
{
Command1
Command2
...
}
Nginx ()
{
Netstat-anlpt | grep nginx
}
The title is the name of the function, the function body is a collection of commands within the function, in writing the script to pay attention to the title of the unique
An error occurs when the execution of a non-unique script
function does not allow empty commands-the collection of commands in the body of the function must contain at least one command
Define a function:
#!/bin/bash
Student () #定义函数
{
echo "Hello shell!" #函数中的命令
}
echo "Now going to run student ()"
Student
echo "End the student ()"
Effect:
[Email protected] shell]# sh for1.sh
Now going to run student ()
Hello shell! #函数的执行结果
End the student ()
Comments:
A simple function is defined in hanshu.sh
First execute echo "Start function student"
Execute the Student function
Finally execute echo "function Student execution End"
Functions, like scripts, are executed sequentially.
Student () { à function named student} represents the end
A reference to a parameter is passed
Environment:
Create A.txt
[email protected] shell]# Touch a.txt
To write a script:
Vim yinyong.sh
#!/bin/bash
Delete ()
{
RM-RF $
Mkdir$2
}
Delete/root/shell/aa.txt/root/shell/mydir
Perform
[Email protected] shell]# sh yinyoug.sh
[Email protected] shell]# ls-ld mydir/
Drwxr-xr-x. 2 root root 6 Sep 5 21:53 mydir/
Functions that call other modules
Functions that source calls other scripts
Define a module with different functions
[email protected] shell]# cat option.sh
#!/bin/bash
Delete ()
{
RM-RF $del
}
Copy ()
{
CP-FR $sdir $tdir
}
Backup ()
{
TARZCVF $tar _name $star _dir &>/dev/null
}
Quit ()
{
Exit
}
Edit a file again
#!/bin/bash
source/root/shell/option.sh
While True
Do
Cat <<eof
******************************
Thefollowing is optional
******************************
1) Copy
2) Delte
3) Backup
4) Exit
******************************
Eof
Read-p "Please enter youroptions:" option
Case $option in
1)
Read-p "Please input your want to copy the source file:" Sdir
Read-p "Please input your target directory:" Tdir
Copy
;;
2)
Read-p "Please input your target directory:" Del
Delete
;;
3)
Read-p "Please enter your backup file names:" Tar_name
Read-p "Please enter your backup file:" Tar_dir
Backup
;;
4)
Quit
Break
;;
*)
echo "$option is not optional operation"
Esac
Done
Shift Move Left
$ $4
$ $
Each time the parameter sequence moves left one position,
10 11 12 13
$ $4
#!/bin/bash #如果位置参数小于等于0
If [$#-le 0]
Then
echo "Error! Not enough parameters "#输出报错的信息
Exit 124
Fi #退出返回一个值
#定义一个sum变量
Sum=0
#如果位置参数大于0, it is established;
While [$#-GT 0]
Do
#运算 $sum +$1 and assignment to the variable sum
sum=$ (expr $sum + $)
#参数序列顺次左移一个位置
Shift
Done
Echo $sum
This article is from the "Technical Achievement Dream" blog, please be sure to keep this source http://andyliu.blog.51cto.com/518879/1846584
Linux Shell loop control characters and functions