开发者

Unix ksh: multiple conditions in if statement, testing 3 variables at the same time

开发者 https://www.devze.com 2023-02-25 08:03 出处:网络
I am trying to test user input, basically var1, var2 and var3, needs to be in c1, c2,c3, c4, c5, c6, c7.

I am trying to test user input, basically var1, var2 and var3, needs to be in c1, c2,c3, c4, c5, c6, c7. I am in ksh

I tried an if statement with -a and -o as well and it did not work, I tried playing with [] surrounding variables, doesn't work, I tried omitting the &&, nothing Can anyone help?

example:

if [[ "$var1" && "$var2" && "$var3"   =  "$c1" || "$c2" || "$c3" || "$c4" || "$c5" || "$c6" || "$c7" ]]

Example:

case "$var1" -a "$var2" -a "$var3" in 
                    "$c1"|"$c2"|"$c3"|"$c4"|"$c5"|"$c6"|"$c7")

I also tried putting c1 to c7 inside a select statement, but that doesn't a开发者_如何学Gollow me to select 3 variables from it. Any solution? Thank you


You can't test multiple variables like that.

Write a function to check one variable is valid, then chain calls together.

isValid()
{
    local value=$1
    case $value in 
        "$c1"|"$c2"|"$c3"|"$c4"|"$c5"|"$c6"|"$c7")
              return 0
              ;;
    esac
    return 1
}

if isValid $var1 && isValid $var2 && isValid $var3 ; then
   echo "All valid"
fi
0

精彩评论

暂无评论...
验证码 换一张
取 消