本節(jié)所講內(nèi)容
22.1? 跳出循環(huán)
22.2? shift? 參數(shù)左移指令
22.3? 函數(shù)的使用
22.4?? 實(shí)戰(zhàn)-shell腳本實(shí)戰(zhàn)
22.1? 跳出循環(huán)
在我們使用循環(huán)語(yǔ)句進(jìn)行循環(huán)的過(guò)程中,有時(shí)候需要在未達(dá)到循環(huán)結(jié)束條件時(shí)強(qiáng)制跳出循環(huán),那么shell為我們提供了兩個(gè)命令來(lái)實(shí)現(xiàn)該功能: break? 和continue??
22.1.1? break? 和continue??
Break?? :?? 跳出整個(gè)循環(huán)
Continue?? :? 跳出本次循環(huán),進(jìn)行下次循環(huán)
break:?


例1 :寫一個(gè)shell菜單,當(dāng)按下數(shù)字4時(shí)推出,否則一直循環(huán)顯示
①[root@localhost ~]# vim b.sh
②#! /bin/bash
if [ $# -le 0 ];then
? ? ? ? echo? 沒(méi)有足夠的參數(shù)
? ? ? ? exit
fi
sum=0
while [ $# -gt 0 ];
do
? ? #sum=(expr $sum + $1)
? ? ? ? sum=[$sum+$1]
? ? ? ? shift
? ? # shift 2 一次移動(dòng)兩個(gè)參數(shù)
done
echo result? is $sum
③[root@localhost ~]# bash break-continue.sh
******************************
Please select your operation:
1 Copy
3 Backup
4 Quit
******************************
互動(dòng): 先演示一下效果,讓大家思考一下,這個(gè)腳本的思路,然后帶我寫
echo? 123456?? | passwd?? --stdin? mk?? 給mk?? 用戶輸入密碼
①[root@localhost ~]# vim adduser2.sh
②#! /bin/bash
while : # 條件一直為真
do
? ? ? ? ? ? ? ? read -p "please enter prefix & password & num:" pre pass num
? ? ? ? ? ? ? ? printf "user information
? ? ? ? ? ? ? ? *************************
? ? ? ? ? ? ? ? user prefix:$pre
? ? ? ? ? ? ? ? user password:$pass
? ? ? ? ? ? ? ? user number:$num
? ? ? ? ? ? ? ? ************************"
read -p "Are you sure ? [ y/n ]" action
if [ "$action" == "y" ];then
? ? ? ? break
fi
done
for i in $( seq $num)? #從i=1? 開(kāi)始,取到$num? 。seq? 表示1-$num??
# $(seq $num)? 等于?? `seq $num`?? 反引號(hào)
for i in $(seq ?$num)????# 從i =1開(kāi)始,取到 $num ?。 seq 表示 1-$num ?
#$(seq ?$num)等于`seq ?$num?` ??; $( 命令 ) ; ${ 變量 } ?; [ 表達(dá)式/條件 ] ?
do
? ? user=${pre}${i}
? ? id $user &> /dev/null? #? 正確的輸入到null中
if [ $? -ne 0 ];then
? ? ? ? useradd $user
? ? ? ? echo "$pass" | passwd --stdin $user &> /dev/null
? ? ? ? if [ $? -eq 0 ];then
? ? ? ? ? ? ? ? echo -e "\033[31m$user\033[0m creat"?? 用紅色來(lái)顯示用戶名
? ? ? ? fi
else
? ? ? ? echo "user $user exist"
fi
③please enter prefix & password & num:mkk 123456 5
user information
*************************
user prefix:mkk
user password:123456
user number:5
************************Are you sure ? [ y/n ]y
mkk1 creat
mkk2 creat
mkk3 creat
mkk4 creat
mkk5 creat
?22.2?? Shift? 參數(shù)左移指令

vim? shift.sh
#! /bin/bash
if? [ $# -le 0 ];then #? 當(dāng)前參數(shù)
? ? ? ? echo "沒(méi)有足夠的參數(shù)"
fi
sum=0
while [ $# -gt 0 ];
do
? ? ? ? sum=$[ $sum + $1 ]
? ? ? ? shift
#shift? 一次移動(dòng)兩個(gè)參數(shù)
done
echo result is $sum
22.3?? 函數(shù)的使用
函數(shù)是一個(gè)腳本代碼塊,你可以對(duì)它進(jìn)行自定義命名,并且可以在標(biāo)本任意位置使用這個(gè)函數(shù),啊喲使用函數(shù),只要使用這個(gè)函數(shù)名稱就可以了,使用函數(shù)的好處,模塊化,代碼可毒性強(qiáng)
22.3.1? 函數(shù)創(chuàng)建語(yǔ)法
?方法1?
function? name? {
commands?
}
注意?? :name是函數(shù)唯一的名稱
方法2:?
name? (){
commands
}
調(diào)用函數(shù)語(yǔ)法
函數(shù)名? 參數(shù)1? 參數(shù)2
調(diào)用函數(shù)時(shí) ,可以傳遞參數(shù),在函數(shù)匯總用$1,$2,$3? 來(lái)引用傳遞的參數(shù)
①[root@localhost ~]# vim fun1.sh
②
#! /bin/bash
function fun_1 {
? ? ? ? echo "aaaaa"
}
function fun_1 {
? ? ? ? echo "this? is? 2222222222222"
}
fun_1
③ [root@localhost ~]# bash fun1.sh
this? is? 2222222222222?? 就近原則
22.3.3? 返回值
使用return 命令來(lái)退出函數(shù)并返回特定的退出碼
①vim? fun1.sh?
②#! /bin/bash
function fun_1 {
? ? ? ? echo "this is function"
? ? ? ? ls /etc/passwd
? ? ? ? return 3
}
fun_1
③[root@localhost ~]# bash fun1.sh
this is function
/etc/passwd
④root@localhost ~]# echo $?
3
返回值?
使用return 命令來(lái)退出函數(shù)并返回特定的退出碼,后期根據(jù)退出碼來(lái)判斷是哪種錯(cuò)誤
比如開(kāi)發(fā)定義? 0?? 代碼成功?? 1? 代碼文件找不到?? 2? 代碼權(quán)限不足
注意:? 狀態(tài)碼的確定必須在在函數(shù)一結(jié)束就運(yùn)行return? 返回值: 狀態(tài)碼的取值范圍0? -255
互動(dòng): exit? 數(shù)字 和return? 數(shù)字的區(qū)別
exit 整個(gè)腳本退出,往回?cái)?shù)字? ;? return? 只是函數(shù)最后一行,燃火返回?cái)?shù)字,只能讓函數(shù)后面的命令不執(zhí)行,無(wú)法強(qiáng)制退出整個(gè)函數(shù)
22.3.4?? 把函數(shù)賦值給變量使用
例子:? 函數(shù)名就相當(dāng)于一個(gè)命令
1)vim??? fun-1.sh
2)#! /bin/bas
fun1(){
? ? ? ? read -p "Input a value:" va
? ? ? ? echo $[ $va*5 ]? ? ? ?
}
num=$(fun1)
echo current num is $num
3) [root@localhost ~]# bash fun1.sh
Input a value:2
current num is 10
22.3.5? 函數(shù)的參數(shù)傳遞
第一種:通過(guò)腳本傳遞參數(shù)給函數(shù)中的位置參數(shù)$1?
1)[root@localhost ~]# vim fun1.sh
#! /bin/bas
fun1(){
? rm -rf $1
}
fun1 $1
~? ? ? ?
2)[root@localhost ~]# bash fun1.sh? for-1.sh
第二種:調(diào)用函數(shù)時(shí)直接傳遞參數(shù)
[root@localhost ~]# touch a.txt
[root@localhost ~]# vim fun1.sh
#! /bin/bash
fun1(){
? rm -rf $1
}
fun1 /root/a.txt
[root@localhost ~]# bash fun1.sh
[root@localhost ~]# ls a.txt
ls: cannot access a.txt: No such file or directory
1)[root@localhost ~]# vim fun1.sh
2)[root@localhost ~]# vim fun1.sh
#! /bin/bash
fun1(){
? echo $[ $1*5 ]
? echo $[ $1*2 ]
}
fun1 5 2?? #? 直接傳兩個(gè)參數(shù)
2)[root@localhost ~]# bash fun1.sh? 測(cè)試
25
4
22.3.6? 函數(shù)中變量的處理?
函數(shù)使用的變量類型有兩種
局部變量
全局變量
1? 全局變量,默認(rèn)情況下,你在腳本中定義的變量都是全局變量,在你在腳本外面定義的變量在函數(shù)內(nèi)也可以使用
①②③[root@localhost ~]# vim fun1.sh
? echo $[ $1*5 ]
#! /bin/bash
function fun1(){
? num1=$[ var1*2 ]
}
read -p "input a num:" var1
fun1
echo the new value is:$num1
[root@localhost ~]# bash fun1.sh
input a num:12
the new value is:24
22.4?? 實(shí)戰(zhàn)-自動(dòng)備份mysql 數(shù)據(jù)庫(kù)腳本和nginx 服務(wù)啟動(dòng)腳本
22.4.1 自動(dòng)備份mysql 數(shù)據(jù)庫(kù)腳本
從centos7.0?? 開(kāi)始,系統(tǒng)中自帶的mysql 數(shù)據(jù)庫(kù)包? ,該名為mariadb???? 數(shù)據(jù)庫(kù)

