0

我有一个 bash 脚本

counter=0
for file in /home/ec2-user/Workspace/events_parts/*
do
        counter=$[counter + 1]
        event=$(cat $file | jq '.Event')
        echo $event
        if [ "$event" = "Time Exceeded" ]  || [ "$event" = "Load Time" ]; then
                echo "Coming Here"
                jq ".url = \"$(jq '.Message' $file | sed 's/.*proxy=\([^&]*\).*/\1/')\"" $file 
        else
                jq ".url = null"
        fi
done
~   

在上面的 bash 脚本中,我试图Event从 JSON 文件中提取字段并检查两个可能的值。if [ "$event" = "Time Exceeded" ] || [ "$event" = "Load Time" ];不像我期望的那样工作。我已经验证了我正在比较的值确实存在。

4

1 回答 1

2

检查一次输出中是否有前导或尾随不需要的字符(空格、换行符...)cat $file | jq '.Event'

cat $file | jq '.Event' | hexdump -C

更新:

代替

[ "$event" = "Time Exceeded" ]

经过

[ "$event" = "Time Exceeded." ]

或替换为

[ "${event%.}" = "Time Exceeded" ]

或替换为

[[ "$event" =~ "Time Exceeded" ]]

匹配一个子字符串。${event%.}庄稼拖尾.

于 2015-03-18T01:11:18.173 回答