Shell脚本练习题

1、

给定两个数字。输出其中较大的值(Shell脚本中的)

read a
read b
if [ $a -gt $b ]
then
  echo $a
else
  echo $b
fi

 

2、

输入一个数字,如果能被3整除,那输出他的平方

read c
if [ $c%3=0 ]
then
  let d="$c*$c"
  echo $d
fi

 

3、

read实现一个脚本 请求给一个用户,添加一个用户然后给定密码:输入m密码取默认123456

#!/bin/bash
read -p "use:" usee
read -p "password:" passs
mm="m"
if [ -n $usee ]
then
        if [ $passs = $mm ]
        then
                useradd $usee
                echo "123456" | passwd $usee --stdin
        elif [ -n $passs ]
        then
                useradd $usee
                echo "$passs" | passwd $usee --stdin
        fi
fi

 

4、

 写一个脚本,提示用户输入一个字符串,如果输入时quit,则退出,否则,显示其输入的字符
串脚本内容。

#!/bin/bash

while [ 1 ]
do
read -p "shu:" a
if [$a="quit"]
then
exit
fi
echo $a
done

 

5、

 判断输入数值是奇数或者偶数的脚本

#!/bin/bash
read g
let b=$g%2
if [ $b = 0 ]
then
echo "oushu"
else
echo "jishu"
fi

 

6、编写shell脚本,计算1-100的和

 

sum=0
for i in $(seq 1 100)
do
sum=$[$i + $sum]
done
echo $sum

 

7、输入两个数,求和、差、商、积、余。

read a
read b
let d=$a+$b
echo $d
let e=$a-$b
echo $e
let f=$a*$b
echo $f
let g=$a/$b
echo $g
let h=$a%$b
echo $h

 

8、

测试主机ip是否在线 

      通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线

ping -c1 -w1 中-c1是指ping的次数,-w是指执行的最后期限,也就是执行的时间,单位为秒

#!/bin/bash

i=0
for i in $(seq 151 254)
do
ping -c1 -w1 192.168.0.$i &>/root/aaa.txt
if [ $? -eq 0 ]
then
echo "192.168.0.$i is up"
else
echo "192.168.0.$i is down"
fi
done

 

posted @ 2018-11-20 11:43  yangyang1182  阅读(446)  评论(0编辑  收藏  举报