shell練習題

一、寫一個腳本,給腳本傳遞兩個參數,顯示二者之和和二者之積shell

#!/bin/bash #寫一個腳本,給腳本傳遞兩個參數,顯示二者之和和二者之積
read -p "請輸入第一個數:" num1 read -p "請輸入第二個數:" num2 echo $[$num1+$num2] echo $[$num1*$num2]

二、寫一個腳本,能接受一個參數(文件路徑),判斷這個參數若是是一個存在的文件就顯示「ok」,不然顯示「No such file"bash

#!/bin/bash
 read -p "請輸入一個文件路徑:" filename if [ -e $filename ];then echo "OK"
else echo "No such file" fi

三、判斷/etc/inittab文件是否大於100行,若是大於,則顯示」/etc/inittab is a big file.」否者顯示」/etc/inittab is a small file.」網絡

#!/bin/bash
 Line=`wc -l /etc/inittab | cut -d" " -f1` if [ $Line -gt 100 ];then echo "/etc/inittab is a big file."
else echo "/etc/inittab is a small file." fi

四、給定一個用戶,來判斷這個用戶是什麼用戶,若是是管理員用戶,則顯示「該用戶爲管理員」,不然顯示「該用戶爲普通用戶」。ui

#!/bin/bash
a=`whoami` str="root"
if [ $a = $str ];then echo "該用戶爲管理員"
else echo "該用戶爲普通用戶" fi

五、判斷某個文件是否存在spa

#!/bin/bash
 read -p "輸入查找的文件名:" filename find $filename &> /dev/null if [ $? -eq 0 ];then echo "這個文件存在"
else echo "這個文件不存在" fi

六、寫出一個腳本程序,給定一個文件,好比:/etc/inittabcode

     a、判斷這個文件中是否有空白行?blog

     b、若是有,則顯示其空白行的行號,不然顯示沒有空白行。ip

#!/bin/bash
  2 b=`grep -n "^[[:space:]]*$" /etc/inittab | wc -l` 3 
  4 c=`grep -n "^[[:space:]]*$" /root/kongbai | cut -d: -f1` 5 if [ $b -eq 0 ];then 6         echo "沒有空白行"
  7         exit 1
  8 else
  9         echo "有空白行,空白行爲$c行"
 10 exit 0 11 fi

七、判斷用戶的默認shell程序是否爲bash程序,有就顯示有多少個,沒有就顯示:沒有這類用戶。內存

1 #!/bin/bash
  2 # Author: 張琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-11 15:57:34
  5 # Name: panduan.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 declare -i sum=`grep "bin/bash$" /etc/passwd | wc -l` 9 if grep "/bin/bash$" /etc/passwd &> /dev/null;then 10         echo "存在 $sum 個用戶,shell程序爲/bin/bash"
 11         grep "/bin/bash$" /etc/passwd | cut -d: -f1 12 exit 0 13 else
 14         echo "沒有這類用戶"
 15         exit 1
 16 fi

八、判斷歷史命令總條目是否大於1000,大於顯示「some command will gone」,不然顯示OK。it

1 #!/bin/bash
  2 # Author: 張琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-11 16:06:04
  5 # Name: history.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 declore -i num 10 
 11 num=`history | wc -l` 12 
 13 if [ $num -gt 30 ];then 14         echo "some command will gone"
 15 exit 0 16 else
 17         echo "OK"
 18         exit 1
 19 fi

 九、給定一個文件,若是是普通文件和目錄文件,就顯示出來,不然顯示「沒法識別」。

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-11 16:39:33
  5 # Name: wenjian.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 
  9 read -t 50 -p "請輸入一個文件:" filename 10 
 11 if [ -z $filename ];then 12         echo "eg./etc/fstab"
 13         exit 8
 14 fi 15 
 16 if [ -f $filename ];then 17         echo "$filename 是一個普通文件"
 18         exit 0
 19 elif [ -d $filename ];then 20         echo "$filename 是一個目錄文件"
 21 else
 22         echo "沒法識別"
 23         exit 1
 24 fi

 十、輸入一個設備文件,輸出這個設備文件的基本信息。

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-12 17:09:59
  5 # Name: shebei.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 
  9 read -t 50 -p "輸入設備文件名:" devname 10 [ -z $devname ] && devname=`fdisk -l` && exit 1
 11 
 12 if [ -b /dev/$devname ];then 13         fdisk -l /dev/$devname 14         exit 0
 15 else
 16         echo "$devname 不是設備文件"
 17         echo "Usage:'請輸入一個設備文件,如sda'"
 18         exit 2
 19 fi

 十一、判斷文件是否存在,若是存在輸出是什麼文件。

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 10:40:27
  5 # Name: ceshipanduan.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 
  9 read -p "輸入一個文件名:" name 10 if [ -z $name ];then 11         echo "Usage:'輸入一個文件名:/etc/fstab"
 12         exit 1
 13 fi 14 
 15 if [ ! -e $name ];then 16         echo "提示:輸入的文件不存在!!!"
 17         exit 2
 18 elif [ -f $name ];then 19         echo "$name is a file."
 20 elif [ -b $name ];then 21         echo "$name is a block file."
 22 elif [ -L $name ];then 23         echo "$name is a Link file."
 24 elif [ -d $name ];then 25         echo "$name is a dir file."
 26 else
 27         echo "$name 是其餘文件類型"
 28 fi

