shell函數實例

shell函數

shell函數說明

在shell腳本當中,函數的應用是非常常見的,也是很有效的。對於常用的功能,我們可以編寫成一個函數,然後自行調用,通過這種方式能提高代碼的可讀性和整體的關聯性。

在linux系統本身的腳本文件中,存在着大量的循環、嵌套、函數的應用,各位可以在瞭解基本的循環和shell函數語法結構之後,去嘗試看看系統文件的腳本編寫,有助於提高運維效率。

語法

方法1:

從命令行寫shell函數,函數主體中行末需要添加分號";"

[root@m01]#function fun { echo "hello shell"; }
[root@m01]#fun
hello shell
[root@m01]#

方法2:

在腳本中編寫shell函數 function func01 { 函數主體 }

[root@python3 func]# sh func_hello.sh 
hello shell
這是我的第一個函數
[root@python3 func]# cat func_hello.sh 
#!/bin/bash
function func01 {
  echo "hello shell"
  echo "這是我的第一個函數"
}
func01
[root@python3 func]# 

方法3:

在腳本中編寫shell函數 func01() { 函數主體 }

[root@python3 func]# sh func_hello.sh 
hello shell
這是我的第一個函數
[root@python3 func]# cat func_hello.sh 
#!/bin/bash
func02() {
  echo "hello shell"
  echo "這是我的第二個函數"
}
func02

函數的調用

#將shell函數文件添加到/etc/bashrc文件末尾,然後在命令行直接使用函數名稱來調用函數,就像執行系統命令一樣,且可以使用$1-$10這樣的系統變量。

[root@python3 func]# tail /etc/bashrc 
......
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
fi
# vim:ts=4:sw=4
. /root/func/func_hello.sh
[root@python3 func]# . /etc/bashrc
[root@python3 func]# func01 
hello shell
這是我的第一個函數
[root@python3 func]# 

普通腳本中也可以調用定義了函數的庫文件,並且在腳本中直接使用該函數庫中的所有函數

[root@python3 func]# cat func_hello
func01() {
  echo "hello shell"
  echo "這是我的第一個函數"
}

[root@python3 func]# cat test.sh 
#!/bin/bash

. /root/func/func_hello
func01
[root@python3 func]# sh test.sh 
hello shell
這是我的第一個函數
[root@python3 func]# 

函數的傳參

函數的傳參和給腳本傳參類似,可以直接接受腳本傳入的參數,也可直接給函數傳參,通過下面的實列說明函數的傳參問題

從命令行執行函數,可以像使用linux命令一樣,接收參數。

[root@python3 func]# func02 1 4
1 * 4 = 4
[root@python3 func]# cat func_hello
#!/bin/bash

func01() {
  echo "hello shell"
  echo "這是我的第一個函數"
}

function func02 {
 echo "$1 * $2 = $[ $1 * $2 ]" 
}
[root@python3 func]# func02 6 4
6 * 4 = 24
[root@python3 func]# 

在腳本中調用函數庫文件,並且執行其中的函數,函數可通過獲取而腳本的參數來給自己傳參

[root@python3 func]# sh test.sh 4 5
通過在腳本中 test.sh 引入函數庫文件,來掉用shell函數
hello shell
這是我的第一個函數
通過調用腳本的參數來傳參: 4 5
4 * 5 = 20
調用腳本的變量
[root@python3 func]# cat test.sh 
#!/bin/bash

. /root/func/func_hello

echo "通過在腳本中 $0 引入函數庫文件,來掉用shell函數"

func01

echo  "通過調用腳本的參數來傳參: $*"

func02  $1 $2
[root@python3 func]# cat func_hello
#!/bin/bash

func01() {
  echo "hello shell"
  echo "這是我的第一個函數"
}

function func02 {
  echo "$1 * $2 = $(($1 * $2))" 
  echo "調用腳本的變量"
}
[root@python3 func]# 

shell函數應用

跳板機

#!/bin/bash
hostlist(){

	echo "散列主機:"
	echo -e "\t<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
	while read line
	do
		echo -e "\t|        "$line
	done<hostlist.txt
	echo -e "\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
}

memuinfo(){
cat <<EOF
        ---------------------------------
        |       1) 主機目錄             |
        |       2) 使用手冊             |
        |       3) 遠程登陸             |
        |       h) help                 |
        ---------------------------------
EOF
}
OperInst(){
cat <<EOF
#本次跳板機只是引用shell函數做的小實驗,請勿太在意其實用性,操作說明如下:
#1.主機列表在單獨的一個文件中,通過一個函數來讀取文件內容,並打印在屏幕中,提供管理員一個簡單手抄作用
#2.使用手冊簡單的對jumphost.sh腳本的執行過程和功能做簡單的介紹
#3.此腳本的主要功能就是通過SSH遠程登陸已經對該跳板機免密登陸的主機
#4.如果您的操作出現了異常情況,請輸入"h"來獲得答案
#
#腳本功能說明:
#1.本次實驗會更多的通過函數的調用的方式來完成各項功能,體現shell函數在shell腳本中的作用,比如:
#hostlist:     表示主機列表
#memuinfo:      功能菜單
#OperInst:     調用您看到的功能說明
#Help:          幫助你退出無聊的循環單中
EOF
}

Login(){
read -p $'請輸入您需要遠程的主機IP地址\n#'  IP
read -p "user login:" User
read  -s -p "password:" Pass
sshpass -p $Pass ssh $User@$IP -o StrictHostKeyChecking=no
}

Help(){
echo "歡迎您不厭其煩的來到這裏,我來幫助你退出無聊的程序"
Mail=$(rpm -qa |grep mailx;echo $?)
if [ $Mail -ne 0 ]
then
	echo "安裝mail插件中..."
	yum -y install mailx
	read -p $'請輸入您對該腳本的意見和建議\n>>>' text
	echo $text|mail -s test [email protected]
	exit
else
	read -p $'請輸入您對該腳本的意見和建議\n>>>' text
	echo $text|mail -s test [email protected]
	exit
fi
}

sshpass_install(){
num=$(rpm -qa |grep "sshpass";echo $?)
if [ $num -ne 0 ]
then
echo "正在安裝所需要的插件: sshpass"
yum -y install sshpass
fi
}

	memuinfo
	trap "" HUP INT TSTP
while true
do
        read -p "請選擇目錄中的功能{ 1 | 2 | 3 | h }: " action
	case $action in
		3)
			sshpass_install
			read -p "您已經選擇執行遠程連接{ yes | no }" action
			
			if [ $action == "yes"  ];then
				Login
			fi
       			continue
		;;
		2)
			clear
			OperInst
			echo -e "\n"
			memuinfo
		;;
		1)
			clear
			hostlist
			echo -e  "\n"
			memuinfo
		;;
		h)
			Help
			echo -e "\n"
			memuinfo
		;;
		exec)
			exit
		;;
		*)
			continue
	esac

done

#主機列表文件:hostlist.txt,請自行編寫一個主機列表文件

忠告:shell腳本如果需要數量掌握,離不開您對linux命令的熟悉,該怎麼做你懂的,加油!
在此歡迎您發表您的看法,咱們一起學習。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章