比较数字在壳

0

的问题

有问题比较2响应的代码的外壳。 跑卷和需要验证,如果回应的是200至400人。 此外,有机会的反应是"000"当一个服务器已关闭。

#!/bin/sh
response1="200" #curl first url
response2="000" #curl second url

if (( $response1 -ge 400 || $response1 -lt 200 || $response2 -ge 400 || $response2 -lt 200 )) ; then
  echo "Something went wrong, response code is not in success range"
  exit 1
else
  echo "Success"
  exit 0
fi

((:200-ge400 || 200 -lt200 || 000 -ge400 || 000 -lt200:中的语法错误的表达(错误的令牌"400 || 200 -lt200 || 000 -ge400 || 000 -lt200")

如果我改变括号内的[[...]]它总是返回正确的。 如果我改变-lt<和ge>=获得的以下错误:

((: 200 = 400 || 200 < 200 || 000 = 400 || 000 < 200 : 尝试分配给非可变(错误的令牌"= 400 || 200 < 200 || 000 = 400 || 000 < 200 ")

bash linux numbers script
2021-11-23 13:18:34
2

最好的答案

3

错误的操作员。 正确的写作方式是:

if (( response1 > 400 || response1 < 200 || response2 > 400 || response2 < 200 )) ; then

没有必要明确引用与 $als长,因为它是确保你的变量包含整数的数字。

2021-11-23 14:23:27

((:response1 400||response1<200||response2 400||response2<200:中的语法错误的表达(错误的令牌"400||response1<200||response2 400||response2<200")
Mykyta Shvets

你会得到什么,当你做一个 echo $BASH_VERSION 只是在前面的 if 发言?
user1934428

@MykytaShvets从错误消息,你是错的 > 操作员 response1 > 400response2 > 400 部分的表达。
Gordon Davisson

谢谢你,之后的运行在不同环境的适当bash版本-这工作
Mykyta Shvets
1

请尽量:

if [ $response1 -ge 400 ] || [ $response1 -lt 200 ] || [ $response2 -ge 400 
 ] || [ $response2 -lt 200 ] ; then
  echo "Something went wrong, response code is not in success range"
  exit 1
else
  echo "Success"
  exit 0
fi
2021-11-23 13:24:09

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................