【问题标题】:Adding a minute to time extracted from a command's output添加从命令输出中提取的时间
【发布时间】:2017-05-31 13:16:21
【问题描述】:

我正在编写一个脚本来检查我家中服务器的运行状况。

smartctl -t short /dev/sda | awk '/Please wait/ {print $3}'

此命令显示 SmartCTL 完成测试需要多长时间。 但为了确定,我想添加一个命令 sleep,该命令的输出加上 1 分钟,以确保它真的完成了。

有人可以帮我吗?

【问题讨论】:

  • 请显示输出的样子和你想要的样子。

标签: bash shell awk


【解决方案1】:

您可以捕获命令管道的输出并将其用作sleep 的参数:

sleep_time=$(smartctl -t short /dev/sda | awk '/Please wait/ {print $3}')
# add logic to check if sleep_time is a number etc.
if [[ $sleep_time ]]; then
  sleep $((sleep_time + 60)) # assuming smartctl prints the wait value in seconds
else
  # there was no sleep time extracted from smartctl - add appropriate logic to handle that situation
fi

【讨论】:

  • 嗯,我没想到在sleep命令里加分钟,我是想把它加到SmartCTL命令输出的变量里!非常感谢,这将帮助我!
【解决方案2】:
wait=`(smartctl -t short /dev/sda | awk '/Please wait/ {print $3}'` # minutes
sleep $(($wait + 1))m 

【讨论】:

  • 谢谢,没想到在sleep命令里多加一分钟,我试试看!
【解决方案3】:
sleep $((60+60*$(($(smartctl -t short /dev/sda|awk '/Please wait/ {print $3}')))))

似乎正是你要找的东西。

  • sleep 默认为 arg,以秒为单位。
  • 如果子shell 由于某种原因没有输出整数,则将$(smartctl ...) 包含在额外的$((...)) 中会计算为零,并防止最外层的计算因语法错误而失败。

【讨论】:

  • 谢谢,没想到在sleep命令里多加一分钟,我试试看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
相关资源
最近更新 更多