1:利用if語句,根據你輸入的成績來定製輸出。
#!/bin/bash
read -p "Please input your score: " score
if [[ ! "$score" =~ ^[0-9]+$ ]];then
echo "Your score is not interger"
exit 1
fi
if [ $score -lt 60 ];then
echo "You need study hard"
elif [ $score -ge 60 -a $score -lt 80 ];then
echo "You score is just soso"
elif [ $score -ge 80 -a $score -le 100 ];then
echo "You score is very good"
else
echo "You score is invalid"
fi
2:寫一個彩票系統,判斷5個數字。
1 5 3 8 9
輸入 第一個值。
輸入 第二個值。
輸入 第三個值。
輸入 第四個值。
輸入 第五個值。
每一步在輸入完,都會判斷,如果中了,則有輸出結果:
第一次中輸出 : First blood
第二次中輸出 : Double kill
第三次中輸出 :Triple kill
第四次中輸出 :Quadra Kill
第五次中輸出 :Penta Kill 以及 ACE!
一次也不中輸出: Defeat!
#!/bin/bash
i=0
k=0
for n in {1..5}
do
ran=$[RANDOM%5]
read -p "第$n次輸入數字,好嗎: " number
if [ $number -eq $ran ];then
i=$[$i+1]
k=1
else echo "你錯了"
fi
if [ $k -eq 1 ];then
case $i in
1) echo "first blood";;
2) echo "double kill";;
3) echo "trible kill";;
4) echo "quadra kill";;
5) echo "penta kill ACE";;
esac
fi
done
if [ $i -eq 0 ];then
echo "defeat"
fi
~
3:利用case寫一個能判斷yes/no的腳本,(大小寫均能識別,yes九種可能,no四種可能)、
#!/bin/bash
read -p "請輸入yes|no: " q
case $q in
[Yy][Ee][Ss]|[Yy])
echo "yes";;
[Nn][Oo])
echo "no";;
*)
echo "請輸入正確的格式"
esac
4:批量創建user1至user10,要求設置隨機16位密碼,包含數字、大小寫字母、符號。並要求用戶使用密碼首次登錄後,強制設置新密碼。
#!/bin/bash
> /root/user.log
for i in {1..10};do
useradd user$i && echo user$i is created
password=$(cat /dev/urandom |tr -dc '0-9a-zA-Z!@_#?.,' |head -c 16)
echo user$i:$password >> /root/user.log
echo $password |passwd --stdin user$i &> /dev/null
passwd -e user$i &> /dev/null
done
5:使用♥,利用for,顯示出一個等腰三角形。
法一:
#!/bin/bash
read -p "請輸入三角形的高度: " num
for i in `seq $num`;do
for x in `seq 1 $[$num-$i]`;do
echo -e " \c"
done
for n in `seq 1 $[2*$i-1]`;do
#yanse=`tr -dc '1-6' < /dev/urandom | head -c 1`
#echo -e "\e[3"$yanse"m♥\e[0m\c"
echo -e "\e[34m♥\e[0m\c"
done
echo
done
法二:
#!/bin/bash
read -p "請輸入行數: " h
for (( i=1;i<=$h;i=$[$i+1] ))
do
for (( j=$[$h-$i];j>0;j-- ))
do
echo -ne ' '
done
for (( k=1;k<=$[2*$i-1];k++ ))
do
echo -n "♥"
done
echo
done
~
6:判斷/var/目錄下所有文件的類型
dc=0
lc=0
cc=0
bc=0
oc=0
zc=0
ls -l /var |grep -v total >/tmp/var_ftype.txt
while read lines
do
ftype=`echo $lines |awk '{print $1}' |cut -c 1`
case $ftype in
d) dname=`echo $lines |awk '{print $9}'`; echo "$dname is a Directory" ; let dc+=1;;
l) lname=`echo $lines |awk '{print $9}'`; echo "$lname is a Links of Soft " ;let lc+=1 ;;
c) cname=`echo $lines |awk '{print $9}'`; echo "$cname is a Character of file" ;let cc+=1;;
b) bname=`echo $lines |awk '{print $9}'`; echo "$bname is a Block file" ; let bc+=1;;
-) zname=`echo $lines |awk '{print $9}'`; echo "$zname is a common file" ; let zc+=1;;
*) echo "Others files"; let oc+=1
esac
done </tmp/var_ftype.txt
echo '-----------------------------'
echo -e "var目錄下普通文件數量: $zc\nvar目錄下子目錄數量:$dc\nvar目錄下鏈接文件數量:$lc\nvar目錄下字符類型文件數量: $cc\nvar
目錄下塊設備文件數量:$bc\n其他類型文件數量:$oc"
rm -f /tmp/var_ftype.txt
7、/etc/rc.d/rc3.d目錄下分別有多個以K開頭和以S開頭的文件;分別讀取每個文件,以K開頭的輸出爲文件加stop,以S開頭的輸出爲文件名加start,如K34filename stop S66filename start
#!/bin/bash
for i in $(ls /etc/rc.d/rc3.d/);do
if [[ $i =~ ^K.* ]];then
echo "$i stop"
elif [[ $i =~ ^S.* ]];then
echo "$i start"
fi
done
8、計算100以內所有能被3整除的整數之和
方法一:
echo {3..100..3} |tr ' ' + |bc
方法二:
#!/bin/bash
sum=0
for n in $(seq 1 100);do
i=$[$n/3]
y=$[3*$i]
if [ $y -eq $n ];then
sum=$[$n+$sum]
fi
done
echo $sum
方法三:
sum=0
for i in {1..100};do
if [ $[$i%3] -eq 0 ];then
let sum+=$i
fi
done
echo $sum
9、在/testdir目錄下創建10個html文件,文件名格式爲數字N(從1到10)加隨機8個字母,如:1AbCdeFgH.html
if ! [ -d /testdir ];then
mkdir /testdir &> /dev/null
fi
for i in {1..10};do
touch /testdir/$i`cat /dev/urandom |tr -dc [:alpha:] |head -c 8`.html
done
10、探測局域網內的主機
172.18.250-254.X
能ping通顯示並保存至/root/ip.log
要求並行探測提高效率。
/root/bin/ip.log
for i in {250..255};do
for n in {1..255};do
{
ping -c1 -w1 172.18."$i"."$n" &>/dev/null
if [ $? -eq 0 ];then
echo 172.18."$i"."$n" host is up |tee -a /root/bin/ip.log
fi
}&
done
done
11.打印國標象棋棋盤
#!/bin/bash
for xline in $(seq 1 8);do
for yline in $(seq 1 8);do
if [ $[$[$xline+$yline]%2] -eq 0 ];then
echo -e "\033[47m \033[0m\c"
else
echo -e "\033[41m \033[0m\c"
fi
done
echo
done
12:編寫一個自動恢復httpd進程的腳本
#!/bin/bash
while true;do
if killall -0 httpd &> /dev/null;then
:
else
systemctl restart httpd &> /dev/null
echo `date "+%F %T"` restart httpd >> /app/httpd.log
fi
sleep 5
done
13:使用一條命令,依次創建指定的用戶。如執行createuser.sh tom bob alice harry
法一:
#!/bin/bash
while [ $# -gt 0 ]
do
id $1 &>/dev/null
if [ $? -eq 0 ];then
echo $1 is already exsit.
shift
continue
fi
useradd $1
echo "$1 用戶創建成功"
shift
done
法二:
#!/bin/bash
while [ $# -gt 0 ];do
id $1 &> /dev/null && { echo $1 is alread exist && shift && continue; }
useradd $1 && echo $1 is created
shift
done
14、每隔3秒鐘到系統上獲取已經登錄的用戶的信息;如果發現用戶hacker登錄,則將登錄時間和主機記錄於日誌/var/log/login.log中,並退出腳本
#!/bin/bash
while true;do
if $(ps -au |grep "^hacker.*" &> /dev/null);then
echo $(ps -au |grep "^hacker.*" |tr -s " " |cut -d" " -f1,7,9) >> /var/log/login.log
break
else
sleep 3s
fi
done
15、用文件名做爲參數,統計所有參數文件的總行數
#!/bin/bash
sumhang=0
while [ $# -gt 0 ];do
hang=$(cat $1 |wc -l)
let sumhang+=$hang
shift
done
echo $sumhang
16、用二個以上的數字爲參數,顯示其中的最大值和最小值
方法一:
#!/bin/bash
min=$1
max=$1
while [ $# -gt 0 ];do
value=$1
if [ $value -gt $max ];then
max=$value
elif [ $value -lt $min ];then
min=$value
fi
shift
done
echo "min $min"
echo "max $max"
方法二:
#!/bin/bash
if [ $# -lt 2 ];then
echo "type more than two numbers"
exit 1
fi
small=$(echo $* |xargs -n1 |sort -n |head -n 1)
big=$(echo $* |xargs -n1 |sort -n |tail -n 1)
echo "Maximum number is $big"
echo "Minimum number is $small"
17:顯示UID小於1000的爲sys user,大於等於1000的爲comm user。
#!/bin/bash
while read userline;do
userid=$(echo $userline |cut -d: -f3)
username=$(echo $userline |cut -d: -f1)
if (( $userid < 1000 )) ;then
echo $username is a sys user
else
echo $username is a comm user
fi
done < /etc/passwd
18:找出分區利用率大於10%的分區,併發出警告/dev/sdaX will be full: 15%
要求使用while read來實現
法一
#!/bin/bash
df |grep /dev/sd > /app/fenqu
while read line;do
used=$(echo $line |tr -s " " % |cut -d% -f5)
name=$(echo $line |cut -d" " -f1)
if (( $used > 10 ));then
echo "$name will be full:$used%"
fi
done < /app/fenqu
法二:
#!/bin/bash
> df_h
df -h | grep ^/dev/[sh]d* >> df_h
while read disk;do
a=`echo $disk |grep -o "\<[0-9]\{0,3\}%" | cut -d% -f1`
b=`echo $disk |grep -o "^[^[:space:]]\+\>"`
if [ $a -gt 10 ];then
echo "$b will be full: $a%"
fi
done < df_h
法三:
#!/bin/bash
df |grep /dev/sd |while read disk;do
diskused=$(echo $disk |sed -r 's/.* ([0-9]+)%.*/\1/')
diskname=$(echo $disk |sed -r 's@(/dev/sd[[:lower:]][[:digit:]]).*@\1@')
if (( $diskused > 10 ));then
echo "$diskname will be full:$diskused%"
fi
done
19:掃描/etc/passwd文件每一行,如發現GECOS字段爲空,則填充用戶名和單位電話爲62985600,並提示該用戶的GECOS信息修改成功。
#!/bin/bash
cat /etc/passwd |while read userline;do
if [ -n "$(echo $userline |cut -d: -f5)" ];then
continue
else
username=$(echo $userline|cut -d: -f1)
usermod -c $username-62985600 $username
echo $username\'s gecos already changed!
fi
done
20:計算從1加到100的和。
法一:
#!/bin/bash
sum=0
for ((i=1;i<=100;i++));do
let sum+=i
done
echo sum=$sum
unset sum i
sum=0
i=1
while [ $i -le 100 ];do
let sum+=i
let i++
done
echo sum=$sum
21:用select語句製作一個菜單。
#!/bin/bash
PS3="Please choose your menu: "
select menu in huangmenji huimian hulatang roujiamo;do
case $REPLY in
1) echo "The price is \$15";;
2) echo "The price is \$10";;
3) echo "The price is \$8";;
4) echo "The price is \$6";;
*) echo "get out";break;;
esac
done