linux - 读取应该只是数字的输出

标签 linux bash unix

在unix中,我需要读取一个应该是数字的o/p,如果假设用户输入字符,它应该抛出“无效标识符”错误。这在 if 循环中可能吗?

最佳答案

试试这个...

unset get_num
while [[ ! ${get_num} =~ ^[0-9]+$ ]]; do
    echo "Please enter in a number:"
    read get_num
done
echo This is a number :  ${get_num}

关于linux - 读取应该只是数字的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957503/

相关文章:

bash - if 语句中的多个一元运算符

regex - Sed 通配符——替换某些字符的中间

c - 为C代码建立调用树

linux - 如何从 Unix/Linux 中的文件名中删除模式

python - getUser 返回 EDAMSystemException errorCode=8

c - 在 Linux 中,如果我尽可能快地调用 send() 为什么会丢失 UDP 数据包?

java - 如何将 Java 指向符号链接(symbolic link)

linux - 在 Linux 下,有没有比 C 延迟更低的语言? (除了汇编程序)

regex - 在 bash 中使用正则表达式提取版本

c - 在 C 中显示文件的所有者名称