regex - to check if input is number in csh(unix)

喜欢而已 提交于 2019-12-13 08:22:09

问题


Please help. How to make regex work in an if condition, to check user input(in 2nd parameter) must be not equal to any number?

set regex="^[0-9]+$"
if ($#argv == 2 && $2 != $regex) then
# do this

回答1:


grep-based solution that only works for non-negative integers.

#!/bin/csh

# Note this requires 2 command line arguments, and checks the second one
# cshell arguments are 0-indexed.
if ($#argv == 2) then

    # Note the different grep regexp syntax, and you must use single quotes
    set test = `echo $2 | grep '^[0-9]*$'`
    if (($test) || ($test == 0)) then
        echo "Bad input"
    else
        echo "Ok!"
    endif
endif


来源:https://stackoverflow.com/questions/26457720/regex-to-check-if-input-is-number-in-cshunix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!