Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...