将shell脚本的输出重定向到文本文件

时间:2021-12-16 19:31:02

I am trying to redirect the output of my script(scheduled using at command) to a text file.

我正在尝试将脚本的输出重定向到一个文本文件。

at -f shelltest.sh -v 11:33 > data.txt

Even the script is running successfully, the output is not getting saved to output text file(data.txt).

即使脚本运行成功,输出也不会保存到输出文本文件(data.txt)。

Say my script demands to check for the "adb devices" then perform reboot, when i schedule the script using at command, neither the command prompt not displayed or the output of a script is not getting saved in text file.

假设我的脚本要求检查“adb设备”,然后执行重新启动,当我使用at命令调度脚本时,命令提示符没有显示,或者脚本的输出没有保存到文本文件中。

How to save the output of a script in text file.

如何在文本文件中保存脚本的输出。

Note:
Text file is having read/write permission.

注意:文本文件具有读/写权限。

2 个解决方案

#1


2  

The shell is doing precisely what you are requesting: It is running the at command, and redirecting its output to the file. So while you may be hoping (or even expecting) that the output of the at job you scheduled would end up there, that is not at all what you are saying. You are saying, and getting, "please redirect the output of the scheduling operation to this file."

shell正在执行您所请求的操作:运行at命令,并将输出重定向到文件。因此,尽管你可能希望(甚至期望)你计划的at作业的输出最终会在那里结束,但这根本不是你想说的。您正在说,并获取,“请将调度操作的输出重定向到此文件。”

The workaround is kind of cumbersome, but given the above, should hopefully make sense.

解决方法有点麻烦,但鉴于上面的内容,希望大家能理解。

at -v 11:33 <<'HERE'
shelltest.sh >data.txt
HERE

If you have Bash, you can use a here string as well:

如果您有Bash,您也可以使用here字符串:

at -v 11:33 <<<'shelltest.sh >data.txt'

Or you can use echo or printf:

也可以使用echo或printf:

printf '%s\n' 'shelltest.sh >data.txt' | at -v 11:33

#2


1  

Try:

试一试:

echo 'shelltest.sh > data.txt' | at -v 11:33

Notes

Consider how the shell interprets this command:

考虑shell如何解释这个命令:

at -f shelltest.sh -v 11:33 > data.txt
  • The shell sees the command at with four arguments: -f, shelltest.sh, -v, and 11:33. The shell cares not what those arguments mean. The shell simply passes them on to at.

    shell看到命令at有四个参数:-f、shelltest。sh - v,十一33。shell并不关心这些参数的含义。shell简单地将它们传递给at。

  • The shell interprets > data.txt to mean that the output of at should be sent to data.txt. This redirection has not affect on what happens When shelltest.sh is finally run.

    shell解释>数据。txt表示应该将at的输出发送到data.txt。这种重定向不会影响shell测试时发生的情况。sh终于运行。

#1


2  

The shell is doing precisely what you are requesting: It is running the at command, and redirecting its output to the file. So while you may be hoping (or even expecting) that the output of the at job you scheduled would end up there, that is not at all what you are saying. You are saying, and getting, "please redirect the output of the scheduling operation to this file."

shell正在执行您所请求的操作:运行at命令,并将输出重定向到文件。因此,尽管你可能希望(甚至期望)你计划的at作业的输出最终会在那里结束,但这根本不是你想说的。您正在说,并获取,“请将调度操作的输出重定向到此文件。”

The workaround is kind of cumbersome, but given the above, should hopefully make sense.

解决方法有点麻烦,但鉴于上面的内容,希望大家能理解。

at -v 11:33 <<'HERE'
shelltest.sh >data.txt
HERE

If you have Bash, you can use a here string as well:

如果您有Bash,您也可以使用here字符串:

at -v 11:33 <<<'shelltest.sh >data.txt'

Or you can use echo or printf:

也可以使用echo或printf:

printf '%s\n' 'shelltest.sh >data.txt' | at -v 11:33

#2


1  

Try:

试一试:

echo 'shelltest.sh > data.txt' | at -v 11:33

Notes

Consider how the shell interprets this command:

考虑shell如何解释这个命令:

at -f shelltest.sh -v 11:33 > data.txt
  • The shell sees the command at with four arguments: -f, shelltest.sh, -v, and 11:33. The shell cares not what those arguments mean. The shell simply passes them on to at.

    shell看到命令at有四个参数:-f、shelltest。sh - v,十一33。shell并不关心这些参数的含义。shell简单地将它们传递给at。

  • The shell interprets > data.txt to mean that the output of at should be sent to data.txt. This redirection has not affect on what happens When shelltest.sh is finally run.

    shell解释>数据。txt表示应该将at的输出发送到data.txt。这种重定向不会影响shell测试时发生的情况。sh终于运行。