我希望脚本在1次迭代后结束。它似乎是循环进行,我不得不杀死它与CTR,不知道如何修复它。
[root@vPAS-Calipers lattice_private]# cat stat.sh
#!/bin/bash
DATE=`/bin/date +"%Y%m%d%H%M"`
#140 Caliper VM- Volte
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4001 " "show call-mode statistics"
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4002 " "show call-mode statistics"
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4003 " "show call-mode statistics"
sleep 2
./stat.sh > stat-tps_${DATE}.txt结果
stat-tps_202101271452.txt
stat-tps_202101271453.txt
stat-tps_202101271455.txt发布于 2021-01-27 19:09:56
这是因为脚本的最后一行:
./stat.sh > stat-tps_${DATE}.txt这将再次运行脚本,并将输出重定向到一个新文件。(当脚本再次运行时,它将再次运行自己,等等)。
我猜想您的目的是将./linux_machine_cli_firing.exp命令的输出重定向到日志文件中?如果是这样的话,您可能也希望记录stderr。您可以为每个命令分别重定向它们:
#!/bin/bash
date=$(/bin/date +"%Y%m%d%H%M")
#140 Caliper VM- Volte
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4001 " "show call-mode statistics" >"stat-tps_${date}.txt" 2>&1
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4002 " "show call-mode statistics" >>"stat-tps_${date}.txt" 2>&1
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4003 " "show call-mode statistics" >>"stat-tps_${date}.txt" 2>&1
sleep 2注意,第一个文件使用>创建一个新文件,其余的使用>>来追加文件,而不是覆盖它。2>&1将stderr发送到stdout要执行的相同位置,因此如果您不想捕获错误,请忽略它。
另外,我切换到了一个小写的date变量;有一堆具有特殊含义的全大写变量名,所以使用小写或混合大写名称来避免冲突是最安全的。我将文件名双引号,因为它包含一个变量引用(这里并不需要,但脚本卫生性很好)。最后,我从使用backticks切换到更干净的$(),以捕获date命令的输出。
另一个选项是在脚本运行时将所有脚本的输出重定向到日志文件。您可以在不使用exec命令再次运行脚本的情况下这样做。请注意,在运行要重定向其输出的命令之前,必须这样做:
#!/bin/bash
date=$(/bin/date +"%Y%m%d%H%M")
exec >"stat-tps_${date}.txt" 2>&1 # Redirect output from this point on
#140 Caliper VM- Volte
sleep 2
./linux_machine_cli_firing.exp 172.9.9.14 "cd /root/cal/Product/run" "./cli -p 4001 " "show call-mode statistics"
sleep 2
...同样,如果不希望捕获错误以及常规输出,请省略2>&1。
https://unix.stackexchange.com/questions/631311
复制相似问题