linux shell 实现九九乘法
1、
[root@centos7pc1 test2]# ls test.sh [root@centos7pc1 test2]# cat test.sh ## 脚本 #!/bin/bash for i in `seq 9` do for j in `seq 9` do echo "$i * $j" | bc >> temp done done awk '{if(NR % 9 == 0) {printf("%02g\n",$0)} else {printf("%02g ", $0)}}' temp > result.txt rm -f temp [root@centos7pc1 test2]# bash test.sh ## 执行程序 [root@centos7pc1 test2]# ls result.txt test.sh [root@centos7pc1 test2]# cat result.txt ## 结果文件 01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 06 12 18 24 30 36 42 48 54 07 14 21 28 35 42 49 56 63 08 16 24 32 40 48 56 64 72 09 18 27 36 45 54 63 72 81
2、sed + awk实现
root@PC1:/home/test2# seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d\t", i, NR, i*NR, i==NR); printf("\n")}' 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
2 参考:https://mp.weixin.qq.com/s?__biz=MzU5NDg5MzM5NQ==&mid=2247495592&idx=1&sn=0780cc96bc18bf3ba5b1b50e243a8e9a&chksm=fe78e094c90f698244a1acb0b33ab4cb9ab215dd86d4e227c6a12b2387de2f42ebadd024e517&mpshare=1&scene=23&srcid=0501IxzLQzAVxjYg4OhuPhnU&sharer_sharetime=1651338673976&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd