linux - case语句中的多个条件,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:第 7 行:意外标记 `-eq 附近的语法错误
./bashtesting.sh: 第 7 行: $X -eq $Y && $Y -eq $Z && $X -eq $Z ) echo "EQUILATERAL";;*
如何一次将三个变量等同起来?
解决方案
这就是我将使用循环和关联数组的方式。
#!/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,并且可能有更好的方法来做到这一点,但我想这会让你到达那里。
推荐阅读
- javascript - 以角度将文本转换为html格式
- css - CSS块布局 - 偶数/奇数?
- ethereum - 以太坊:从 Restful API 调用智能合约函数
- javascript - 对数组中不同类型的变量进行排序
- node.js - “awsmobile init”不适用于网络反应
- angular - DatePicker Angular 材质,Angular 5
- swift - 如何使关联值相等的 Swift 枚举
- laravel - Laravel:无法打开流:成功_身份验证错误
- javascript - VS2017中的javascript验证在脚本以外的其他文件夹中
- php - 如何在 PHP 照片上传脚本功能中添加文字或徽标水印?