1. 程式人生 > >bash腳本進階練習題

bash腳本進階練習題

簡單的練習題

1、編寫腳本/root/bin/createuser.sh,實現如下功能:使用一個用戶名做為參數,如果指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息

[ $# -lt 1 ] && echo "input username !" && exit

id $1 &> /dev/null

if [ $? -eq 1 ];then

useradd $1 ;echo -e "successful\n`id $1`"

else echo "user already exists"

fi

2、編寫腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,並判斷用戶輸入的是yes還是no,或是其它信息

read -p "yes or no " answer

if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]];then

echo "your anwser is yes"

elif [[ $answer =~ ^[Nn][Oo]?$ ]];then

echo "your anwer is no"

else

echo "other"

fi

3、編寫腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其它文件類型)

[ $# -ne 1 ] && echo "input file path"&&exit 1

if [ -d $1 ] ;then

echo dir

elif [ -h $1 ] ;then

echo link

elif [ -f $1 ] ;then

echo file

elif [ ! -e $1 ] ;then

echo not exist

else echo other

fi

4、編寫腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否為正整數

read -p "input positive integer " num

if [[ $num =~ ^[1-9][0-9]*$ ]] ;then

echo right

else

echo "error"

fi


1、判斷/var/目錄下所有文件的類型

for i in `ls -A /var/` ; do

echo "$i -- `filetype15.sh /var/$i`"

done

2、添加10個用戶user1-user10,密碼為8位隨機字符

> ./user.log

for i in {1..10} ;do

useradd user$i

mima=`openssl rand -base64 10|head -c8`

echo $mima|passwd --stdin user$i

echo "user$i $mima" >>./user.log

done

3、/etc/rc.d/rc3.d目錄下分別有多個以K開頭和以S開頭的文件;分別讀取每個文件,以K開頭的輸出為文件加stop,以S開頭的輸出為文件名加start,如K34filename stop S66filename start

for i in `ls /etc/rc.d/rc3.d` ;do

if [[ $i =~ ^K.*$ ]];then

echo "$i start"

elif [[ $i =~ ^S.*$ ]];then

echo "$i stop"

fi

done

4、編寫腳本,提示輸入正整數n的值,計算1+2+…+n的總和

read -p "input a number " i

if [[ $i =~ [1-9][0-9]* ]];then

for j in `seq $i`;do

let sum+=j

done

echo $sum

else

echo error

fi

5、計算100以內所有能被3整除的整數之和

for i in `seq 100`;do

let sum=i%3

if [ $sum -eq 0 ];then

let add=add+$i

fi

done

echo $add

6、編寫腳本,提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態

read -p "input ipv4 address " ip

if [[ $ip =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]] ;then

nip=`echo $ip|cut -d. -f1-3`

for i in {1..254};do

ping -w1 -c1 $nip.$i &>/dev/null && echo "$nip.$i is up"|| echo "$nip.$i is down"&

done

wait

else

echo "error"

fi

7、打印九九乘法表

for i in `seq 9`;do

for j in `seq $i`;do

let k=$i*$j

echo -e "$j*$i=$k \c"

done

echo

done

8、在/testdir目錄下創建10個html文件,文件名格式為數字N(從1到10)加隨機8個字母,如:1AbCdeFgH.html

for i in `seq 10`;do

num=`cat /dev/urandom |tr -dc "[:alpha:]"|head -c8`

mkdir /app/$i$num.html

done

9、打印等腰三角形

read -p "input mun " t

if [[ $t =~ ^[1-9][0-9]*$ ]];then

for n in `seq $t`;do

let kmun=$t-$n

let smun=2*$n-1

for i in `seq $kmun`;do

echo -e " \c"

done

for i in `seq $smun`;do

echo -e "*\c"

done

echo

done

else

echo error

fi

10.國際象棋

for i in `seq 4`;do

for j in `seq 4`;do

for a in `seq 4`;do

echo -e "\033[46m \033[0m\c"

echo -e "\033[43m \033[0m\c"

done

echo

done

for k in `seq 4`;do

for b in `seq 4`;do

echo -e "\033[43m \033[0m\c"

echo -e "\033[46m \033[0m\c"

done

echo

done

done


bash腳本進階練習題