Shibu
Shibu

Reputation: 1542

how to redirect variables to a file but not display on screen

I have this small code to do some data processing.

#!/bin/sh
export DIR=`pwd`
if [ -d "$DIR" ] 
then
    for f in "$DIR"/HistoryData*; do
    if find "$f" -newermt 2017-03-13 ! -newermt 2017-03-14 
        then
            echo "$f" >> file
        fi
        done
else
    echo "$DIR does not exists"
fi
for f in $(cat < $DIR/file);do
        awk '/CO2/ && !/VAV/{ print $0 }' "$f" >> HistoryData_CO2
    done

in line echo "$f" >> file i am trying to write the variable to file but it is also showing the value on screen. How can i suppress the value on console and just write to file

Upvotes: 3

Views: 96

Answers (1)

Inian
Inian

Reputation: 85895

It is not because of the echo that is writing to stdout, but the output of find that writes it, just suppress it to /dev/null

if find "$f" -newermt 2017-03-13 ! -newermt 2017-03-14 > /dev/null

This way, you just use the return code of the find in the if-clause and the output of the command is not printed to stdout but suppressed to the NULL device.


But generally relying on the output of find is not a good way to make your code work.

find "$DIR" -name 'HistoryData*' -newermt 2017-03-13 ! -newermt 2017-03-14  -print0 | 
    while IFS= read -r -d $'\0' line; do 
        echo "$line" >> file
    done

Upvotes: 5

Related Questions