0

似乎我不断收到以下 -gt 或 == 错误。有人可以帮忙吗?

flag= echo $flightSeatBooked | awk -F[,] '{print match($flightSeatBooked, $orderSeats)}'
if $flag == 0; then
        echo "Success";
else
        echo "fail";

鉴于:

flightSeatBooked= 9;,A1,A2,A3,A4,B2,E4,C3,B3,D3,D2,E1,E2,C2,B4,C4,D4,C1,D1,E3,B1
orderSeats= B2 (not found in the variable)

预期输出:成功

4

3 回答 3

1

这是如何做你问的:

flag=$(awk -v flseat="$flightSeatBooked" -v orseat="$orderSeats" 'BEGIN{print index(flseat, orseat)}')

if [ $flag -eq 0 ]; then
        echo "Success"
else
        echo "fail"
fi

但我不认为你问的是一个好主意。它至少应该是这样的:

awk -v flseat="$flightSeatBooked" -v orseat="$orderSeats" 'BEGIN{exit index(flseat, orseat)}')

if [ $? -eq 0 ]; then
        echo "Success"
else
        echo "fail"
fi

你可能真正需要的是这样的:

case "$flightSeatBooked" in
   *"$orderSeats"* ) echo "fail";;
   * ) echo "Success" ;;
esac

检查逻辑(因为我没有!),但希望你能得到方法。

于 2012-11-23T20:24:51.457 回答
1

Quite a few mistakes. Change it like this:

flag=$(echo $flightSeatBooked | awk -v flseat=$flightSeatBooked -v orseat=$orderSeats '{print match(flseat, orseat)}')

if [ $flag -eq 0 ]; then
        echo "Success";
else
        echo "fail";
fi
  1. Command substitution has been done using the $(...) notation.
  2. It is not a good practice to use the shell variables directly in awk, and hence passed shell variables to awk using -v.
  3. The syntax of if used was incorrect, updated to correct it.
于 2012-11-23T11:02:51.560 回答
0

您还可以在下面使用它来检查 $orderSeats 是否在 $flightSeatBooked 中。如果它在则返回匹配的字符串的长度或返回 0。

expr "$flightSeatBooked" : ".*,${orderSeats},"
于 2012-11-23T11:42:10.427 回答