zoukankan      html  css  js  c++  java
  • echo

    程序包:GNU coreutils

    shell的printf

    C语言的printf

      对“escape”的理解。英文直译:逃离、逃脱。这里代表:转义。比如“反斜线转义字符”,就是“backslash-escaped characters”。

    echo


    ●1 基本用法

      info coreutils 'echo invocation';echo - 显示一行文本,三个短选项,“-n”行尾不换行、“-e”启用转义字符、“-E”不启用转义字符(默认)。“-e”选项支持一些转义字符的(展开)用法:

    专业字符释义示例
    \
    输出反斜线

    echo "\"

    echo \

    a
    一声警报

    echo -e 'a'

    echo -e "x07"  十六进制表示

    echo -e "07"  八进制表示

    echo -e "007"  八进制表示

     退格
    echo -ne "hello"
    NNN
    表示一个八进制数值

    echo -e '041'

    echo -e '07  等效于第二行

    xHH
    表示一个十六进制数值
    echo -e 'x21'
    e
    转义
    echo -e 'e[33m'

     实例:

    [root@right mag]# echo -e '41'
    !
    [root@right mag]# echo -e '041'
    !
    [root@right mag]# echo -e 'x21'
    !
    [root@right mag]# echo -e "110145154154157"
    Hello
    [root@right mag]# echo -e '110145154154157'
    Hello
    [root@right mag]# echo -e \0110\0145\0154\0154\0157
    Hello

     

      在程序中玩个炫的,像计数器一样等待三秒的效果:

    $ echo -ne "1a";sleep 1;echo -ne "2a";sleep 1;echo -ne "3a";sleep 1
    
    

    ●2 输出随机数

    输出一个随机数:

    [root@right ~]# echo $RANDOM
    18130

    输出一个“八进制”的随机数(0~7):

    [root@right ~]# echo $[$RANDOM%8]
    5
    [root@right ~]# echo $[$RANDOM%8]
    2

    输出一个双色球的红球号(1-33):

    [root@right ~]# echo $[$RANDOM%33+1]
    33

    *验证随机数获取的随机性


      验证脚本;经验证10000次才能得到较理想的结果。

    #!/bin/bash
    # Verify that the randomness of random numbers is
    # not normally distributed.
    # The random number comes from "$RANDOM".
    
    # Generates a random number.
    #num="$[$RANDOM%10+1]"
    #echo $num
    
    # var The number of validations.
    var=$1
    
    # The array is used to count the number of occurrences
    # of random numbers. result[0] returns the value as a
    # running script.
    # sum - The sum of the array values.
    result=(0 0 0 0 0 0 0 0 0 0 0)
    sum=0
    
    # The occurrence of statistical random numbers.
    for ((i=0; i<var; i++)); do
        num="$[$RANDOM%10+1]"
        case $num in 
        1)
            let result[1]+=1
            ;;
        2)
            let result[2]+=1
            ;;
        3)
            let result[3]+=1
            ;;
        4)
            let result[4]+=1
            ;;
        5)
            let result[5]+=1
            ;;
        6)
            let result[6]+=1
            ;;
        7)
            let result[7]+=1
            ;;
        8)
            let result[8]+=1
            ;;
        9)
            let result[9]+=1
            ;;
        10)
            let result[10]+=1
            ;;
            
        *)
            result[0]=1
            echo "There was an error running the script. Error"
            echo -e "a"
            exit ${result[0]}
            ;;
        esac
    done
    
    # Output array.
    for ((i=1; i<=10; i++)); do
        echo "result[$i] = ${result[i]}"
        let sum+=${result[i]}
    done
    echo "sum = $sum"

      测试示例(接近5分钟):

    [root@right mag]# ./tes.sh 10000000
    result[1] = 999048
    result[2] = 999651
    result[3] = 1000614
    result[4] = 999725
    result[5] = 1000150
    result[6] = 999200
    result[7] = 1000972
    result[8] = 999712
    result[9] = 1000448
    result[10] = 1000480
    sum = 10000000

    ●3 输出一个数组

    字符数组输出:

    [root@right mag]# echo {a..z}
    a b c d e f g h i j k l m n o p q r s t u v w x y z


    数字数组输出:

    [root@right mag]# echo {1..9}
    1 2 3 4 5 6 7 8 9

     

    输出ASCII字符表(先写个脚本来生成):

    #!/bin/bash
    #
    
    ascii=`echo -e {000..177} | sed 's/[[:space:]]/
    /g' | grep -v 8 | grep -v 9`
    #echo $ascii
    echo "Beginning..."
    for i in $(echo $ascii); do
        echo -n "$i: "
        echo -e "$i"
    done
    一切代码都是为了生活,一切生活都是调剂
  • 相关阅读:
    Linux编辑器- vi / vim
    Java使用POI对Excel进行基本操作(4)-Excel中绘制图片
    Java使用POI对Excel进行基本操作(3)-合并单元格
    Java使用POI对Excel进行基本操作(2)-基本操作和样式设置
    Java使用POI对Excel进行基本操作(1)-概述和maven依赖
    Linux之docker搭建
    docker的个人理解
    python接口自动化-requests-toolbelt处理multipart/form-data
    python3.6安装lxml库
    pytest之assert断言
  • 原文地址:https://www.cnblogs.com/argor/p/7909903.html
Copyright © 2011-2022 走看看