shell训练营Day18
練習(xí)31
提示用戶輸入網(wǎng)卡的名字,然后我們用腳本輸出網(wǎng)卡的ip,需要考慮下面問題:
?
#!/bin/bash
ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/eth.list
while :
do
eths=cat /tmp/eth.list |xargs
read -p "Please input a if name(The eths is echo -e "\033[31m$eths\033[0m"): " eth
if [ -z "$eth" ]
then
echo "Please input a if name."
continue
fi
if ! grep -qw "$eth" /tmp/eth.list
then
echo "The if name is error."
continue
else
break
fi
done
if_ip()
{
ip add show dev $1 |grep ' inet ' |awk '{print $2}'|awk -F '/' '{print $1}' >/tmp/$1.txt
n=wc -l /tmp/$1.txt|awk '{print $1}'
if [ $n -eq 0 ]
then
echo "There is no ip address on the eth."
else
echo "The ip addreess is:"
for ip in cat /tmp/$1.txt
do
echo -e "\033[33m$ip\033[0m"
done
fi
}
if_ip $eth
練習(xí)32
寫一個腳本,實現(xiàn)如下功能:
?
#!/bin/bash
if [ $# -eq 0 ]
then
echo "當(dāng)前目錄下的文件是:"
ls .
else
for d in $@
do
if [ -d $d ]
then
echo "目錄$d下有這些子目錄:"
find $d -type d
else
echo "并沒有該目錄:$d"
fi
done
fi
練習(xí)33
定義一個shell函數(shù),能接受兩個參數(shù),滿足以下要求:
?
#!/bin/bash
if [ $# -ne 2 ]
then
echo "你必須要輸入兩個參數(shù),第一個參數(shù)是網(wǎng)址,第二個參數(shù)是目錄."
exit 1
fi
if [ ! -d $2 ]
then
while :
do
echo "你輸入的第二個參數(shù),并不是一個存在的目錄。是否要創(chuàng)建該目錄呢?(y|n): "c
case $c in
y|Y)
mkdir -p $2
;;
n|N)
exit 51
;;
*)
echo "請輸入y或者n."
continue
;;
esac
done
else
cd $2
wget $1
if [ $? -eq 0 ]
then
exit 0
else
echo "下載失敗."
exit 52
fi
fi
練習(xí)34
寫一個猜數(shù)字腳本,當(dāng)用戶輸入的數(shù)字和預(yù)設(shè)數(shù)字(隨機生成一個0-100的數(shù)字)一樣時,直接退出,否則讓用戶一直輸入,并且提示用戶的數(shù)字比預(yù)設(shè)數(shù)字大或者小。
#!/bin/bash
n=$[$RANDOM%101]
while :
do
read -p "請輸入一個0-100的數(shù)字:" n1
if [ -z "$n1" ]
then
echo "必須要輸入一個數(shù)字。"
continue
fi
n2=echo $n1 |sed 's/[0-9]//g'
if [ -n "$n2" ]
then
echo "你輸入的數(shù)字并不是正整數(shù)."
continue
else
if [ $n -gt $n1 ]
then
echo "你輸入的數(shù)字小了,請重試。"
continue
elif [ $n -lt $n1 ]
then
echo "你輸入的數(shù)字大了,請重試。"
continue
else
echo "恭喜你,猜對了!"
break
fi
fi
done
練習(xí)35
寫一個shell腳本,能實現(xiàn)如下需求:
?
#!/bin/bash
f=/tmp/user_number.txt
j_n()
{
while :
do
n=$[RANDOM%100]
if awk '{print $2}' $f|grep -qw $n
then
continue
else
break
fi
done
}
while :
do
read -p "Please input a username: " u
if [ -z "$u" ]
then
echo "請輸入用戶名."
continue
fi
done
轉(zhuǎn)載于:https://blog.51cto.com/12898947/2340030
總結(jié)
以上是生活随笔為你收集整理的shell训练营Day18的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 屏蔽控制台应用程序的窗口#pragma
- 下一篇: Datawhale编程——动态规划DP