十二、99乘法表

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 11:46:09
  5 # Name: 99.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 
  9 for i in `seq 9`;do
 10         for j in `seq 9`;do
 11                 [ $j -le $i ] && echo -n -e "$i*$j=`echo $(($i*$j))`\t"
 12 done 13         echo " "
 14 done

1三、用for循環遍歷全部本網絡網段中全部的up的電腦

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 11:56:36
  5 # Name: up.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 
  9 declare -i sum=0
 10 
 11 for i in $(seq 1 100);do
 12         ping -c 1 -w 1 10.6.12.$i &> /dev/null
 13         if [ $? -eq 0 ];then 14                 let sum++
 15                 echo "10.6.12.$i 是通的"
 16         else
 17                 echo "10.6.12.$i 是不通的"
 18 fi 19 done 20 
 21 echo "總共有$sum 臺電腦在線"

1四、求出1到100的偶數和

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 12:21:21
  5 # Name: oushuhe.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 
  9 declare -i sum=0
 10 
 11 for i in $(seq 0 2 100);do
 12         let sum+=$i 13 done 14 
 15 echo "總數爲:$sum"

 1五、編寫腳本查看CPU,磁盤,內存等信息

1 #!/bin/bash 2 # Author: 張琦妮 3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-17 14:57:08
  5 # Name: chakan.sh 6 # Version: v1.0
  7 # Description: This is a Script. 8 cat << EOF 9 菜單 10 -------------------
 11 cpu)顯示CPU信息 12 mem)顯示內存信息 13 disk)顯示磁盤信息 14 quit)退出 15 ------------------
 16 EOF 17 
 18 read -t 5 -p "請輸入須要查看的信息:" infomation 19 echo 20 
 21 if [ -z $infomation ];then 22         echo "請輸入一個正確的參數"
 23         echo "例如:cpu|mem|disk|quit"
 24         exit 1
 25 fi 26 
 27 if [ $infomation = 'cpu' ];then 28         cat /proc/cpuinfo 29 elif [ $infomation = 'mem' ];then 30         free -m 31 elif [ $infomation = 'disk' ];then 32         fdisk -l 33 elif [ $infomation = 'quit' ];then 34         echo "正確退出"
 35 else
 36         echo "輸出內容不正確,請輸出以下內容"
 37         echo "例如:cpu|mem|disk|quit"
 38 fi

15.2 編寫腳本查看CPU,磁盤,內存等信息。加while循環,執行一個信息不退出。

8 cat << EOF 9 菜單 10 -------------------
 11 cpu)顯示CPU信息 12 mem)顯示內存信息 13 disk)顯示磁盤信息 14 quit)退出 15 ------------------
 16 EOF 17 
 18 while true;do
 19 
 20 read -t 5 -p "請輸入須要查看的信息:" infomation 21 echo 22 
 23 if [ -z $infomation ];then 24         echo "請輸入一個正確的參數"
 25         echo "例如:cpu|mem|disk|quit"
 26         exit 1
 27 fi 28 
 29 if [ $infomation = 'cpu' ];then 30         cat /proc/cpuinfo 31 elif [ $infomation = 'mem' ];then 32         free -m 33 elif [ $infomation = 'disk' ];then 34         fdisk -l 35 elif [ $infomation = 'quit' ];then 36         echo "正確退出"
 37 else
 38         echo "輸出內容不正確,請輸出以下內容"
 39         echo "例如:cpu|mem|disk|quit"
 40 fi 41 
 42 done
相關文章
相關標籤/搜索