更多內容請點擊:html
Linux學習從入門到打死也不放棄,徹底筆記整理(持續更新,求收藏,求點贊~~~~)
shell
https://blog.51cto.com/13683480/2095439centos
1,編寫腳本/root/bin/createuser.sh,實現以下功能:使用一個用戶名作爲參數,若是指定參數的用戶存在,就顯示其存在,不然添加之;顯示添加的用戶的id號等信息數組
if [ -z "$1" ]; then echo no argument exit 1 elif id "$1" &> /dev/null ;then echo usre "$1" is already exisit exit 2 elif useradd "$1" &> /dev/null ;then echo "$!"abc | passwd --stdin "$1" &> /dev/null ID=$(id -u "$1") HD=$(getent passwd|grep "$1" |cut -d: -f6) echo -e "user ${1} is add.\nUID:${ID}\nPASSWORD:${1}abc\nHOMEDIR:${HD}" exit 0 else echo user name is illegal exit 3 fi
執行結果:bash
二、編寫腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,並判斷用戶輸入的是yes仍是no,或是其它信息網絡
read -t 10 -p "please input you answer. yes or no? : " ANS if [ -z "$ANS" ]; then echo "I can't here anything" exit 1 fi case $ANS in [yY]|[yY][eE][Ss]) echo your answer is yes exit 0 ;; [nN]|[nN][oO]) echo your answer is no exit 0 ;; *) echo "I don't understand you" exit 1 esac
執行結果:socket
三、 編寫腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,連接,其它文件類型)ide
if [ -z "$1" ]; then echo there must be a filename argument exit 1 elif [ ! -e "$1" ]; then echo no such file exit 1 elif [ -d "$1" ]; then echo directory file exit 0 elif [ -L "$1" ]; then echo link file exit 0 elif [ -f "$1" ]; then echo normal file exit 0 else echo other file exit 0 fi
執行結果:函數
四、編寫腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否爲正整數 學習
[ -z "$1" ] && echo no argument && exit 1 if [[ "$1" =~ ^[0-9]+$ ]] ; then echo int number case $1 in *1|*3|*5|*7|*9) echo and its an Odd number exit 0 ;; *2|*4|*6|*8|*0) echo and its an Even number exit 0 ;; *) echo unkown error exit 1 ;; esac else echo not int number exit 1 fi
額外增長了判斷奇數仍是偶數的功能,不過不能判斷負數
執行結果:
五、用for循環,編寫一個腳本統計一下 /dev/下每種文件類型的文件分別有多少個,將結果打印出來。
declare -i cf=0 declare -i bf=0 declare -i df=0 declare -i lf=0 declare -i sf=0 declare -i pf=0 declare -i ff=0 declare -i of=0 [ -d "$1" ] || { echo putin a dir be argument;exit 1; } for i in $1/* ;do #for j in $(ls $1);do #declare i=/dev/$j if [ -c "$i" ];then #echo $i is en character file let cf++ elif [ -b "$i" ];then #echo $i is a block file let bf++ elif [ -d "$i" ];then #echo $i is a directory file let df++ elif [ -L "$i" ];then #echo $i is a link file let lf++ elif [ -s "$i" ];then #echo $i is a socket file let sf++ elif [ -p "$i" ];then #echo $i is a piping file let pf++ elif [ -f "$i" ];then #echo $i is a numal file let ff++ else #echo $i is other file let of++ fi done let total=cf+bf+df+lf+sf+pf+ff+of echo -e "tomal file number:\t $total " echo -e "character file number:\t $cf " echo -e "block file number:\t $bf " echo -e "dir file number:\t $df " echo -e "link file number:\t $lf " echo -e "socket file number:\t $sf" echo -e "piping file number:\t $pf" echo -e "nomal file number:\t $ff" echo -e "other file number:\t $of"
支持參數傳遞要查詢的目錄,不指定/var
執行結果:
六、添加10個用戶user1-user10,密碼爲8位隨機字符
for i in user{1..10};do if id $i &>/dev/null;then echo $i is exist else useradd $i &>/dev/null pass=$(openssl rand -base64 20|tr -dc [[:alpha:]]|cut -c1-8) echo $pass |passwd --stdin "$i" &>/dev/null echo user $i is add,password is: $pass fi done
執行結果:
附帶刪除腳本:
for i in user{1..10};do userdel -r $i &>/dev/null echo user $i is del done
執行結果:
七、/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 -e "$i \t\tstop" elif [[ "$i" =~ ^S.* ]]; then echo -e "$i \t\tstart" else echo -d "$i \t\tother" fi done
執行結果,centos6上效果明顯一點,太晚了不想再開機了。。
八、編寫腳本,提示輸入正整數n的值,計算1+2+…+n的總和
[[ "$1" =~ ^[0-9]+$ ]] || { echo no number; exit 1; } bite=$(echo $1 |grep -o [0-9]|wc -l) if [ $bite -gt 6 ];then echo too big number exit 1 fi declare -i sum=0 for i in $(seq 1 $1);do let sum=sum+i done echo sum=$sum unset sum
執行結果:
九、計算100之內全部能被3整除的整數之和
declare -i sum=0 declare -i i=1 while [ "$i" -le 100 ];do let sum+=i let i+=2 done echo sum=$sum
執行結果:
十、編寫腳本,提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態、
while true; do read -t 60 -p "input the network number: " netnum if [[ $netnum =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}0$ ]];then break else echo wrong network number fi done last=$(echo $netnum|cut -d'.' -f3) declare -i up=0 declare -i down=0 if [ $last != 0 ];then net=$(echo $netnum|cut -d'.' -f1-3) for i in {1..255};do j=${net}.$i { if ping -c1 -w1 $j &>/dev/null;then echo host $j is up let up++ else #echo host $j is down let down++ fi; } & done wait #echo #echo -e "uphost number is:\t $up" #echo -e "downhost number is:\t $down" else second=$(echo $netnum |cut -d'.' -f2) if [ $second = 0 ];then echo I cant do this. Its a too much big network exit 2 else net=$(echo $netnum |cut -d'.' -f1-2) for i in `seq 0 255`;do j=${net}.$i for k in `seq 0 255`;do { h=${j}.$k if ping -c1 -w1 $h &>/dev/null;then echo host $h is up let up++ else #echo host $h is dowm let down++ fi; } & done wait done #echo #echo -e "uphost number is:\t $up" #echo -e "downhost number is:\t $down" fi fi
使用了並行執行,增長了測試效率,可是沒法統計total,如須要統計totol 或者順序顯示,去掉並行執行,開啓計數便可
執行結果:
十一、打印九九乘法表
for i in {1..9};do for j in `seq $i`;do let h=j*i echo -e "${j}x${i}=${h}\t\c" done echo done
執行結果:使用while或者until循環均可以,就不單獨演示了
十二、在/testdir目錄下建立10個html文件,文件名格式爲數字N(從1到10)加隨機8個字母,如:1AbCdeFgH.html
for i in {1..10};do kk=$(openssl rand -base64 20|tr -dc [[:alpha:]]|cut -c1-8) touch /data/testdir/"$i""$kk".html done
執行結果:
1三、打印等腰三角形
while true;do read -t 30 -p "input the base length 2-50: " high if [[ "$high" =~ ^[1-9]?[0-9]$ ]] && [ "$high" -ge 2 -a "$high" -lt 51 ];then break else echo wrong number fi done declare color for i in `seq $high`;do let space=high-i let prin=2*i-1 for j in `seq $space`;do echo -e " \c" done for k in `seq $prin`;do echo -e "\033[1;$[$RANDOM%7+31]m*\c" done for l in `seq $space`;do echo -e " \c" done echo done
執行結果:增長了顏色顯示
1四、編寫腳本,利用變量RANDOM生成10個隨機數字,輸出這個10數字,並顯示其中的最大值和最小值
while true;do read -t 30 -p "input the base length 2-50: " high if [[ "$high" =~ ^[1-9]?[0-9]$ ]] && [ "$high" -ge 2 -a "$high" -lt 51 ];then break else echo wrong number fi done declare color for i in `seq $high`;do let space=high-i let prin=2*i-1 for j in `seq $space`;do echo -e " \c" done for k in `seq $prin`;do echo -e "\033[1;$[$RANDOM%7+31]m*\c" done for l in `seq $space`;do echo -e " \c" done echo done
執行結果:
也能夠基於數組實現:
declare -i max declare -i min for ((i=0;i<=9;i++));do num[$i]=$RANDOM if [ $i = 0 ];then let max=${num[$i]} let min=${num[$i]} else if [ ${num[$i]} -gt $max ];then let max=${num[$i]} elif [ ${num[$i]} -lt $min ];then let min=${num[$i]} fi fi done echo ${num[@]} echo the max number is: $max echo the min number is: $min
執行結果:
1五、編寫腳本,實現打印國際象棋棋盤
### level=1 i=1 while [ $i -le 4 ];do j=1;k=1 while [ $j -le 4 ];do echo -e "\033[49m \033[0m\033[47m \033[0m\c" let j++ done echo while [ $k -le 4 ];do echo -e "\033[47m \033[0m\033[49m \033[0m\c" let k++ done echo let i++ done ### echo echo this is level 1 echo echo #### level=2 # declare -i s while true;do read -p "choose the level 1-9: " s if [[ "$s" =~ ^[1-9]$ ]] &>/dev/null ;then break else echo wrong number fi done PS3="choose the first color(1-8): " select manu in red green yellow blue voilet light-blue white black;do case $manu in red) echo you have choose the $manu be the first color break ;; green) echo you have choose the $manu be the first color break ;; yellow) echo you have choose the $manu be the first color break ;; blue) echo you have choose the $manu be the first color break ;; voilet) echo you have choose the $manu be the first color break ;; light-blue) echo you have choose the $manu be the first color break ;; white) echo you have choose the $manu be the first color break ;; black) echo you have choose the $manu be the first color break ;; *) echo i cant hera you esac done first=$REPLY PS3="choose the second color(1-8): " select manu in red green yellow blue voilet light-blue white black;do case $manu in red) echo you have choose the $manu be the second color break ;; green) echo you have choose the $manu be the second color break ;; yellow) echo you have choose the $manu be the second color break ;; blue) echo you have choose the $manu be the second color break ;; voilet) echo you have choose the $manu be the second color break ;; light-blue) echo you have choose the $manu be the second color break ;; white) echo you have choose the $manu be the second color break ;; black) echo you have choose the $manu be the second color break ;; *) echo i cant hera you esac done second=$REPLY let color1=40+first let color2=40+second #### while code i=1 while [ $i -le 4 ];do j=1;while [ $j -le $s ];do k=1;while [ $k -le 4 ];do m=1;n=1 while [ $m -le $s ];do echo -e "\033[${color1}m \033[0m\c" let m++ done while [ $n -le $s ];do echo -e "\033[${color2}m \033[0m\c" let n++ done let k++ done echo let j++ done j=1;while [ $j -le $s ];do k=1;while [ $k -le 4 ];do m=1;n=1 while [ $m -le $s ];do echo -e "\033[${color2}m \033[0m\c" let m++ done while [ $n -le $s ];do echo -e "\033[${color1}m \033[0m\c" let n++ done let k++ done echo let j++ done let i++ done ##ending
生成了2次菜單,能夠考慮函數複用
執行結果:
1六、後續六個字符串:efbaf275cd、4be9c40b8b、44b2395c4六、f8c8873ce0、b902c16c8b、ad865d2f63是經過對隨機數變量RANDOM隨機
執行命令: echo $RANDOM|md5sum|cut –c1-10 後的結果,請破解字符串對應的RANDOM值
i=1;while [ $# != 0 ];do j=1;while true ;do num=$(echo $j |md5sum |cut -c1-10) if [ $num = $1 ];then break else let j++ fi done echo $1:$j shift done
執行結果:參數仍是須要手工輸入,如過多,能夠考慮寫入文件以後使用while循環讀取
1七、每隔3秒鐘到系統上獲取已經登陸的用戶的信息;若是發現用戶hacker登陸,則將登陸時間和主機記錄於日誌/var/log/login.log中,並退出腳本
if [ -z $1 ];then echo no argument exit 1 else if id $1 &> /dev/null ;then if who |grep $1 &>/dev/null;then echo user $1 is already running exit 3 fi else echo no such user exit 2 fi fi while true;do sleep 3 if who |grep $1 &>/dev/null;then echo "`date +%F-%T`: user $1 is login ">> /var/log/login.log exit fi done
執行結果:
執行命令
登陸hello帳號:
發現命令已終止,查看日誌已記錄登陸
1八、隨機生成10之內的數字,實現猜字遊戲,提示比較大或小,相等則退出
declare num=$[$RANDOM%191+9] declare -i time=0 while true ;do read -p "input a number 9-199: " ans let time+=1 if [[ ! $ans =~ ^[0-9]+$ ]];then continue elif [ $ans -gt $num ];then echo too big elif [ $ans -lt $num ];then echo too small else break fi if [ $time -ge 10 ];then echo times out,the right number is $num exit 1 fi done echo binngo,the right number is $num
執行結果:
執行結果2
1九、用文件名作爲參數,統計全部參數文件的總行數
declare -i sum=0 until [ $# = 0 ];do if [ -f $1 ];then let sum+=$(cat $1|wc -l) else echo $1 is not found or wrong type file fi shift done echo echo the totalline is : $sum
執行結果
20、用二個以上的數字爲參數,顯示其中的最大值和最小值
if [ $# -lt 2 ];then echo at least 2 argument exit 1 fi declare -i big declare -i small declare -i fir=$# until [ $# = 0 ];do if [[ ! "$1" =~ ^[0-9]+$ ]];then echo wrong number $1 exit fi if [ $fir = $# ];then let big=$1 let small=$1 else if [ $1 -gt $big ];then let big=$1 elif [ $1 -lt $small ];then let small=$1 fi fi shift done echo the biggest number is : $big echo the smallest unmber is: $small
執行結果:
2一、編寫腳本,實現2進制數與10進制數的轉換
if [ "$1" = 2 -o "$1" = 10 ] && [[ "$2" =~ ^[0-9]+$ ]];then : else echo wrong argument exit 1 fi if [ "$1" = 10 ]; then ##check the $2 is or not too big bite=$(echo "$2"|grep -o '[0-9]'|wc -l) if [ "$bite" -gt 19 ];then echo too big number "$2" exit 2 fi ##Computing unit code declare -i x="$2" declare y while [ "$x" -gt 1 ];do gem=$[$x%2] x=$[x/2] y=$gem$y done y=$x$y echo $y fi ##ending 1 if [ "$1" = 2 ];then ##check the $2 is or not too big bite=$(echo "$2"|grep -o '[0-9]'|wc -l) if [ "$bite" -gt 63 ];then echo too big number "$2" exit 2 fi ##Computing unit code ##include judge the $2 is or not a Binary number y=0 let x=$bite-1 b=$x for i in $(seq "$b") ;do j=$(echo "$2"|cut -c $i) if [ $j -gt 1 ];then echo "$2" is not a Binary number. exit 3 else y=$[j*2**x+y] let x-- fi done last=$(echo "$2"|cut -c "$bite") if [ "$last" -gt 1 ];then echo "$2" is not a Binary number. exit 3 else let y=y+last echo $y fi fi ##ending 2
補充說明:給定2個參數,參數1爲「2」則表示參數2須要爲2進制數,須要轉換成10進制
若是參數1 爲「10」則表示參數2爲10進制數,須要轉化成2進制
實際測試,發現shell最大支持計算值爲2^64次方,因此二級數超過64位以上提示報錯
執行結果1
執行結果2