2017-05-23 98 views
0

中在&b和/或運算符中出現錯誤我試圖根據編解碼器和比特率來過濾視頻。如果在語句

如果編解碼器是XVID或X264或X265和比特率大於500然後鍵入= 42 否則對Xvid和比特率低於500然後類型是2和X264/X265和比特率低於500然後類型是54

下面的方法是正確的方法嗎?

if [ "$PROFILE" == "TV" ]; then 
    LEETCAT=TV 
    bitrate=$(cat $UP_INFO_FILE | grep "Height" | awk -F ':' '{print $2}' | grep -o "[0-9]*" | head -n 1) 
    echo "Height: $bitrate" 
    if [ "$bitrate" == "" ]; then 
     echo Bad bitrate 
     continue 
    fi 
    if [ "$bitrate" -ge "500" ]; then 
     LEETTYPE=41 
    else 
     LEETTYPE=6 
    fi 

else 
    if [ "$PROFILE" == "MOVIE" ]; then 
     LEETCAT=Movies 
     XviD=$(cat $UP_INFO_FILE | grep "[email protected]" | awk -F ':' '{print $2}' | grep -o "[0-9]*" | head -n 1) 
     X264=$(cat $UP_INFO_FILE | grep "Advanced Video Codec" | awk -F ':' '{print $2}' | grep -o "[0-9]*" | head -n 1) 
     X264=$(cat $UP_INFO_FILE | grep "High Efficiency Video Codec" | awk -F ':' '{print $2}' | grep -o "[0-9]*" | head -n 1) 
     bitrate=$(cat $UP_INFO_FILE | grep "Height" | awk -F ':' '{print $2}' | grep -o "[0-9]*" | head -n 1) 

     echo "Codec: $bitrate" 

     if [ "$bitrate" == "" ]; then 
      echo Bad bitrate 
      continue 
     fi 

     if [ "$XviD" == "[email protected]" ] || [ "$X264" == "Advanced Video Codec" ] || [ "$X265" == "High Efficiency Video Codec" ] && [ "$bitrate" -ge "500" ]; 
      LEETTYPE=42 
     elif [ "$XviD" == "[email protected]" ] && [ "$bitrate" -le "499" ]; then 
      LEETTYPE=2 
     elif [ "$X264" == "Advanced Video Codec" ] || [ "$X265" == "Advanced Video Codec" ] && [ "$bitrate" -le "499" ]; then 
      LEETTYPE=54 
     fi 
     echo $LEETTYPE 
    else 
     echo Bad profile:$PROFILE 
     continue 
    fi 
fi 
+1

注意,'&&'和''||有shell中的* same * precedence; 'x || y && z'相當於'(x || y)&& z',而不是'x || (y && z)'。 – chepner

+0

那麼我該如何做到正確? – Coder7878

回答

0

代替嵌套的if語句,使用elif(否則...如果...),以測試你的其他場景:

if [ "$XviD" = "[email protected]" ] || [ "$X264" = "Advanced Video Codec" ] || [ "$X265" = "High Efficiency Video Codec" ] && [ "$bitrate" -ge "500" ]; 
    LEETTYPE=42 
elif [ "$XviD" = "[email protected]" ] && [ "$bitrate" -le "499" ]; then 
    LEETTYPE=2 
elif [ "$X264" = "Advanced Video Codec" ] || [ "$X265" = "Advanced Video Codec" ] && [ "$bitrate" -le "499" ]; then 
    LEETTYPE=54 
fi