I am using solutions from Test if a command outputs an empty string. In addition to those solutions, I want to print command's output if its not empty.
I was wondering would it be possible to print command's output if its not empty by calling the command only one time.
I can do it by re-calling the same command inside the
if condition, but now second call may consume additional CPU usage, since we already obtained its result.
Possible approach could be, where the same command is called two times:
if [[ $(ls -A) ]]; then ls -A else echo "no files found" fi
output=$(git diff --ignore-blank-lines --color-words HEAD | tr -d ' \n\r\t ' | wc -c) if [ "$output" -gt 0 ]; then git diff --ignore-blank-lines --color-words HEAD else echo "diff is empty" fi
lsis not the best tool for the job. It would be better to
set -- *(possibly setting
bashfirst) and then look at
moreutilspackage. You'll need to install that package.