【发布时间】:2019-12-22 21:47:26
【问题描述】:
这里是新手 shell 脚本。
我在 bash shell 中。以我自己的身份运行以下脚本有效,但在我使用 sudo 时无效。正如您可能知道的那样,touch 错误只是为了确保将某些内容写入日志文件。
dave@pi1:~ $ ./test.sh
touch: cannot touch '/asdf/asdf/asdf': No such file or directory <--- As expected
dave@pi1:~ $ sudo ./test.sh
./test.sh: 2: ./test.sh: Syntax error: redirection unexpected <--- why this not work?
dave@pi1:~ $
脚本...
LOG_FILE=/var/log/usbhook
exec > >(tee -a ${LOG_FILE} )
exec 2> >(tee -a ${LOG_FILE} >&2)
touch /asdf/asdf/asdf
目的是在我插入 USB 记忆棒时触发一个脚本,所以我假设它在使用 sudo 运行时应该可以工作。
编辑
这与评论中链接的其他问题不同。这与不能在 shell 中使用的运算符有关。
【问题讨论】:
-
将
#!/usr/bin/env bash添加为脚本的第一行 -
// , 你有什么具体的问题要问,或者更多的是“这是我期望它做的,你能帮助调试我的代码吗?”
-
// ,另外,欢迎来到奇怪而美妙的 shell 脚本世界。这里是龙。
-
@anubhava 谢谢,成功了。