- 编写shell脚本,计算1-100的和;
[root@yong-01 20180723]# vim sum100.sh#!/bin/bashsum=0for i in `seq 1 100`do sum=$[$sum+$i]doneecho $sum
- 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;
[root@yong-01 20180723]# vim he_n.sh#!/bin/bashn=0while [ $n -le "1" ]do read -p "Please in put a nubmer,it must greater than "1": " ndonesum=0for i in `seq 1 $n`do sum=$[$sum+$i]doneecho $sum
- 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;
[root@yong-01 20180723]# vim cp_root.sh #!/bin/bashcd /rootfor f in `ls`;do if [ -d $f ];then cp -r $f /tmp/ fidone
- 编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组;
[root@yong-01 20180723]# vim useradd.sh#!/bin/bashgroupadd usersfor i in `seq -w 1 100|sed "s/^0//g"`do useradd -g users user_$idone
- 编写shell脚本,截取文件test.log中包含关键词 ‘abc’ 的行中的第一列(假设分隔符为 ”:” ),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;
[root@yong-01 20180723]# vim test.sh#! /bin/bashawk -F':' '$0~/abc/ {print $1}' test.log >/tmp/n.txtsort -n n.txt |uniq -c |sort -n >/tmp/n2.txtawk '$1>10 {print $2}' /tmp/n2.txt
- 编写shell脚本,判断输入的IP是否正确(IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)。
[root@yong-01 20180723]# vim check_ip.sh#! /bin/bashcheckip() { if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then a=`echo $1 | awk -F. '{print $1}'` b=`echo $1 | awk -F. '{print $2}'` c=`echo $1 | awk -F. '{print $3}'` d=`echo $1 | awk -F. '{print $4}'` for n in $a $b $c $d; do if [ $n -ge 255 ] || [ $n -le 0 ]; then echo "the number of the IP should less than 255 and greate than 0" return 2 fi done else echo "The IP you input is something wrong, the format is like 192.168.100.1" return 1 fi}rs=1while [ $rs -gt 0 ]; do read -p "Please input the ip:" ip checkip $ip rs=`echo $?`doneecho "The IP is right!"