Piping curl followed by an echo some times truncates the output when using tail

Given the following 2 lines in a bash script,

LOCATION=$(curl -i -H "Windmill-Name: $APPLICATION_NAME" -H "Windmill-Identifier: $CFBundleIdentifier" -F "ipa=@$IPA" -F "plist=@$PLIST" $WINDMILL_BASE_URL/windmill/rest/windmill/$USER | grep ^Location | awk '{print $2}')
echo "[windmill] Use $LOCATION for accessing '$APPLICATION_NAME'"

In some cases, the echo string appears like below.

for accessing 'MultiPartIOSDemo' [a truncated $LOCATION]

The behaviour is not consistent but when it reproduces, the malformed output is consistent (ie a truncated $LOCATION at some range).

It looks like echo outputs the string to the buffer but the piping of curl isn't yet done and ends up writing its output on top.

Can't quite tell.

Update

Tried all of your suggestions, but the same problem occurs.

Have now dropped grep and the script looks like so

LOCATION=$(curl -i -H "Windmill-Name: $APPLICATION_NAME" -H "Windmill-Identifier: $CFBundleIdentifier" -F "ipa=@$IPA" -F "plist=@$PLIST" "$WINDMILL_BASE_URL/windmill/rest/windmill/$USER" | awk -W '/^Location/ {print $2}')
echo "[windmill] Use $LOCATION for accessing '$APPLICATION_NAME'"

Here are some more details.

The script that includes the above lines is wrapped in a

(
(
# bash script
) 2>&1 | tee $HOME/.windmill/$PROJECT_NAME.log

) 2>&1 | tee $HOME/.windmill/windmill.log

Hence the output of the echo is on both logs.

Just noticed that the above behaviour occurs while tailing , eg

tail -fn 20 ~/.windmill/windmill.log

However if I do a

more ~/.windmill/windmill.log

I can see that the echo message appears correctly. Notice the newline character "^M". Wondering if it has anything to do with the way tail parses the log.

[windmill] Use [correct $LOCATION] ^M for accessing 'MultiPartIOSDemo'

Clarified Question
Guess after all the above, there are really 2 questions.

  • Under what circumstances does the ^M appear in the log?
  • Why is tail parsing the log wrong, ie parsing ^M in such a way that it first outputs the "for accessing 'MultiPartIOSDemo'" then the "Use $LOCATION" on top.

  • I would (1) change

    $WINDMILL_BASE_URL/windmill/rest/windmill/$USER
    

    to

    "$WINDMILL_BASE_URL/windmill/rest/windmill/$USER"
    

    (2) use tee to debug and (3) use awk to match "^location" instead of grep

    LOCATION=$(curl -i -H "Windmill-Name: $APPLICATION_NAME" -H "Windmill-Identifier: $CFBundleIdentifier" -F "ipa=@$IPA" -F "plist=@$PLIST" "$WINDMILL_BASE_URL/windmill/rest/windmill/$USER" | tee /tmp/debug.$$ | awk ' /^Location/ {print $2}')
    echo "[windmill] Use $LOCATION for accessing '$APPLICATION_NAME'"
    

    Perhaps curl is returing an error. You can check the results and only parse it if no error occured

    # Create a trace file
    tfile=/tmp/trace.$$
    
    # uncomment this line if you want to delete the trace file at the end of the script
    #trap "/bin/rm $tfile" 0 1 15
    
    curl -i -H "Windmill-Name: $APPLICATION_NAME" -H "Windmill-Identifier: $CFBundleIdentifier" -F "ipa=@$IPA" -F "plist=@$PLIST" "$WINDMILL_BASE_URL/windmill/rest/windmill/$USER" >$tfile
    status=$?
    if [ $status -eg 0 ]
    then
        echo curl was successful
        LOCATION=$(awk '/^Location/ {print $2}' <$tfile)
        echo "[windmill] Use $LOCATION for accessing '$APPLICATION_NAME'"
    else
        echo curl exited with status $status
    fi
    
    链接地址: http://www.djcxy.com/p/42702.html

    上一篇: 将尾部输出重定向到程序中

    下一篇: 当使用尾部时,管道卷曲后跟回波有时会截断输出