shell脚本练习题

1、猜数字游戏

系统随机给出一个数,猜大小,

#!/bin/bash
echo "可以输入q或者quit退出!"

a=$[RANDOM%100+1]
while :
do
        read -p "请输入一个1-100之间的整数:" num
        i=`echo $num | sed 's/[0-9]//g'`
                if [ -z "$num" ];then
                        echo '输入为空,请重新输入!'
                        continue
                fi
                        if [ $num == q ] || [ $num == quit ];then
                                exit 2
                        fi
                                if [ ! -z "$i" ];then
                                        echo "你输入的不是一个数字,请重新输入!"
                                        continue
                                fi
                        if [ $num -lt 0 ] || [ $num -gt 100 ];then
                                echo "请输入1-100之间的整数!"
                                continue
                        fi
                if [ $num -lt $a ];then
                        echo "猜小了,请重新猜"
                elif [ $num -gt $a ];then
                        echo "猜大了,请重新猜"
                else
                        echo "恭喜你才对了"
                        read -p "输入yes再来一次,输入no或者其他退出:" as
                        case $as in
                                yes)
                                continue
                                ;;
                                no)
                                exit
                                ;;
                                *)
                                break
                        esac
                fi
done              

2、监控web可用性

写一个shell脚本,每隔10秒通过curl -I 返回的状态码来判定所访问的网站是否正常。比如,当状态码为200时,才算正常。

#!/bin/bash
read -p "请输入url:" url
sta=`curl -I $url 2>/dev/null | head -1 | awk '{print $2}'`
while :
do
        if [ $sta != "200" ];then
                echo "$url down."
        else
                echo "$url is up!"
        fi
        sleep 10
        continue
done

3、三行变一行

[root@localhost shell]# cat /tmp/1.txt 
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost shell]# sed 'N;N;s/\n/ /g' /tmp/1.txt 
1 2 3
4 5 6
7 8 9
10 11 12

3、检查服务

检查服务是否启动,如果没有启动,启动服务

#!/bin/bash
read -p "请输入服务名:" server
test=`ps -ef | grep $server | grep -v grep`
if [ ! -n "$test" ];then
        echo "$server is down!" 
        /etc/init.d/$server start &>/dev/null
        echo "$server restart is ok!"
else        
        echo "$server is up!" 
fi

4、写一个getinterface.sh脚本,完成下面任务

1、使用以下形式:getinterface.sh [-i interface | -I ip]
2、当用户使用-i选项时,显示指定网卡的IP地址;当用户使用-I选项时,显示指定ip所属的网卡。
例:sh getinterface.sh -i eth0
sh getinterface.sh -I 192.168.0.1
3、当用户使用除[-i | -I]选项时,显示[-i interface | -I ip]此信息
4、当用户指定信息不符合时,显示错误。(比如指定的eth0没有,而是eth1时)

#!/bin/bash
ip addr | awk -F ":" '$1 ~ /^[1-9]/ {print $2}' | sed 's/ //g' > /tmp/eths.txt
[ -f /tmp/eth_ip.log ] && rm -f /tmp/eth_ip.log
for eth in `cat /tmp/eths.txt`
do
        ip=`ifconfig $eth | awk -F " " '/inet / {print $2}' | cut -d ":" -f 2`
        echo "$eth:$ip" >> /tmp/eth_ip.log
done
useage()
{       
        echo "please useage:$0 -i 网卡名字 or $0 -I ip地址"
}

wrong_eth()
{
        if ! awk -F ":" '{print $1}' /tmp/eth_ip.log | grep -qw "^$1$"
        then
                echo "请指定正确的网卡名字"
                exit
        fi
}
wrong_ip()
{
        if ! awk -F ":" '{print $2}' /tmp/eth_ip.log | grep -qw "^$1$"
        then
                echo "请指定正确的ip地址"
                exit
        fi
}
if [ $# -ne 2 ]
then
        useage
        exit
fi
case $1 in
        -i)
        wrong_eth $2
        grep -w $2 /tmp/eth_ip.log | awk -F ":" '{print $2}'
        ;;
        -I)
        wrong_ip $2
        grep -w $2 /tmp/eth_ip.log | awk -F ":" '{print $1}'
        ;;
        *)
        useage
        exit
esac

5、写一个计算器,可以运行加减乘除等运算

#!/bin/bash
read -p "请输入第一个数字:" num1
read -p "请输入运算符:" opr
read -p "请输入第二个数字:" num2

sum1=`echo $num1 | sed 's/[0-9]//g' | sed 's/-//g'`
sum2=`echo $num2 | sed 's/[0-9]//g' | sed 's/-//g'`

