Centos 腳本基礎練習10

練習1
寫一個腳本,完成如下功能;
說明: 此腳本能於同一個repo文件中建立多個yum源的指向;
1, 接受一個文件名作爲參數,此文件存放至/etc/yum.repos.d目錄中,且文件名以.repo爲後綴;要求,此文件不能事先存,不然,報錯;
2, 在腳本中,提醒用戶輸入repo id; 若是爲quit; 則退出腳本; 不然繼續完成下面的步驟;
3, repo name 以及baseurl 的路徑,然後以repo文件的格式將其保存至指定的文件中;
4, enable默認爲1,而gpgcheck默認設定爲0;
5, 此腳本會循環執行多閃,除非用戶爲repo.d指定爲quit.
[root@localhost script]# cat lx35.sh
#!/bin/bash
REPOFILE="/etc/yum.repos.d/$1"
if [ -e $REPOFILE ]; then
echo "Mistake. run it again."
exit 7
else
read -p "Please input Repo-ID:" RID
fi
until [ $RID == 'quit' ]; do
echo "[$RID]" >> $REPOFILE
read -p "Reposname:" RNAME
echo "name=$RNAME" >> $REPOFILE
read -p "Reposd Baseurl:" RURL
echo "baseurl=$RURL" >> $REPOFILE
echo -e "enable=1\ngpgcheck=0" >> $REPOFILE
read -p "Please input Repo-ID:" RID
donebash

練習2
寫一個腳本,完成以下功能:ide

說明:此腳本可以爲指定網卡建立別名,則指定地址;使用格式如:mkethalias.sh -v|--verbose -i ethXui

一、-i選項用於指定網卡;指定完成後,要判斷其是否存在,若是不存在,就退出;url

二、若是用戶指定的網卡存在,則讓用戶爲其指定一個別名,此別名能夠爲空;若是不空,請確保其事先不存在,不然,要報錯,並讓用戶從新輸入;code

三、在用戶輸入了一個正確的別名後,請用戶輸入地址和掩碼;並將其配置在指定的別名上;ip

四、若是用戶使用了-v選項,則在配置完成後,顯示其配置結果信息;不然,將不顯示;
lx34.sh
#!/bin/bash
case $# in
'2')
if [ $1 != '-i' ]; then
echo "Mistake! Please run it again"
exit 9
fi
if ! ifconfig -a | grep "$2" &> /dev/null; then
echo "The $2 is not exist."
exit 10
else
read -p "Please input alias:" BM
fi
until ! ip addr show | grep "$BM" &> /dev/null; do
echo "The alias $BM has been."
read -p "Please input alias:" BM
done
read -p "Please input IP/MASK:" IP
if [ $BM = '' ]; then
ip addr add $IP dev $2
else
ip addr add $IP dev $2 label $BM
fi
;;
'3')
if [ $2 != '-i' -o $1 != '-v' ]; then
echo "Mistake! Please run script again."
exit 10
fi
if ! ifconfig -a | grep "$3" &> /dev/null; then
echo "No $3 device."
exit 11
else
read -p "Please input another name:" BM
fi
until ! ip addr show | grep "$BM" &> /dev/null; do
echo "$BM HAS been."
read -p "Please input another name:" BM
done
read -p "Please assine IP/MASK:" IP
if [ $BM = ' ' ]; then
ip addr add $IP dev $3
else
ip addr add $IP dev $3 label $BM
fi
ip addr show
;;
*)
echo "Please try it again."
exit 12
;;
esacinput

相關文章
相關標籤/搜索