一、构显99乘法表
#!/usr/bin/env bash test() { for((i=1;i<10;i++)) do for((x=1;x<=$i;x++)) do echo -n "$x x $i = $(($i * $x)) " done echo -e "\n" done } test | gawk ‘BEGIN{RS=""}{print}‘
[email protected] ~/bash_script/2016-08-16 $ bash 99.sh 1 x 1 = 1 1 x 2 = 2 2 x 2 = 4 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
二、构显国际象棋8*8棋盘
#!/bin/bash line_0() { for i in {0..47} do if [[ $(($(($i / 8)) % 2)) == "0" ]] then echo -ne "\033[41m \033[00m" else echo -ne "\033[46m \033[00m" fi done } line_1() { for i in {0..47} do if [[ $(($(($i / 8)) % 2)) != "0" ]] then echo -ne "\033[41m \033[00m" else echo -ne "\033[46m \033[00m" fi done } chess() { for x in {0..23} do if [[ $(($(($x / 4)) % 2)) != "0" ]] then line_0 else line_1 fi echo -e "\n" done } chess | gawk ‘BEGIN{RS=""}{print}‘
International Chess
#!/usr/bin/bash n=1 test() { for x in {1..8} do if [[ $(($x % 2)) == "0" ]] then echo -e "\033[41m \033[00m" else echo -e "\033[46m \033[00m" fi done } while [[ $n < 9 ]] do if [[ $(($n % 2)) == "0" ]] then test > $n.txt else test | tac > $n.txt fi let n+=1 done paste {1..8}.txt > result.txt cat result.txt | sed ‘s/\t//g‘ rm result.txt {1..8}.txt
tac:International Chess
三、取10个随机数中的最大值和最小值
#!/usr/bin/env bash max=$RANDOM min=$max echo $max for((i=9;i>0;i--)) do max_old=$max max=$RANDOM echo $max if [[ $max -lt $max_old ]] then min_old=$min min=$max if [[ $min -gt $min_old ]] then min=$min_old fi max=$max_old fi done echo -e "\nLargest: $max; smallest: $min"
[email protected] ~/bash_script/2016-08-16 $ bash max_min.sh 5677 28252 27333 13922 3719 22836 2412 6102 3560 12516 Largest: 28252; smallest: 2412
Max and Min
四、求1-100整数之和
#!/usr/bin/bash read -p "Please input an +int: " N sumN=0 for((i=1;i<=$N;i++)) do sumN=$(($sumN + $i)) done echo "$sumN"
时间: 2024-09-30 02:28:12