一、編寫腳本/root/bin/createuser.sh,實現以下功能:使用一個用戶名作爲參數,若是指定參數的用戶存在,就顯示其存在,不然添加之,並生成8位隨機口令並存在一個文件中,初步提示改口令,顯示添加的用戶的id號等信息。html
#!/bin/bash # ------------------------------------------ # Filename: cid.sh # Date: 2017-09-16 # Author: liuke # Description: 使用一個用戶名作爲參數,若是指定參數的用戶存在,就顯示其存在,不然添加之# 生成8位隨機口令並存在一個文件中,初步提示改口令,顯示添加的用戶的id號等信息。 # ------------------------------------------- read -p "please input a username:" uname id $uname &> /dev/null e=$? if [ $e -eq 0 ] then echo "$uname is exist" exit 2 else useradd $uname echo `cat /dev/urandom | tr -d -c '[:alnum:][:punct:]' | head -c 10` > /app/mima passwd $uname id $uname fi unset uanme e
二、編寫腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,並判斷用戶輸入的是yes仍是no,或是其它信息bash
#!/bin/bash # ------------------------------------------ # Filename: yesno.sh # Revision: null # Date: 2017-09-10 # Author: liuke # Description: 提示用戶輸入yes或no,並判斷用戶輸入的是yes仍是no,或是其它信息 # ------------------------------------------- read -p "Yue ma?yes or no:" word word=`echo $word|tr "A-Z" "a-z"` case $word in yes|y) echo "yue" ;; no|n) echo "buyue" ;; *) echo "error input" esac
三、編寫腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,連接,其它文件類型)網絡
#!/bin/bash # ------------------------------------------ # Filename: filetype.sh # Date: 2017-09-11 # Author: liuke # Description: 判斷用戶輸入文件路徑,顯示其文件類型 # ------------------------------------------- read -p "please input a file:" file if [ -f $file ];then echo "filetype is common file" elif [ -d $file ];then echo "filetype is directory" elif [ -h $file ];then echo "filetype is soft link" else echo "filetype is other" fi unset file
四、編寫腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否爲正整數app
#!/bin/bash # ------------------------------------------ # Filename: checkint.sh # Date: 2017-09-11 # Author: liuke # Description: 判斷用戶輸入的參數是否爲正整數 # ------------------------------------------- read -p "please input a num:" num if [[ $num =~ ^[1-9][0-9]*$ ]];then echo "the num is a positive integer " else echo "wrong input" fi
五、判斷/var/目錄下全部文件的類型dom
#!/bin/bash # ------------------------------------------ # Filename: file-var.sh # Date: 2017-09-11 # Author: liuke # Description: 判斷/var/目錄下全部文件的類型 # ------------------------------------------ cd /var/;ls -1 | while read filename do if [ -f $filename ];then echo " $filename filetype is common file" elif [ -d $filename ];then echo "$filename filetype is directory" elif [ -h $filename ];then echo "$filename filetype is soft link" else echo "$filename filetype is other" fi done unset filename
六、添加10個用戶user1-user10,密碼爲8位隨機字符ide
#!/bin/bash # ------------------------------------------ # Filename: 10user.sh # Date: 2017-09-11 # Author: liuke # Description: 添加10個用戶user1-user10,密碼爲8位隨機字符 # ------------------------------------------- for ((i=1;i<= 10;i+=1)) do pw=`cat /dev/urandom |tr -d -c '[:alnum:][:punct:]'|head -c 8` useradd user$i && echo $pw|passwd --stdin user$i &> /dev/null echo -e "user:user$i;passwd:$pw" done # for i in {1..10};do userdel -r user$i;done 一次刪除10個用戶
七、/etc/rc.d/rc3.d目錄下分別有多個以K開頭和以S開頭的文件;分別讀取每一個文件,以K開頭的輸出爲文件加stop,以S開頭的輸出爲文件名加start,如K34filename stop S66filename startui
#!/bin/bash # ------------------------------------------ # Filename: rc3.sh # Date: 2017-09-16 # Author: liuke # Description: 分別讀取/etc/rc.d/rc3.d/每一個文件,以K開頭的輸出爲文件加stop,以S開頭的輸出爲文件名加start # ------------------------------------------- for filename in `ls /etc/rc.d/rc3.d/`;do case $filename in K*) echo "$filename stop" ;; S*) echo "$filename start" esac done unset filename
八、編寫腳本,提示輸入正整數n的值,計算1+2+…+n的總和spa
#!/bin/bash # ------------------------------------------ # Filename: 1-nhe.sh # Date: 2017-09-11 # Author: liuke # Description: 提示輸入正整數n的值,計算1+2+…+n的總和 # ------------------------------------------- read -p "please input a num:" num if [[ $num =~ ^[1-9][0-9]*$ ]];then sum=0 for ((i=1;i<= $num;i+=1)) do sum=$(($sum+$i)) done echo "1+2+…+n的總和爲:$sum" fi unset num sum
九、計算100之內全部能被3整除的整數之和htm
#!/bin/bash # ------------------------------------------ # Filename: zhengchu3.sh # Date: 2017-09-11 # Author: liuke # Description: 計算100之內全部能被3整除的整數之和 # ------------------------------------------- sum=0 for ((i=3;i<= 100;i+=3)) do sum=$(($sum+$i)) done echo "100之內全部能被3整除的整數之和爲:$sum" unset sum
十、編寫腳本,提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態。ip
#!/bin/bash # ------------------------------------------ # Filename: scanip11-2.sh # Revision: null # Date: 2017-09-10 # Author: liuke # Description: 提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態。 # ------------------------------------------- > /app/ip.txt read -p "please input a ip:" ip echo $ip|egrep "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"&> /dev/null if [ $? -eq 0 ];then id=`echo $ip|egrep -o ".*\."` ping -c 1 -w 1 $ip &> /dev/null echo "$ip is up">>/app/ip.txt echo "$ip is down" fi
十一、打印九九乘法表(for循環)
#!/bin/bash # ------------------------------------------ # Filename: 9*9.sh # Revision: null # Date: 2017-09-10 # Author: liuke # Description: 打印九九乘法表(for循環) # ------------------------------------------- for i in {1..9};do for j in `seq $i`;do echo -en "${i}X${j}=$[$ix$j]\t" done echo done
十二、打印九九乘法表(while循環)
#!/bin/bash # ------------------------------------------ # Filename: 9*9while.sh # Date: 2017-09-11 # Author: liuke # Description: 打印九九乘法表(while) # ------------------------------------------- while ((i<10,j<9)) do echo -en "${i}X${j}=$[$i*$j|bc]\t" done echo
1三、在/testdir目錄下建立10個html文件,文件名格式爲數字N(從1到10)加隨機8個字母,如:1AbCdeFgH.html。
#!/bin/bash # ------------------------------------------ # Filename: 10user.sh # Date: 2017-09-11 # Author: liuke # Description: 添加10個用戶user1-user10,密碼爲8位隨機字符 # ------------------------------------------- for ((i=1;i<= 10;i+=1)) do pw=`cat /dev/urandom |tr -d -c '[:alnum:][:punct:]'|head -c 8` useradd user$i && echo $pw|passwd --stdin user$i &> /dev/null echo -e "user:user$i;passwd:$pw" done # for i in {1..10};do userdel -r user$i;done 一次刪除10個用戶
1四、編寫腳本,求100之內全部正奇數之和
#!/bin/bash # ------------------------------------------ # Filename: jishuhe.sh # Date: 2017-09-11 # Author: liuke # Description: 編寫腳本,求100之內全部正奇數之和 # ------------------------------------------- sum=0;i=1 while ((i<100)) do sum=$[$sum + $i] let i=$[$i+2] done echo "100之內全部正奇數之和爲:$sum"
1五、編寫腳本,利用變量RANDOM生成10個隨機數字,輸出這個10數字,並顯示其中的最大值和最小值。
#!/bin/bash # ------------------------------------------ # Filename: random10-2.sh # Date: 2017-09-16 # Author: liuke # Description: 利用變量RANDOM生成10個隨機數字,輸出這個10數字,並顯示其中的最大值和最小值 # while # ------------------------------------------- i=1 while [ $i -lt 11 ];do sum=`echo $[RANDOM]` echo -e "$sum" "\c" if [ $i -le 1 ];then max=$sum min=$sum elif [[ $sum -gt $max ]];then max=$sum elif [[ $sum -lt $min ]];then min=$sum fi let i+=1 done echo echo "the number max is:$max;min number is:$min"
1六、編寫腳本,編輯菜單,用戶輸入菜單列表中的某個數字,顯示相應價格。
#!/bin/bash # ------------------------------------------ # Filename: caidan.sh # Date: 2017-09-10 # Author: liuke # Description: 編輯菜單,用戶輸入菜單列表中的某個數字,顯示相應價格。 # ------------------------------------------- echo "1=yangroutang 2=mifan 3=hulatang 4=jiaozi 5=lamian 6=huimian" read -p "please input your choose num:" num case $num in 1|4) echo "the price is 20¥" ;; 2|5) echo "the price is 12¥" ;; 3|6) echo "the price is 10¥" ;; *) echo "error choose" esac
1七、編寫腳本,實現打印國際象棋棋盤
#!/bin/bash # ------------------------------------------ # Filename: chess11.sh # Date: 2017-09-11 # Author: liuke # Description: 棋盤 # ------------------------------------------- for ((i=1;i<=8;i++));do for ((j=1;j<=8;j++));do total=$(($i+$j)) tmp=$(($total % 2)) if [ $tmp -eq 0 ] then echo -e -n "\033[43m \033[0m" else echo -e -n "\033[41m \033[0m" fi done echo done