【shell】Linux shell 之 打印99乘法表详解

前端之家收集整理的这篇文章主要介绍了【shell】Linux shell 之 打印99乘法表详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

打印99乘法表在任何语言中都是一个必写的程序,特别是学习了循环之后。

打印99乘法表第一步

众所周知,99乘法表的格式为x * y = z
所以我们至少需要两个参数,一个为x,一个为y,这里我们使用 i 和 j 来表示。
x 和 y 都不会超过 9 ,并具有一定的规律,比如 1 x 2,1 x 3

根据上面的分析,我们首先需要把 i 和 j 先写出来,写就需要用到我们的双层for循环了。
#!/bin/bash -
for i in `seq 9`
do
    for j in `seq 9`
        do
        echo $j,$i
    done

done

执行结果

[root@XiaoPeng scripts]# bash chengfa.sh 
1,1
2,1
3,1
4,1
5,1
6,1
7,1
8,1
9,1
1,2
2,2
3,2
4,2
5,2
6,2
7,2
8,2
9,2
1,3
2,3
3,3
4,3
5,3
6,3
7,3
8,3
9,3
1,4
2,4
3,4
4,4
5,4
6,4
7,4
8,4
9,4
1,5
2,5
3,5
4,5
5,5
6,5
7,5
8,5
9,5
1,6
2,6
3,6
4,6
5,6
6,6
7,6
8,6
9,6
1,7
2,7
3,7
4,7
5,7
6,7
7,7
8,7
9,7
1,8
2,8
3,8
4,8
5,8
6,8
7,8
8,8
9,8
1,9
2,9
3,9
4,9
5,9
6,9
7,9
8,9
9,9
[root@XiaoPeng scripts]#

到了这里,大家可能已经找到一点规律了,i 和 j我们已经得出来了,所以我们要给 i 和 j 进行一个乘法运算即可。

!/bin/bash -
for i in `seq 9`
do
    for j in `seq 9`
        do
        echo "$i x $j = `echo $(($i*$j))`"      # 运算
    done

done

执行结果

[root@XiaoPeng scripts]# bash chengfa.sh 
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81

到这一步之后,整个雏形就出现了,这时候我们会发现一个问题,那就是每个公式都是单独的一行,所以我们需要修改一些程序,echo 中的 -n 参数便是取消跨行输出

修改部分
 echo -n "$i x $j = `echo $(($i*$j))`"

执行结果

[root@XiaoPeng scripts]# bash chengfa.sh 
1 x 1 = 11 x 2 = 21 x 3 = 31 x 4 = 41 x 5 = 51 x 6 = 61 x 7 = 71 x 8 = 81 x 9 = 92 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 = 142 x 8 = 162 x 9 = 183 x 1 = 33 x 2 = 63 x 3 = 93 x 4 = 123 x 5 = 153 x 6 = 183 x 7 = 213 x 8 = 243 x 9 = 274 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 365 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 456 x 1 = 66 x 2 = 126 x 3 = 186 x 4 = 246 x 5 = 306 x 6 = 366 x 7 = 426 x 8 = 486 x 9 = 547 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 638 x 1 = 88 x 2 = 168 x 3 = 248 x 4 = 328 x 5 = 408 x 6 = 488 x 7 = 568 x 8 = 648 x 9 = 729 x 1 = 99 x 2 = 189 x 3 = 279 x 4 = 369 x 5 = 459 x 6 = 549 x 7 = 639 x 8 = 729 x 9 = 81[root@XiaoPeng scripts]#

到了步之后,大家会发现,所有的变量都是挨着的,怎么改呢?我们只需要在echo 后面加两个空格就好啦!

修改部分
 echo -n "$i x $j = `echo $(($i*$j))`  ”     # ”前面加了两个空格。

执行结果

1 x 1 = 1  1 x 2 = 2  1 x 3 = 3  1 x 4 = 4  1 x 5 = 5  1 x 6 = 6  1 x 7 = 7  1 x 8 = 8  1 x 9 = 9  2 x 1 = 2  2 x 2 = 4  2 x 3 = 6  2 x 4 = 8  2 x 5 = 10  2 x 6 = 12  2 x 7 = 14  2 x 8 = 16  2 x 9 = 18  3 x 1 = 3  3 x 2 = 6  3 x 3 = 9  3 x 4 = 12  3 x 5 = 15  3 x 6 = 18  3 x 7 = 21  3 x 8 = 24  3 x 9 = 27  4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16  4 x 5 = 20  4 x 6 = 24  4 x 7 = 28  4 x 8 = 32  4 x 9 = 36  5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25  5 x 6 = 30  5 x 7 = 35  5 x 8 = 40  5 x 9 = 45  6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36  6 x 7 = 42  6 x 8 = 48  6 x 9 = 54  7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49  7 x 8 = 56  7 x 9 = 63  8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64  8 x 9 = 72  9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81

好看多了吧,但是我们还会发现,输出的结果并不会换行,因为99乘法表是需要换行的,1 x 1 =2 一行,1 x 2 =2 2 x 2 = 4 一行。所以我们需要把程序继续修改,比如我们规定,每当 j 循环结束我们就换行一次。

修改部分
for i in `seq 9`
do
    for j in `seq 9`
        do
        echo -n "$i x $j = `echo $(($i*$j))`  "
    done
echo ""   # 打印空行
don

执行结果

[root@XiaoPeng scripts]# bash chengfa.sh 
1 x 1 = 1  1 x 2 = 2  1 x 3 = 3  1 x 4 = 4  1 x 5 = 5  1 x 6 = 6  1 x 7 = 7  1 x 8 = 8  1 x 9 = 9  
2 x 1 = 2  2 x 2 = 4  2 x 3 = 6  2 x 4 = 8  2 x 5 = 10  2 x 6 = 12  2 x 7 = 14  2 x 8 = 16  2 x 9 = 18  
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9  3 x 4 = 12  3 x 5 = 15  3 x 6 = 18  3 x 7 = 21  3 x 8 = 24  3 x 9 = 27  
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16  4 x 5 = 20  4 x 6 = 24  4 x 7 = 28  4 x 8 = 32  4 x 9 = 36  
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25  5 x 6 = 30  5 x 7 = 35  5 x 8 = 40  5 x 9 = 45  
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36  6 x 7 = 42  6 x 8 = 48  6 x 9 = 54  
7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49  7 x 8 = 56  7 x 9 = 63  
8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64  8 x 9 = 72  
9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81

这时候大家会发现,我们正常的乘法表是直角三角形,现在的是矩形输出,为什么呢?大家可能发现了,因为我们有部分的公式是重复的,如果 1 x 2 = 2和 2 x 1 =2 重复了,那么怎么去解决呢?大家可以发现一个规律,1 x 2 =2,2 x 2 = 4 ,还有 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 ,同一行中,前面一个数不会大于后面的数,即 j x i = z,j 不会大于 i

修改部分
for i in `seq 9`
do
    for j in `seq 9`
        do
        [ $j -le $i ] && echo -n "$i x $j = `echo $(($i*$j))`  "   # 如果 j 小与等于 i 才会打印式子
    done
echo ""
done

执行结果

[root@XiaoPeng scripts]# bash chengfa.sh 
1 x 1 = 1  
2 x 1 = 2  2 x 2 = 4  
3 x 1 = 3  3 x 2 = 6  3 x 3 = 9  
4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16  
5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25  
6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36  
7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49  
8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64  
9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81

至此完毕。
版权所有arppinging欢迎关注的我的个人微信公众号

原文链接:https://www.f2er.com/bash/388371.html

猜你在找的Bash相关文章