if [ -z "$sum1" -a -z "$sum2" ];then
        case $opr in
                \*)
                result=$(($num1*$num2))
                ;;
                +)
                result=$(($num1+$num2))
                ;;
                -)
                result=$(($num1-$num2))
                ;;
                /)
                result=$(($num1/$num2))
                ;;
                %)
                result=$(($num1%$num2))
                ;;
                *)
                echo "请输入正确的运算符:"
                exit 2
        esac
        echo $result
else
        echo "请输入正确的数字!!"
fi

6、3位随机数字

写一个脚本产生随机3未的数字,并且可以根据用户的输入参数来判断输出几组,例如: 脚本名字为number3.sh
执行方法:
bash number3.sh 直接产生一组3位数字
bash number3.sh 10 插上10组3为数字

#!/bin/bash
get_a_num() {
        n=$[$RANDOM%10]
        echo $n
}
get_numbers() {
        for i in 1 2 3;do
                a[$i]=`get_a_num`
        done
        echo ${a[@]}
}
if [ -n "$1" ];then
        m=`echo $1 | sed 's/[0-9]//g'`
        if [ -n "$m" ];then
                echo "Useage bash $0 n,n is a number,example:bash $0 5"
                exit
        else
                for i in `seq 1 $1`
                do
                        get_numbers
                done
        fi
else
        get_numbers
fi

7、编写一个chname脚本,将目录下的.txt结尾的文件名都修改为以.doc结尾

#!/bin/bash
find /tmp/dir/ -type f -name "*.txt" > /tmp/txt.list
for f in `cat /tmp/txt.list`
do
        n=`echo $f | sed -r 's/(.*)\.txt/\1/'`
        mv $f $n.doc
done

8、编写一个chuser程序,执行中没割5分钟检测指定的用户登录,用户名,从命令行输入;如果指定的用户登录,则显示相关信息

#!/bin/bash
read -p "Please input the username:" user
USER=`env | grep USER | cut -d "=" -f 2`
while :
do
        if [ "$user" == "$USER" ];then
                echo $user login
        else
                echo $user not login
        fi
        sleep 300
done

9、判断pid是否一致

我们用ps -aux命令查看到的进程的pid都会在/proc内产生,如果通过命令看到的pid是/proc里面没有的,则进程很可能被修改了,编写一个脚本,定期检查下,自己的系统是否被入侵过

#!/bin/bash
ps aux | awk '/[0-9]/ {print $2}' | while read pid
do
	result=`find /proc/ -maxdepth 1 -type d -name "$pid"`
	if [ -z $result ];then
		echo "$pid abnormal!"
	fi
done

10、判断文件是否存在

编写一个名为iffile程序,它执行时判断/bin/目录下date文件是否存在?

#!/bin/bash

if [ -f /bin/date ]
then
	echo "/bin/date file exist!"
else
	echo "/bin/date not exist!"
fi

11、编写一个名为greet的文成程序,它执行时能根据系统当前的时间向用户输出问候信息,设从半夜到中午为造成,中午到下午六点为下午,下午六点到半夜为晚上

#!/bin/bash

h=`date +%H`
if [ $h -ge 0 ] && [ $h -lt 12 ]
then
        echo "Good morning."
elif [ $h -ge 12 ] && [ $h -lt 18 ]
then
        echo "Good afternoon."
else
        echo "Good evening."
fi

12、编写一个名为ifuser的程序,它执行时代用户名作为命令行参数,判断该用户是否已经在系统中登录,并给出相关信息

#!/bin/bash

read -p "Please input the username:" user
if who | grep -qw $user
then
	echo $user is online.
else
	echo $user not online
fi

13、编写一个名为menu的程序,实现简单的弹出式菜单,用户能根据显示的额菜单项从键盘选择执行对应的命令

#!/bin/bash

function message()
{
	echo "0. w"
	echo "1. ls"
	echo "2. quit"
	read -p "please input parameter: " par
}
message
while [ $par -ne '2' ];do
	case $par in
	0)
		w
		;;
	1)
		ls
		;;
	2)
		exit
		;;
	*)
		echo "Unkown command"
		;;
	esac
	message
done

14、格式化输出

输入一串随机数字,然后按千分位输出,比如输入的数字串味“123456789”,输出为“123,456,789”

 #!/bin/bash
read -p "输入一串数字:" num
v=`echo $num | sed 's/[0-9]//g'`
if [ -n "$v" ]
then
	echo "请输入纯数字!"
	exit
fi
echo $num | sed -r '{:number;s/([0-9]+)([0-9]{3})/\1,\2/;t number}'
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值