Shell 将第一个命令的输出传递给管道中的下一个命令的脚本

Shell script to pass the output of first command to the next in pipe

我想从日志中 grep 异常或错误,但问题是确切的日志文件名未知。可以肯定的是,最新的文件是我的日志文件,我想执行此单个命令,因为我将使用该命令从单个源到多个服务器执行 ssh

喜欢

ssh user@server "ls -ltr console*.log | tail -1; egrep -i 'exception|error' <<output of first command (i.e) log file name>>"

这可以在单个命令中完成吗??

这对你有用吗:

egrep -i 'exception|error' < $(\ls -tr console*.log|tail -1)
  • 只获取日志名称不要在ls
  • 中使用-l

谢谢大家,终于搞定了

ssh user@server "ls -ltr console*.log | tail -1; awk '{print $9}' | xargs egrep -i 'exception|error' <<第一个命令的输出(即)日志文件姓名>>"