1. 程式人生 > >shell指令碼面試題

shell指令碼面試題

1:開發Shell指令碼判斷系統根分割槽剩餘空間的大小,如果低於1000MB就提示不足,否則提示充足。

#!/bin/bash

m=`df -m | awk '{print $4}' | sed -n 2p`

if [ $m -gt 1000 ] ; then

        echo "充足"

else

        echo "不足"

fi

2:分別使用變數定義、read讀入及指令碼傳參方式實現比較2個整數的大小。

#!/bin/bash

read -p "請輸入兩個數:" num1 num2

a=$num1

b=$num2

if [ "echo "$a" | sed -r 's/[^0-9]//g'"="$a" ];then

        if [ "echo "$b" | sed -r 's/[^0-9]//g'"="$a" ];then

        if [ $a -eq $b ];then

                echo "兩個數一般大"

        elif [ $a -gt $b ];then

                echo "第一個數比第二個數大"

        else

                echo "第二個數比第一個數大"

        fi

 

else

        echo "第二個數不是整數"

fi

 

else

        echo "第一個數不是整數"

fi

3:列印一個選單如下,當用戶選擇對應的數字時,就執行對應項的應用,最好對使用者的輸入進行是否為整數判斷。

 1.install lamp

  2.install lnmp

  3.exit

 

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-24 17:45:43

# Name: install.sh

# Version: v1.0

# Description: This is a Script.

cat << EOF

  1.install lamp

  2.install lnmp

  3.exit

EOF

read -p "please input choice" a

if [ -n "$(echo $a| sed -n "/^[0-9]\+$/p")" ];then

        echo "please input num"

fi

if [ $a -eq 1 ];then

        yum install httpd mysql php -y

elif [  $a -eq 2 ];then

        yum install nginx mysql php -y

elif [ $a -eq 3 ];then

        exit 0

fi

4:通過指令碼傳參的方式,檢查Web網站URL是否正常(要求主體使用函式)。

####可以使用curl 命令取狀態嗎  與wget同理

curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-24 14:31:53

# Name: url.sh

# Version: v1.0

# Description: This is a Script.

function_url(){

wget --spider -T 5 -q -t 2 $1

        if [ $? -eq 0 ];then

                echo 'OK!'

        else   

                echo 'error..'

        fi 

}

function_url $1

5:利用case語句開發Rsync服務啟動停止指令碼,並能通過chkconfig實現開機自啟動。

1 #!/bin/bash

  2 # Author: Peter zh

  3 # Blog: https://www.cnblogs.com/zhangwduoduoj/

  4 # Time: 2019-08-24 14:35:58

  5 # Name: rsync.sh

  6 # Version: v1.0

  7 # Description: This is a Script.

  8 chkconfig rpcbind on

  9 chkconfig rsync on

 10 function_start(){

 11         rsync --daemon

 12         if [ $? -eq 0 ]

 13         then

 14                 echo 'rsync is start'

 15         else

 16                 echo 'error..'

 17         fi

 18 }

 19 function_stop(){

 20         pkill rsync

 21         if [ $? -eq 0 ]

 22         then

 23                 echo 'rsync is stop'

 24         else

 25                 echo 'errorrr..'

 26         fi

 27 }

 28 function_restart(){

 29         function_start

 30         function_stop

 31 }

 32 case $1 in

 33 start)

 34         function_start

 35 ;;

 36 stop)

 37         function_stop

 38 ;;

 39 restart)

 40         function_restart

 41 ;;

 42 *)

 43         echo "Usage:start|stop|restart"

 44 ;;

 45 esac

6:猜數字遊戲。首先讓系統隨機生成一個數字,給這個數字定一個範圍(1-60),讓使用者輸入猜的數字,對輸入進行判斷,如果不符合要求,就給予高或低的提示,猜對後則給出猜對用的次數,請用while語句實現。

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-24 17:11:05

# Name: random.sh

# Version: v1.0

# Description: This is a Script.

sum1=0

sum2=0

number_range=$((RANDOM%60))

while :

do

read -p "please input intger>> " num

if [[ $num == "quit"  ]];then

        exit 0

fi

#echo $number_range

if [ $num -lt $number_range ]

then

        echo "little"

        let sum1++

elif [ $num -gt $number_range  ];then

 

        echo 'big'

        let sum2++

elif [ $num -eq $number_range ];then

        echo "all guess $((sum1+sum2+1))"

fi

done

7:分析nginx訪問日誌(自備),把日誌中每行的訪問位元組數對應欄位數字相加,計算出總的訪問量。給出實現程式,請用while迴圈實現。

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-24 15:06:04

# Name: nginxlog.sh

# Version: v1.0

# Description: This is a Script.

sum=0

exec <  /application/nginx/logs/access.log

while read line

do

  b=`echo "$line" | awk '{print $10}'`

  if [ $? -eq 0 ]; then

   sum=$(($sum+$b))

  fi 

done

echo $sum

8:計算從1加到100之和(要求用for和while,至少給出兩種方法)。

#!/bin/bash
sum=0
for i in {1..100}
do
        let sum+=i
done
echo `seq -s "+" 100`"="$sum

9:利用bash for迴圈列印下面這句話中字母數不大於6的單詞(某企業面試真題)。I am zzz teacher welcome to zzzzzz training class

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-24 15:11:52

# Name: 6.sh

# Version: v1.0

# Description: This is a Script.

a="I am zzz teacher welcome to zzzzzz training class"

b=`echo $a |tr " " "\n"`

 

for i in $b

do

        if [[ `echo $i|wc -L` < 6 ]];then

                echo $i

fi

done

 提示:判斷字串長度的方法

  1.wc -L

  2.echo $(expr length $變數名)

  3.echo ${#變數名}

10:使用read讀入方式比較兩個整數大小,要求對使用者輸入的內容嚴格判斷是否為整數,是否輸入了兩個數字。

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-24 17:24:21

# Name: bijiao.sh

# Version: v1.0

# Description: This is a Script.

read -p "first:" a

read -p "second:" b

 

if [[ -z $a || -z $b ]];then

        echo "please input num---"

        exit 1

fi

 

if [[ -n "$(echo $a| sed -n "/^[0-9]\+$/p")" && -n "$(echo $b| sed -n "/^[0-9]\+$/p")" ]];then

 

if [ $a -lt $b ];then

       echo "big----small" $b $a

elif [ $a -gt $b  ];then

        echo  "big----small" $a $b

else

        echo 'same num'

fi

 

else

       echo "dont  want   abcd!#%^&...."

fi

11.關閉chkconfig命令中所有第5階段下的程式,全都寫成off

#!/bin/bash

# Author: Peter zh

# Blog: https://www.cnblogs.com/zhangwduoduoj/

# Time: 2019-08-21 11:08:30

# Name: chkconfig.sh

# Version: v1.0

# Description: This is a Script.

 

chk=`chkconfig --list|awk '{print $1}'`

for i in $chk

do

judge=`chkconfig $i --list |awk '{print $7}'|cut -d: -f2`

        if [[ $judge == "off"  ]];then

        echo "$i dont need off ,already off"

        else

        chkconfig --level 5 $i off

        fi 

done