首先安裝一下mariadb
?1) [root@localhost ~]# yum install -y mariadb mariadb-server
? 2)? [root@localhost ~]# rpm -qf /usr/bin/mysql
mariadb-5.5.64-1.el7.x86_64
啟動(dòng)數(shù)據(jù)庫(kù)
? 3)[root@localhost ~]# systemctl start mariadb.service
登錄mysql
mysqladmin ? -u? root password '123456' ?? 給root用戶配置一個(gè)密碼 ?? 123456
? 4) [root@localhost ~]# mysql -u root?? 直接登錄上去??? 默認(rèn)是沒(méi)有密碼的

創(chuàng)建數(shù)據(jù)庫(kù)和表

并插入數(shù)據(jù)

mysql 自動(dòng)化備份腳本
思路:
1 ,檢查一下運(yùn)行環(huán)境: 目錄是否存在,時(shí)間,權(quán)限,用戶
2?? 運(yùn)行要執(zhí)行的命令: 備份,到處數(shù)據(jù)
3? 把命令執(zhí)行過(guò)程中? 沒(méi)有用的文件刪除一下
4?? 彈出命令運(yùn)行成功的消息?
1)[root@localhost ~]# vim mysql-back-auto.sh
腳本?
#! /bin/bash
#auto backup mysql? ?
#Define? PATH? 定義變量
BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`? #? 備份的地址?
MYSQLDB=xuegod1? #備份的數(shù)據(jù)庫(kù)的名稱
MYSQLUSR=root
MYSQLPW=123456
#MYSQLPD? ? ? #mysql? 數(shù)據(jù)庫(kù)密碼
#must? use root user run script? 必須使用root? 用戶運(yùn)行,$UID? 系統(tǒng)變量?
if
? ? ? ? [ $UID -ne 0 ];then
? ? ? ? echo This script must use the root user! ! !
? ? ? ? sleep 2
? ? ? ? exit 0
fi
# Define? DIR? and? mkdir? DIR? 判斷目錄是否存在,不存在則新建
if
? ? ? ? [ ! -d $BAKDIR ];then
? ? ? ? mkdir -p $BAKDIR
else
? ? ? ? echo This is $BAKDIR exists
? ? ? ? exit
fi
# Use mysqldump? backup mysql 使用mysqldump? 備份數(shù)據(jù)庫(kù)
/usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW $MYSQLDB > $BAKDIR/${MYSQLDB}_db.sql
cd $BAKDIR; tar -czf ${MYSQLDB}_db.tar.gz *.sql
#? 查找備份目錄一下以.sql? 結(jié)尾的文件并刪除成功
find? $BAKDIR -type f? -name *.sql -exec rm -rf {} \;
#或者
#? 如果數(shù)據(jù)庫(kù)備份成功,則打印成功,并且刪除備份目錄30天以前的目錄
[ $? -eq 0 ]; echo "This `date +%Y-%m-%d` MySQL BACKUP is SUCCESS"
cd /data/backup/mysql/? &&? find .? -type d? -mtime +30 |xargs rm -rf
echo "The mysql backup successfully "
3)[root@localhost ~]# bash mysql-back-auto.sh
This 2020-04-15 MySQL BACKUP is SUCCESS
The mysql backup successfully

可以看到已經(jīng)成功


發(fā)現(xiàn)是有數(shù)據(jù)的
22.4.2?? nginx?? 服務(wù)器啟動(dòng)腳本???