如何将脚本的输出保存到文件中?

如何将脚本的输出保存到文件中?

我有以下脚本:


LOOKUPS=/tmp/$0.LOOKUPS.$$
OK=/tmp/$0.OK.$$
KO=/tmp/$0.KO.$$
NOVENUE=/tmp/$0.NOVENUE.$$

trap "rm -f $LOOKUPS $OK $KO $NOVENUE" 0 1 2 3 4 5 13 15

tu=`$APP_SERVICE | cut -b 9-11`
date=`date +%Y%m%d`
echo "date before change: ${date}"
H=$(date +%H)
if [[ $H -ge 0 && $H -lt 9 ]]
then
        trt_date=$(date --date 'yesterday' +'%Y%m%d')
       echo "date changed: ${date}"
else
        echo "date didnt change: ${date}"
fi

grep "^$date.*lookup .. for.*" $UTP_LOG_DIR/logs.* | awk '{print $13"_"$4" "$11" "$17"="$19" "$20"="$23}' | sed 's/\[.*\]//g' | sed -e 's/string{//g' -e 's/, / /g' -e 's/=(/=/g' -e 's/)).//g' -e 's/}//g' | sort -u > $LOOKUPS

grep " OK " $LOOKUPS | awk '{print $1" "$3" "$4}' > $OK
grep " KO " $LOOKUPS | awk '{print $1" "$3" "$4}' > $KO
grep "^$date.*Can't find a venue for" $UTP_LOG_DIR/logs.* | sed 's/-$//g' | awk '{print $NF}' | sort -u > $NOVENUE

while read line
do
        if [ $(grep "$line" $OK | wc -l) -eq 0 ]
        then
                isin=$(echo $line | cut -d'_' -f1)
                feed=$(echo $line | cut -d'_' -f2,3)
                filters=$(echo "$line" | cut -d' ' -f2-)

                echo Fail lookup for $isin in feed $feed with filters: "$filters"
        fi
done < $KO

while read line
do
        if [ $(grep "$line" $OK | wc -l) -eq 0 ]
        then
                echo Cannot find venue for $line
        fi
done < $NOVENUE

为了生成输出文件,我重定向stdout到一个文件lookup.sh > file.txt

但我需要它自动创建输出文件。

答案1

在文件中,将整个脚本封装在

{


} > file.txt

它将重定向{}.

答案2

您始终可以通过例如将内容填充到文件中echo Something > the-file

相关内容