2017/09/22脚本练习

2017/09/22脚本练习

  1. 编写shell脚本,计算1-100的和;
  2. 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;
  3. 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;
  4. 编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组;
  5. 编写shell脚本,截取文件test.log中包含关键词 ‘abc’ 的行中的第一列(假设分隔符为 ”:” ),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;
  6. 编写shell脚本,判断输入的IP是否正确(IP的规则是,n1.n2.n3.n4,其中1<n1<255, 0<n2<255, 0<n3<255, 0<n4<255)。
脚本1:
#!/bin/bash
sum=0
for i in `seq 1 100`
do
  sum=$[$sum+$i]
done
echo "The sum of the 1-100 is $sum"
脚本2:
[root@aming2 sbin]# cat practice2.sh 
#!/bin/bash
sum=0
read -p "Please input a number:" n
if [ -z $n ]
then
  echo "Please input a number!"
  exit
fi
n2=`echo $n |sed 's/[0-9]//g'`
if [ -n "$n2" ]
then
  echo "Please just input a number!"
  exit
else
  for i in `seq 1 $n`
  do
    sum=$[$sum+$i]
  done
  echo "The sum of 1-$i is $sum"
fi
脚本3:
[root@aming2 sbin]# cat practice3.sh 
#!/bin/bash
for i in `ls /root/`
do
  if [ -d /root/$i ]
  then
    cp -r /root/$i /tmp/
    ls -d /tmp/$i
    continue
  fi
done
脚本4:
[root@aming2 sbin]# cat practice4.sh 
#!/bin/bash
#groupadd users
#for i in `seq 0 9`
do
  useradd user_0$i -g users
done
#for i in `seq 10 100`
do
  useradd user_$i -g users
done
##This script not be done;
脚本5:
[root@aming2 sbin]# cat practice5.sh 
#!/bin/bash
grep 'abc' test.log|awk -F ':' '{print $!}' |sort -n |uniq -c |sort -n |awk '$1>10 {print $2}'
#or
awk -F':' '$0~/abc/ {print $1}' test.log >/tmp/n.txt
sort -n n.txt |uniq -c |sort -n >/tmp/n2.txt
awk '$1>10 {print $2}' /tmp/n2.txt
脚本6:
[root@aming2 sbin]# cat practice6.sh 
#!/bin/bash
read -p "Please input a ip address:" ip
if echo $ip |egrep -q '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
then
  n1=`echo $ip |awk -F '.' '{print $1}'`
  n2=`echo $ip |awk -F '.' '{print $2}'`
  n3=`echo $ip |awk -F '.' '{print $3}'`
  n4=`echo $ip |awk -F '.' '{print $4}'`
  for i in $n1 $n2 $n3 $n4 
  do
    if [ $i -ge 255 ] && [ $i -le 0 ]
    then
      echo "the number of the IP should less than 255 and greate than 0"
      exit 1
    fi
  done
  echo "The ip is ok!"
else
  echo "The IP you input is something wrong, the format is like 192.168.100.1"
  exit 2
fi

参考答案:

1. #! /bin/bash
sum=0
for i in `seq 1 100`; do
        sum=$[$i+$sum]
done
echo $sum
2. #! /bin/bash
n=0
while [ $n -lt "1" ]; do
        read -p "Please input a number, it must greater than "1":" n
done
sum=0
for i in `seq 1 $n`; do
        sum=$[$i+$sum]
done
echo $sum
3. #! /bin/bash
cd /root
for f in `ls `; do
        if [ -d $f ] ; then
                cp -r $f /tmp/
        fi
done
4. #! /bin/bash
groupadd users
for i in `seq 0 9`; do
        useradd -g users user_0$i
done
for j in `seq 10 100`; do
        useradd -g users user_$j
done
5. #! /bin/bash
awk -F':' '$0~/abc/ {print $1}' test.log >/tmp/n.txt
sort -n n.txt |uniq -c |sort -n >/tmp/n2.txt
awk '$1>10 {print $2}' /tmp/n2.txt
6. #! /bin/bash
checkip() {
        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=1
while [ $rs -gt 0 ]; do
    read -p  "Please input the ip:" ip
    checkip $ip
    rs=`echo $?`
done
echo "The IP is right!"

转载于:https://my.oschina.net/u/3651233/blog/1541952

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值