2017-04-25 52 views
3

我有這個小代碼來做一些數據處理。如何將變量重定向到文件但不能在屏幕上顯示

#!/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 
符合 echo "$f" >> file

我試圖寫入文件中的變量,但它也顯示在屏幕上的價值。我怎樣才能抑制控制檯上的價值,只是寫入文件

回答

5

這不是因爲echo被寫入stdout,但find寫入它的輸出,只需將其打壓至/dev/null

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

這樣,您只需在if-clause中使用find的返回碼,並且該命令的輸出不會打印到stdout,而是會打印到NULL設備。


但通常依靠find輸出是不是讓你的代碼工作的好辦法。

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 
+0

能否請您解釋一下你正在做這行'什麼而IFS =讀-r -d $「\ 0」 line' –

+0

@occasionalvisitor:這是因爲在'-print0'選項'找到'輸出,它將搜索結果附加'\ 0'字符,並且'read'命令對同一個字符進行取消限制以讀取單個文件。在http://man7.org/linux/man-pages/man1/find.1.html – Inian

+0

中查看更多關於'-print0'的信息'done >> file'會更有效一些。 – codeforester