BASH, HOWTO: Test No File Output
I had a case where I needed to use cat piped through grep to check for the occurrences of keywords. The problem is to determine if that found any occurrences. If I didn't get anything, then I don't want to append this output file to a previous one I was building and expanding.
For instance, imagine filtering down a Windows event log that was syslog forwarded to Linux.
Anyway, I was having cases where the appended stuff was somehow messing everything up, giving me zero output. Don't know why. Don't want to spend years and years thinking about it. Needed a workaround. Therefore, every step of the way, I wanted to check the result of sed or grep and then see if that generated any output.
If you want to check whether a file is over a certain amount of K, here's a handy script for that that you can use in your other Bash scripts via the backtick character.
#!/bin/bash # save me as smallfile # chmod me with chmod u+x smallfile S=$(ls -1 -s --block-size=K $1 | tr -d "K$1") # test whether file is less than 4K if [ "$S" -lt "4" ] then echo -n "1" else echo -n "0" fi
To call it from another script, do something like:
if [ "$(smallfile TEST.TXT)" -eq 0 ]
echo "file is too small"
echo "file is large enough"