案例陈述中有多个条件,bash

时间:2020-06-20 21:46:23

标签: linux bash syntax

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:第7行:意外令牌'-eq

附近的语法错误

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


如何一次将这三个变量等价?

1 个答案:

答案 0 :(得分:0)

这就是我要使用循环和关联数组的方式。

#!/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
  • 它并不漂亮,它看起来像Anti Pattern,也许有更好的方法可以做到这一点,但我想这会让您到达那里。
相关问题