Linux Shell经典面试题(其四)
参考:http://blog.csdn.net/hiyun9/article/details/52057255
1.请用shell或Python编写一个正方形(square.sh),接受用户输入的数字。
[root@oldboy ~]# sh square1.sh
Please Enter a number:5
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
[root@oldboy ~]# sh square2.sh
Please Enter a number:9
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
■■■■■■■■■
############square1.sh 的脚本编写##############
$ vi square1.sh
#!/bin/bash
read -p "Please Enter a number:" a
for ((i=1;$i<=$a;i++))
do
for ((j=1;j<=$((2*$a));j++))
do
echo -e "+\c" #不换行
done
echo
done
############square2.sh 的脚本编写##############
vi square2.sh
#!/bin/bash
read -p "Please Enter a number:" a
for ((i=1;$i<=$a;i++))
do
for((j=1;j<=$a;j++))
do
echo -e "■ \c" #不换行
done
echo
done
2.请用shell或python编写一个等腰三角形(triangle.sh),接收用户输入的数字。
例如:
[root@oldboy ~]# sh triangle.sh
Please Enter a number:5
*
***
*****
*******
*********
[root@oldboy ~]# sh triangle.sh
Please Enter a number:8
*
***
*****
*******
*********
***********
*************
***************
############triangle.sh 的脚本编写##############
$ sh triangle.sh
#!/bin/bash
read -p "Please Enter a number:" a
for((i=1;$i<=$a;i++))
do
for((h=$((2*$a-2*$i));h>=0;h--))
do
echo -e " \c"
done
for((j=1;j<=$((2*$i-1));j++))
do
echo -e " *\c" #不换行
done
echo
done
3.请用shell或Python编写一个画直角梯形程序(right-angied-trapezoid.sh),接收用户输入的参数n,m。
[root@oldboy ~]# sh right-angied-trapezoid.sh 4 6
****
*****
******
$ vi right-angied-trapezoid.sh
#!/bin/bash
if [[ -n $1 ]] && [[ -n $2 ]];then
for ((i=$1;$i<=$2;i++))
do
for ((j=1;j<=$i;j++))
do
echo -e "*\c" #不换行
done
echo
done
else
echo "No given two int arguments, such as $0 5 6!"
fi