January frist 2018 monday ;
Weather : clear !
1、需求:
请详细查看如下几个数字的规律,并使用shell脚本输出后面的十个数字。
10 31 53 77 105 141 …….
[root@Dasoncheng sbin]# cat n.sh
#!/bin/bash
sum=10
m=20
for i in `seq 0 14` ;
do
n=$[$m+2**$i]
sum=$[$sum+$n]
if [ $i -ge 5 ];
then
echo "$sum"
fi
done
2、需求:
写个shell,看看你的Linux系统中是否有自定义用户(普通用户),若是有,一共有几个?
[root@Dasoncheng sbin]# cat o.sh
#!/bin/bash
##How to collect zhe user who uid exceeds one thousand ;
n=`awk -F ':' '$3>1000' /etc/passwd |wc -l`
if [ $n != 0 ];
then
echo "The number of general user is $n"
else
echo "There is no general user."
fi
参考答案:
1、
试题解析:
我想大多数人都会去比较这些数字的差值:
10 31 53 77 105 141
21 22 24 28 36
但是这个差值看,并没有什么规律,而我们再仔细看的时候,发现这个差值的差值是有规律的:
10 31 53 77 105 141
21 22 24 28 36
1 2 4 8
#! /bin/bash
x=21
m=10
echo $m
for i in `seq 0 14`; do
j=$[2**$i]
m=$[$m+$x]
echo $m
x=$[$x+$j]
done
2、
##假设所有普通用户都是uid大于1000的
#!/bin/bash
n=`awk -F ':' '$3>1000' /etc/passwd|wc -l`
if [ $n -gt 0 ]
then
echo "There are $n common users."
else
echo "No common users."
fi