Bash文字列と整数の比較

Bash文字列と整数の比較

ここ[[文字列と整数の比較が許可されていることを確認しました。

    [usr1@host dir]$ echo $count1
    [1] "0"

    [usr1@host dir]$ echo $count2
    13188

    [usr1@host dir]$ if [[ $count1 -ne $count2 ]]
    > then
    > echo "NE"
    > fi
    bash: [[: [1] "0": syntax error: operand expected (error token is "[1] "0"")

   #this worked fine at one point
   if [[ $count1 -ne $count2 ]] then
      echo "NE"
   fi
   syntax error near unexpected token 'then'  

   if [[ $count1 -ne $count2 ]];    then
      echo "NE"
   fi
   [[: [1] "0": syntax error: operand expected (error token is "[1] "0"")

構文がどのように機能するかは非常に混乱しています。さまざまなシナリオにどのように対応しますか?値がどのように変更されても構文エラーを回避するにはどうすればよいですか? (だから今エラーが発生しているようです。)

ベストアンサー1

変数にcount1文字列が含まれています[1] "0"。 8文字の文字列です。いいえ整数。

価値が正当であっても、"0"存在のテストは[[ $count1 -ne $count2 ]]$count1"0"とても違うfromは単一の文字列[[ "$count1" -ne "$count2" ]]です。$count10

おすすめ記事