【问题标题】:noob - Syntax error: redirection unexpected [duplicate]noob - 语法错误:重定向意外[重复]
【发布时间】: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 谢谢,成功了。

标签: linux bash


【解决方案1】:

Bash: Syntax error: redirection unexpected

^类似,可能会回答您的问题。从该线程中接受的答案:

"Ubuntu 中的默认系统 shell 是 dash,而不是 bash,因此如果您有 #!/bin/sh,那么您的脚本将使用与您预期不同的 shell。Dash 没有

【讨论】:

  • 我不相信那是一样的。这是关于不同 Linux 风格不支持的特定运算符
  • // ,我同意 Ted 的观点,尽管 solutions 可能是相同的(添加一个 shebang,菜鸟!)questions 可能不同.泰德,你的问题有什么不同?
猜你喜欢
  • 2013-12-09
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 2016-04-11
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
相关资源
最近更新 更多