1.shell函數(shù)
1.1 函數(shù)的作用
1.命令合集,完成特定功能的代碼塊
2.在shell中定義函數(shù)可以使用代碼模塊化, 便于復用代碼,加強可讀性
3.函數(shù)和變量類似, 先定義才可調(diào)用,如果定義不調(diào)用則不會被執(zhí)行
1.2 函數(shù)的語法介紹
推薦使用第三種
function 函數(shù)名() {
指令集.....
}
function 函數(shù)名 {
指令集.....
}
函數(shù)名() {
指令集.....
}
1.3 簡單實踐
調(diào)用函數(shù)非常簡單, 只需要在腳本中寫入函數(shù)的名稱即可
簡單函數(shù)
haoge() {
echo "I am boy."
}
function yuyu {
echo "I am girl."
}
test() {
echo "Hello world."
}
oldboy
oldgirl
帶參數(shù)的函數(shù)編寫執(zhí)行
[root@shell scripts]# cat test.sh
#!/bin/sh
haoge() {
echo "I am $1"
}
haoge haoge
[root@shell scripts]# sh test.sh
I am haoge
將腳本傳參轉(zhuǎn)為函數(shù)傳參
$1 傳入腳本后第一個參數(shù)
$1 在函數(shù)中表示函數(shù)的傳參,在函數(shù)外表示腳本的傳參,可以將腳本傳參轉(zhuǎn)為函數(shù)傳參
[root@shell scripts]# cat test.sh
#!/bin/sh
haoge() {
echo "I am $1"
}
haoge $1
[root@shell scripts]# sh test.sh haoge
I am haoge
[root@shell scripts]# cat test.sh
#!/bin/sh
sum(){
a=0
num=$1
for i in `seq $num`
do
a=$[ $i + $a ]
done
echo "計算結(jié)果是: $a"
}
sum $1
sum $2
sum $3
[root@shell scripts]# sh test.sh 10 20 30
計算結(jié)果是: 55
計算結(jié)果是: 210
計算結(jié)果是: 465
函數(shù)的狀態(tài)值
return 返回指定函數(shù)退出狀態(tài)碼
[root@shell scripts]# cat test.sh
#!/bin/sh
fun2() {
echo 100
return 1
}
fun2
echo "函數(shù)的狀態(tài)碼是: $?"
[root@shell scripts]# sh test.sh
100
函數(shù)的狀態(tài)碼是: 1
1.4 系統(tǒng)自帶函數(shù)庫
/etc/rc.d/init.d/functions
系統(tǒng)本身會有自帶的函數(shù),我們也可以將自己定義的函數(shù)加到/etc/rc.d/init.d/functions中,在腳本中想使用/etc/rc.d/init.d/functions中的函數(shù)時只需要加載一下就可以啦
要在文件結(jié)尾strstr上面添加自己的函數(shù)
[root@shell scripts]# tail /etc/rc.d/init.d/functions
fi
fi
haoge(){
echo "I am $1"
}
strstr "$(cat /proc/cmdline)" "rc.debug" && set -x
return 0
測試一下
[root@shell scripts]# cat test.sh
#!/bin/sh
source /etc/rc.d/init.d/functions
haoge $1
[root@shell scripts]# sh test.sh haoge
I am haoge
1.5 練習:通過腳本傳參的方式,檢查 Web 網(wǎng)站 URL 是否正常
測試語句
wget --spider -T 5 -q -t 2 www.baidu.com
簡單版 不用函數(shù)
[root@shell scripts]# cat test.sh
#!/bin/sh
if [ $# -ne 1 ]
then
echo "Usage:$0 url"
exit 1
fi
wget --spider -t 2 -T 3 -q $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "$1 is ok"
else
echo "$1 is fail"
fi
[root@shell scripts]# sh test.sh 1
1 is fail
[root@shell scripts]# sh test.sh 1 2
Usage:test.sh url
[root@shell scripts]# sh test.sh www.baidu.com
www.baidu.com is ok
精確判斷網(wǎng)址(引入系統(tǒng) action 函數(shù)庫)
[root@shell scripts]# cat test.sh
#!/bin/sh
source /etc/rc.d/init.d/functions
usage(){
echo "Usage:$0 url"
exit 1
}
check_para(){
if [[ $1 =~ http://www.* ]]
then
:
else
echo "地址必須以 http://www開始"
exit 2
fi
}
check_url(){
wget --spider -t 2 -T 3 -q $1 &>/dev/null
if [ $? -eq 0 ]
then
action "$1 is ok" /bin/true
else
action "$1 is fail" /bin/false
fi
}
main(){ #<---主函數(shù)
if [ $# -ne 1 ]
then
usage
fi
check_para $1
check_url $1
}
main $*
測試一下
[root@mongodb scripts]# sh test.sh 114.114.114.114
地址必須以 http://www開始
[root@mongodb scripts]# sh test.sh www.baidu.com
地址必須以 http://www開始
[root@mongodb scripts]# sh test.sh http://www.baidu.com
http://www.baidu.com is ok [ 確定 ]
2. case結(jié)構條件句
2.1 case語句格式
case 變量 in
模式 1)
命令序列 1;;
模式 2)
命令序列 2;;
模式 3)
命令序列 3 ;;
*)
無匹配后命令序列
esac
中文形象語法
case "找老婆條件" in
家里有房子)
嫁給你
;;
家庭有背景)
嫁給你
;;
很努力工作)
先交往
;;
*)
再見!
esac
2.2 練習
簡單練習
執(zhí)行Shell腳本,打印一個如下的水果菜單:
1.apple
2.pear
3.banana
4.cherry
cat <<EOF
1.apple
2.pear
3.banana
4.cherry
EOF
read -p "請選擇一個數(shù)字:" num
case "$num" in
1)
echo "apple"
;;
2)
echo "pear"
;;
3)
echo "banana"
;;
4)
echo "cherry"
;;
*)
echo "請不要瞎輸入,選擇{1|2|3|4}"
exit 1
esac
輸出下面菜單
1.install MySQL
2.install Tomcat
3.exit
當用戶選擇對應的數(shù)字就開始安裝對應的服務(可用echo輸出安裝提示替代),需要對用戶輸入的數(shù)字進行判斷是否為整數(shù)。
echo '1.install MySQL
2.install Tomcat
3.exit'
read -p "請選擇一個數(shù)字:" num
[ -z "$num" ] && exit 1
expr $num + 99 &>/dev/null
if [ $? -ne 0 ]
then
echo "請輸入整數(shù)"
exit
fi
case "$num" in
1)
echo "install MySQL"
;;
2)
echo "install Tomcat"
;;
3)
echo bye
exit 1
;;
*)
echo "別瞎逼輸入,請輸入{1|2|3}"
esac
2.3 字體跟背景顏色
字體顏色
echo -e "\033[30m 黑色字haoge trainning \033[0m" #<==30m表示黑色字。
echo -e "\033[31m 紅色字haoge trainning \033[0m" #<==31m表示紅色字。
echo -e "\033[32m 綠色字haoge trainning \033[0m" #<==32m表示綠色字。
echo -e "\033[33m 棕色字haoge trainning \033[0m" #<==33m表示棕色字(brown),和黃色字相近。
echo -e "\033[34m 藍色字haoge trainning \033[0m" #<==34m表示藍色字。
echo -e "\033[35m 洋紅字haoge trainning \033[0m" #<==35m表示洋紅色字(magenta),和紫色字相近。
echo -e "\033[36m 藍綠色haoge trainning \033[0m" #<==36m表示藍綠色字(cyan),和淺藍色字相近。
echo -e "\033[37m 白色字haoge trainning \033[0m" #<==37m表示白色字。
背景顏色
echo -e "\033[40;37m 黑底白字haoge\033[0m" #<==40m表示黑色背景。
echo -e "\033[41;37m 紅底白字haoge\033[0m" #<==41m表示紅色背景。
echo -e "\033[42;37m 綠底白字haoge\033[0m" #<==42m表示綠色背景。
echo -e "\033[43;37m 棕底白字haoge\033[0m" #<==43m表示棕色背景(brown),和黃色背景相近。
echo -e "\033[44;37m 藍底白字haoge\033[0m" #<==44m表示藍色背景。
echo -e "\033[45;37m 洋紅底白字haoge\033[0m" #<==45m表示洋紅色背景(magenta),和紫色背景相近。
echo -e "\033[46;37m藍綠底白字haoge\033[0m" #<==46m表示藍綠色背景(cyan),和淺藍色背景相近。
echo -e "\033[47;30m 白底黑字haoge\033[0m" #<==47m表示白色背景。
2.4 案例 jumpserver簡單實現(xiàn)
trap命令
信號說明
HUP(1) 掛起,通常因終端掉線或用戶退出而引發(fā)
INT(2) 中斷,通常因按下Ctrl+C組合鍵而引發(fā)
QUIT(3) 退出,通常因按下Ctrl+組合鍵而引發(fā)
ABRT(6) 中止,通常因某些嚴重的執(zhí)行錯誤而引發(fā)
ALRM(14) 報警,通常用來處理超時
TERM(15) 終止,通常在系統(tǒng)關機時發(fā)送
SIGTSTP 停止進程 終端來的停止信號
[root@shell ~]# cat /server/scripts/jumpserver.sh
#!/usr/bin/bash
#jumpServer
Mysql=10.0.0.10
Nginx=10.0.0.6
Nginx1=10.0.0.7
Nginx2=10.0.0.8
meminfo(){
cat <<-EOF
-------------------------------
| 1) mysql |
| 2) Nginx |
| 3) Nginx1 |
| 4) Nginx2 |
| h) help |
---------------------------------
EOF
}
#調(diào)用函數(shù)打印菜單
meminfo
#控制不讓輸入ctrl+c,z;可以省略
trap "" HUP INT TSTP
while true
do
read -p "請輸入要連接的主機編號: " num
case $num in
1|mysql) #<---- 符號|表示或者
ssh root@$Mysql
;;
2|nginx)
ssh root@$nginx
;;
3|nginx)
ssh root@$nginx1
;;
h|help)
clear
meminfo
;;
exec)
break
;;
esac
done