1 #! /bin/bash
2 sum=0
3 for i in `seq 1 100`; do
4 sum=$[$i+$sum]
5 done
6 echo $sum
计算-00和
2. 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止;
1 #! /bin/bash
2 n=0
3 while [ $n -lt "1" ]; do
4 read -p "Please input a number, it must greater than "1":" n
5 done
6
7 sum=0
8 for i in `seq 1 $n`; do
9 sum=$[$i+$sum]
10 done
11 echo $sum
12
13
判断输入数字
3. 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下;
1 #! /bin/bash
2 for f in `ls /root/`; do
3 if [ -d $f ] ; then
4 cp -r $f /tmp/
5 fi
6 done
拷贝目录
4. 编写shell脚本,批量建立用户user_00, user_01, … ,user_100并且所有用户同属于users组;
1 #! /bin/bash
2 groupadd users
3 for i in `seq 0 9`; do
4 useradd -g users user_0$i
5 done
6
7 for j in `seq 10 100`; do
8 useradd -g users user_$j
9 done
批量建用户
5. 编写shell脚本,截取文件test.log中包含关键词’abc’的行中的第一列(假设分隔符为”:”),然后把截取的数字排序(假设第一列为数字),然后打印出重复次数超过10次的列;
1 #! /bin/bash
2 checkip()
3 {
4 if echo $1 |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then
5 a=`echo $1 | awk -F. '{print $1}'`
6 b=`echo $1 | awk -F. '{print $2}'`
7 c=`echo $1 | awk -F. '{print $3}'`
8 d=`echo $1 | awk -F. '{print $4}'`
9
10 fi
11
12 for n in $a $b $c $d; do
13 if [ $n -ge 255 ] || [ $n -le 0 ]; then
14 echo "the number of the IP should less than 255 and greate than 0"
15 return 2
16 else
17 echo "The IP you input is something wrong, the format is like 192.168.100.1"
18 return 1
19 fi
20 done
21
22 }
23
24
25
26 rs=1
27 while [ $rs -gt 0 ]; do
28 read -p "Please input the ip:" ip
29 checkip $ip
30 rs=`echo $?`
31
32 done
33
34 echo "The IP is right!"
35 判断IP地址
判断IP地址
7.编写一个脚本,打印任何数的乘法表。如输入3则打印
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9