简体   繁体   English

case语句中的多个条件,bash

[英]Multiple conditions in case statement, bash

read X;
read Y;
read Z;
if [[ $X,$Y,$Z -ne "0" ]]; then
  if [[ $X,$Y,$Z -ge 1 && $X,$Y,$Z -le 1000 ]] && [[ $((X+Y)) -gt $Z || $((Y+Z)) -gt $X || $((X+Z)) -gt $Y ]]; then
    case $X,$Y,$Z in
      $X -eq $Y && $Y -eq $Z && $X -eq $Z ) echo "EQUILATERAL";;

    esac
  else
    echo "bye";
  fi
fi

*./bashtesting.sh: line 7: syntax error near unexpected token `-eq *./bashtesting.sh:第 7 行:意外标记 `-eq 附近的语法错误

./bashtesting.sh: line 7: $X -eq $Y && $Y -eq $Z && $X -eq $Z ) echo "EQUILATERAL";;* ./bashtesting.sh: 第 7 行: $X -eq $Y && $Y -eq $Z && $X -eq $Z ) echo "EQUILATERAL";;*


how to equate the three variables at a time?如何一次将三个变量等同起来?

This is how I would do it, with a loop and associative array.这就是我将使用循环和关联数组的方式。

#!/usr/bin/env bash

for h in x y z; do
  read -rp 'Enter input: ' input
  if [[ -z $input ]]; then
    printf >&2 '%s is empty, please try again!\n' "${input:-input}"
    exit 1
  elif [[ $input != *[0-9]* ]]; then
    printf '%s is not an int, please try again!\n' "$input"
    exit 1
  else
    declare -A num[$h]=$input
  fi
done

for i in "${num[@]}"; do
  if ! (( i == 0 )); then
    for j in "${num[@]}"; do
      if (( j > 1 && j < 100 )); then
        if (( (num[x] + num[y]) > num[z] || (num[y] + num[z]) > num[x] || (num[x] + num[z]) > num[y] )); then
          if (( num[x] == num[y] && num[y] == num[x] && num[x] == num[z] )); then
            echo equilateral
            break 2
          fi
        fi
      fi
    done
  fi
done
  • It it not pretty, It looks like an Anti Pattern , and there might be a better way to do it but this will get you there I suppose.它不漂亮,它看起来像一个Anti Pattern ,并且可能有更好的方法来做到这一点,但我想这会让你到达那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM