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