Skip to main content
Welcome guest. | Register | Login | Post

BASH, HOWTO: Test No File Output

1 reply [Last post]
supermike's picture
Offline
Joined: 2006-02-17

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 ]
then
echo "file is too small"
else
echo "file is large enough"
fi

free-zombie's picture
Offline
Joined: 2006-03-08

cat FILE | grep WORD
[[ $? -eq 0 ]] && APPEND

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.