且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式 - 检查输入是否是csh中的数字(unix)

更新时间:2023-11-28 13:26:40

基于grep的解决方案仅适用于非负整数

#!/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