2013-02-12 55 views
1

我有一個腳本,在運行命令xyz後將stdout重定向到日誌文件。我試着在控制檯這樣做:Bash腳本不迎合新行,將文本附加到當前行

xyz > temp.log & 

,當我看着temp.log其完美的,因爲我希望它是。但是,當我在腳本中運行上述命令時,temp.log只是所有stdout中的一大部分。我如何使它從控制檯運行它一樣打印?我已經在下面顯示了日誌。

運行xyz > temp.log &從控制檯

/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 18 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 6 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 3 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 2 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 19 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 11 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 1 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 12 
/source_id:int64/(Green-aeon-GlobalMillennium-Spout): 3 

從腳本中運行

ESC[?1049hESC[1;48rESC(BESC[mESC[4lESC[?7hESC[HESC[2JEvery 5.0s: Green-aeon-GlobalMillennium-SpoutESC[1;178HTue Feb 12 11:36:08 2013ESC[3;1H/source_id:36/sch_event:1797777 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC[4d/source_id:36/sch_event:1797779 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC[5d/source_id:36/sch_event:1797781 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC[6d/source_id:36/sch_event:1797783 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC[7d/source_id:36/sch_event:1797785 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC[8d/source_id:36/sch_event:1797787 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST` 2013^MESC[9d/source_id:36/sch_event:1797789 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC[10d/source_id:36/sch_event:1797791 for Green-aeon-GlobalMillennium-Spout at Tue Feb 12 11:16:41 CST 2013^MESC 

這是腳本

#!/bin/sh 

die() { 
    echo >&2 "[email protected]" 
    exit 1 
} 

[ "$#" -ge 4 ] || die "usage notification_stats <table_name> <listener_name> <time_interval> <time_to_run>" 

echo $3 | grep -E -q '^[0-9]+$' || die "Numeric argument required, $3 provided for time_interval" 
echo $4 | grep -E -q '^[0-9]+$' || die "Numeric argument required, $4 provided for time_to_run" 

watch -n $3 xyz -v $1 $2 > notify.log & 
$my_pid = $! 

sleep $4 
kill -9 $my_pid 
+0

你能張貼腳本? – EJK 2013-02-12 18:01:31

+0

FWIW,這些看起來像ANSI轉義序列。挑戰在於搞清楚輸出它們的是什麼。 – EJK 2013-02-12 18:05:40

+0

@EJK添加腳本 – noMAD 2013-02-12 18:06:44

回答

3

請勿使用watch。它意味着交互式顯示,而不是重複運行一個程序。

您可以使用

#!/bin/bash 
while ((SECONDS <= $4)) 
do 
    xyz -v $1 $2 
    sleep $3 
done > notify.log 

代替

+0

什麼是SECONDS?這是當前時間的秒數嗎? – noMAD 2013-02-12 18:37:28

+1

每次引用此參數時,返回自從shell調用返回後的秒數。 (來自「男人bash」) – 2013-02-12 18:38:52