I'm downloading a file from the web and I would like to redirect the file to stdout
only if there is something printed to stdout
, if not I would like to skip the redirection. This is because I have an if..then..else
statement as follows:
if ! [[ -f data/worldcitiespop.csv ]]; then curl -L https://burntsushi.net/stuff/worldcitiespop.csv ; fi > data/worldcitiespop.csv 2> log
In case data/worldcitiespop.csv
already exists and if i run this again the output will be empty. I don't want that, I want to leave it populated.
CodePudding user response:
You can store curl
command output in a variable and check if there is a non-whitespace character in that variable before writing that variable content to output file:
if ! [[ -f data/worldcitiespop.csv ]]; then
out=$(curl -L https://burntsushi.net/stuff/worldcitiespop.csv 2>log)
[[ $out = *[![:space:]]* ]] && echo "$out" > data/worldcitiespop.csv
fi >