zoukankan      html  css  js  c++  java
  • 『BASH』

    一、构显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}'
    f@z ~/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"
    f@z ~/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"
  • 相关阅读:
    [语言基础] 我只想导入目标包中的一个模块,没想到目标包的其他非模块代码也被执行了。。
    [vscode] os.getcwd(),调试和命令行运行的结果不一致
    234. 回文链表
    不生成新数组的迭代器方法:forEach()&every()&some()&reduce()&reduceRight()
    合并数组并对数组排序
    为数组排序:sort()&reverse()
    从数组中间位置添加元素:unshift()方法的有一种运用
    从数组中删除元素:pop()&unshift()方法
    为数组添加元素:push()&unshift()方法
    由已有数组创建新数组:concat()&splice()方法
  • 原文地址:https://www.cnblogs.com/hadex/p/5777549.html
Copyright © 2011-2022 走看看