shell中如何生成随机数?

简介: shell中如何生成随机数?
########################
# Generate a random string
# Arguments:
#   -t|--type - String type (ascii, alphanumeric, numeric), defaults to ascii
#   -c|--count - Number of characters, defaults to 32
# Arguments:
#   None
# Returns:
#   None
# Returns:
#   String
#########################
generate_random_string() {
    local type="ascii"
    local count="32"
    local filter
    local result
    # Validate arguments
    while [[ "$#" -gt 0 ]]; do
        case "$1" in
        -t | --type)
            shift
            type="$1"
            ;;
        -c | --count)
            shift
            count="$1"
            ;;
        *)
            echo "Invalid command line flag $1" >&2
            return 1
            ;;
        esac
        shift
    done
    # Validate type
    case "$type" in
    ascii)
        filter="[:print:]"
        ;;
    alphanumeric)
        filter="a-zA-Z0-9"
        ;;
    numeric)
        filter="0-9"
        ;;
    *)
        echo "Invalid type ${type}" >&2
        return 1
        ;;
    esac
    # Obtain count + 10 lines from /dev/urandom to ensure that the resulting string has the expected size
    # Note there is a very small chance of strings starting with EOL character
    # Therefore, the higher amount of lines read, this will happen less frequently
    result="$(head -n "$((count + 10))" /dev/urandom | tr -dc "$filter" | head -c "$count")"
    echo "$result"
}

c8d5663c7af0483cb6cafc3c4505df83.pngtr 中-d -c

# 字符集补集,从输入文本中将不在补集中的所有字符删除
echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n'

5aeda60eccbf48378f9a3b5395c7c87e.png

目录
相关文章
|
存储 安全 开发工具
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
1125 4
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
|
NoSQL Shell 测试技术
shell命令行并行神器 - parallel
GNU parallel 是一个 shell 工具,用于使用一台或多台计算机并行执行作业。作业可以是单个命令或必须为输入中的每一行运行的小脚本。典型的输入是文件列表、主机列表、用户列表、URL 列表或表列表。作业也可以是从管道读取的命令。 GNU parallel 然后可以拆分输入并将其通过管道并行传输到命令中。
928 0
|
人工智能 机器人 Shell
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
【shell】shell数组的操作(定义、索引、长度、获取、删除、修改、拼接)
|
人工智能 弹性计算 持续交付
Docker与AI结合,会让部署更加丝滑吗?
Docker与AI结合,会让部署更加丝滑吗?
933 25
|
运维 关系型数据库 MySQL
安装MySQL8数据库
本文介绍了MySQL的不同版本及其特点,并详细描述了如何通过Yum源安装MySQL 8.4社区版,包括配置Yum源、安装MySQL、启动服务、设置开机自启动、修改root用户密码以及设置远程登录等步骤。最后还提供了测试连接的方法。适用于初学者和运维人员。
1100 0
|
Shell Perl
在Shell脚本中,检查一个进程是否正在运行
在Shell脚本中,检查一个进程是否正在运行
4619 1
|
JSON Kubernetes 前端开发
保姆级二进制安装高可用k8s集群文档(1.23.8)
市面上有很多方式,最终主要分两种,kubeadmin 和二进制.
2720 0
|
Linux Docker 容器
Redhat离线安装docker
Redhat离线安装docker
806 1
|
Java Linux API
单调时钟与墙上时钟的区别?ntp如何优雅校时?
单调时钟与墙上时钟的区别?ntp如何优雅校时?
809 0
单调时钟与墙上时钟的区别?ntp如何优雅校时?
|
消息中间件 监控 安全
Linux- rsync企业级实战
从字面意思上,rsync 可以理解为 remote sync(远程同步),但它不仅可以远程同步数据(类似于 scp 命令),还可以本地同步数据(类似于 cp 命令)。不同于 cp 或 scp 的一点是,使用 rsync 命令备份数据时,不会直接覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
623 0

热门文章

最新文章