prasmussen / gdrive

Google Drive CLI Client
MIT License
8.99k stars 1.18k forks source link

Can't parse gdrive stdout output propely in shell script #661

Closed fritzrehde closed 2 years ago

fritzrehde commented 2 years ago

I want to parse the output of the upload command line by line with a while loop in a shell script. However, gdrive doesn't print new updates (e.g. updated progress percentage) to new lines on stdout, I think it uses \r to remove the last line so it looks like there is only one output line that is constantly getting updated. I am trying to convert all \rs into \ns with this script, but it isn't working. It keeps printing the gdrive output instead of the inside of the while loop.

gdrive upload "$FILE" \
    | tr '\r' '\n' \
    | while read LINE; do 
    echo "do something with: $LINE"
done

Any ideas how to fix this script (or is this a bug in gdrive)?

fritzrehde commented 2 years ago

I found the issue: gdrive prints to stderr instead of stdout. So this is what works for now:

stdbuf -oL gdrive upload "$FILE" 2>&1 \
    | stdbuf -i0 -oL tr '\r' '\n' \
    | grep --line-buffered '[^[:blank:]]' \
    | while read LINE; do 
    echo "do something with: $LINE"
done;

But this is still quite complicated, so I'll file another issue for better logging output.