if文文字列の比較

if文文字列の比較

1行あたり14個の値を含むcsvファイルを読み込んでいます。 10番目の[]値を16進値に変換したいと思いますf10。これを行うには、if7番目の[ f7]値をそのまま維持する必要があります。またはEnHrEnStSpJbChemTrTmPmTmBrTmHyTm

これは私のコードの一部です。

echo "Test 3 f7 is $f7"   

    if [ "$f7" == "EnHr" ] || [ "$f7" == "EnSt" ] || [ "$f7" == "SpJb" ] || [ "$f7" == "Chem" ] || [ "$f7" == "TrTm" ] || [ "$f7" == "PmTm" ] || [ "$f7" == "BrTm" ] || [ "$f7" == "HyTm" ]
        then
            echo -e "\t\t\t\t\tRAW VAL is $f10"   #print raw val
            f10=`echo "ibase=16; $f10" | bc`      #convert from HEX to Dec
            echo -e "\t\t\t\t\tinside the if loop\tSen: $f7\tVal: $f10" #reprint the converted value as DEC
            inside=$((inside+1))
    fi

そして印刷するとecho -e "\t\t\t\t\tRAW VAL is $f10"hex 値が出ますが、次の行には 12 月の Val が 0 で出てきますね。

これが私が今まで得た結果です。

Test 1             line count is 26
Test 2
Test 3 f7 is Chem
                                        RAW VAL is 0000
                                        inside the if loop      Sen: Chem       Val: 0
Test 4
Test 1             line count is 27
Test 2
Test 3 f7 is TrTm
                                        RAW VAL is 0019c4ef
(standard_in) 1: syntax error
                                        inside the if loop      Sen: TrTm       Val:
Test 4
Test 1             line count is 28
Test 2
Test 3 f7 is PmTm
                                        RAW VAL is 000b57bf
(standard_in) 1: syntax error
                                        inside the if loop      Sen: PmTm       Val:
Test 4
Test 1             line count is 29
Test 2
Test 3 f7 is BrTm
                                        RAW VAL is 00022d51
(standard_in) 1: syntax error
                                        inside the if loop      Sen: BrTm       Val:
Test 4
Test 1             line count is 30
Test 2
Test 3 f7 is HyTm
                                        RAW VAL is 00004ff9
(standard_in) 1: syntax error
                                        inside the if loop      Sen: HyTm       Val:
Test 4

Test 1-4がすぐそこにあるので、コードがどこにあるのかがわかります。実際の意味がない単純なエコーだけです。私の懸念は次のとおりです。

質問1:(standard_in) 1: syntax errorそれがどこから来るのか、どうすれば削除できますか?

質問2:そして、出力を見ると、生データの16進値が10進数に変換されないのはなぜですか?

ベストアンサー1

POSIXlyでは、16進数の10進値を取得できます。たとえば、次のようになります。

hex=10
echo "$((0x$hex))"

16

あなたは本物[これらのテストをすべて実行する必要はありません]。私は次がうまくいくべきだと思います。

case $f7 in
(EnHr|EnSt|SpJb|Chem|[BT]rTm|PmTm|HyTm)
    printf "\t\t\t\t\t%s%b"       \
       "HEX VALUE is" ":\t$f10"   \
       '' "inside case loop.\t\t" \
       Sen ":\t$f7"               \
       Val ":\t$((!(inside+=1)|0x$f10))"
esac

おすすめ記事