• 幕客老师召集小伙伴
  • 运维高手36项修炼
  • python自动化运维项目实战
  • nginx从入门到实战
  • 阿里云与Centos7实战

一道简单的shell面试题

Please write a shell script that:
For numbers from 1 to 100, print the following for each number:
if the number is divisible by 3, print "three";
if the number is divisible by 5, print "five";
if the number is divisible by 3 and 5, print "threefive";
otherwise, print nothing.
Each message should be printed on a seperate line, and only one of
the three messages above should be printed.
i.e. for 60, it should print only "threefive".  

 

Method one:

#########################################################################
# File Name: test.sh
# Author:Jeson
# mail:
# Created Time: Tue 29 Sep 2015 03:57:58 PM CST
#================================================================
#!/bin/bash
for number in $(seq 1 100)
do
if [ $(($number%3)) -eq 0 ] && [ $(($number%5)) -eq 0 ];then
echo "threefive $number"
continue
elif [ $(($number%3)) -eq 0 ] && [ $(($number%5)) -ne 0 ];then
echo "three $number"
elif [ $(($number%3)) -eq 0 ] && [ $(($number%5)) -eq 0 ];then
echo "five $number"
fi
done

The second method(这种方法随便写写):

 echo -e "$(seq 1 100)\n"|awk '{if($1%3 == 0 && $1%5 == 0){print "threefive"} else if($1%3 == 0 && $1%5 != 0){print "three"} else if($1%3 != 0 && $1%5 == 0){print "five"} }'

 

一道简单的shell面试题

Pingbacks已打开。

引用地址

暂无评论

发表评论