1

我有一个脚本,在运行命令后将标准输出重定向到日志文件xyz。我尝试在控制台中这样做:

xyz > temp.log &

当我看到temp.log它像我期望的那样完美时。但是当我在脚本中运行上述命令时,temp.log 只是所有标准输出的一大行。如何使其打印与从控制台运行它相同?我在下面显示了日志。

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 "$@"
    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
4

1 回答 1

3

不要使用watch. 它用于交互式显示,而不是重复运行程序。

您可以使用

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

反而

于 2013-02-12T18:13:57.397 回答