企业shell编程基础问题解决实践

1.请用shell或Python编写一个正方形(oldboy4.sh),接收用户输入的数字。
例如:用+号实现

xiaorui@xiaorui:~/script$ vim oldboy_squarel.sh
xiaorui@xiaorui:~/script$ ./oldboy_squarel.sh
Please Enter a number:4
++++++++
++++++++
++++++++
++++++++
xiaorui@xiaorui:~/script$ cat oldboy_squarel.sh
#/biin/bash

read -p "Please Enter a number:" num

for ((i=0;i<$num;i++))
{
    for((j=0;j<$num*2;j++))
    {
        echo -n "+"
    }
    echo
}

2.请用shell或Python编写一个等腰三角形(oldboy2_triangle.sh),接收用户输入的数字。
例如:用*号实现

xiaorui@xiaorui:~/script$ vim oldboy2_triangle.sh
xiaorui@xiaorui:~/script$ ./oldboy2_triangle.sh
Please Enter the num:3
   *
  ***
 *****
xiaorui@xiaorui:~/script$ ./oldboy2_triangle.sh
Please Enter the num:5
     *
    ***
   *****
  *******
 *********
xiaorui@xiaorui:~/script$ cat oldboy2_triangle.sh
#/bin/bash

read -p "Please Enter the num:" num

for ((i=0;i<$num;i++))
{
    for ((j=0;j<$num-$i;j++))
    {    
        echo -n " "
    }    
    for ((k=0;k<$i*2+1;k++))
    {
        echo -n "*"
    }
    echo

}

 3.请用shell或Python编写一个画直角梯形程序(oldboy3.sh),接收用户输入的参数n,m

read读入:
xiaorui@xiaorui:~/script$ ./oldboy3.sh Please Enter the min num:3 Please Enter the max num:8 *** **** ***** ****** ******* ******** xiaorui@xiaorui:~/script$ cat oldboy3.sh #/bin/bash read -p "Please Enter the min num:" num1 read -p "Please Enter the max num:" num2 for ((i=0;i<$num2-$num1+1;i++)) { for ((j=0;j<$num1+$i;j++)) { echo -n "*" } echo }
命令行直接接参数:
xiaorui@xiaorui:~/script$ ./oldboy3.sh 2 5
**
***
****
*****
xiaorui@xiaorui:~/script$ cat oldboy3.sh
#/bin/bash
num1=$1
num2=$2
for ((i=0;i<$num2-$num1+1;i++))
{
    for ((j=0;j<$num1+$i;j++))
    {    
        echo -n "*"
    }    
    echo
}

原题来源:http://oldboy.blog.51cto.com/2561410/1718607

原文地址:https://www.cnblogs.com/migongci0412/p/5016869.html