如何使用 bash 脚本将所有 jar 结果打印到文件中

How to print all jar results into a file using bash script

我一直在尝试执行我编写的这个简单脚本,它 运行 是我制作的可执行 jar 文件。脚本命令如下:

#!/bin/bash
msisdn=

java -cp /home/support/phuzca/Migration/PostpaidXMigration_lib/ -jar /home/support/phuzca/Migration/PostpaidXMigration.jar $msisdn /home/support/phuzca/Migration/config.properties /opt/tomcat9/webapps/axis2/WEB-INF/classes/META-INF/PlanID.xml

jar 文件按预期工作,我收到了预期的结果:

我一直想弄清楚的想法是如何防止这些文本在我 运行 我的脚本时出现,而是将它们打印在一个文件中以便以后查看。我希望你能为我打开一些想法。非常感谢。

将输出重定向到文件:

migrateToPstopaidX.sh > output.log

如果你想重定向 stderr 使用这个

  migrateToPstopaidX.sh &> output.log

您可以使用此 >> 代替 >

来附加日志

stdoutstderr 重定向到输出文件。

migrateToPstopaidX.sh > output.log 2>&1

您可以使用 >> 来追加而不是覆盖您的文件。

Bash 从左到右执行重定向,如下所示:

  • >: 在覆盖模式下打开 output.log 并将 stdout 重定向到那里。
  • 2>&1:将 stderr 重定向到 "where stdout is currently going"。在这种情况下,这是一个以追加模式打开的文件。换句话说,&1 重用了 stdout 当前使用的文件描述符。