【问题标题】:sudo heredoc: bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF...EOF')sudo heredoc:bash:警告:第 0 行的此处文档由文件结尾分隔(需要 `EOF...EOF')
【发布时间】:2019-08-13 22:08:47
【问题描述】:

我的目标是使用sudo 将内容写入另一个用户的xstartup 文件。

但我的尝试...

sudo -iu user1 bash -lic 'cat > ~/.vnc/xstartup <<\EOF
...
EOF'

...导致...

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF...EOF')

然而嵌套的 heredocs 工作...

sudo -iu user1 bash -s <<\OUTER
cat > ~/.vnc/xstartup <<\EOF
...
EOF
OUTER

有什么建议可以在没有嵌套 heredocs 的情况下完成 sudo 重定向?

【问题讨论】:

  • 在这里使用-i 参数到sudo 是告诉sudo 启动一个附加 shell,超出你使用显式@ 调用的那个shell 987654329@ 稍后。

标签: bash sudo heredoc


【解决方案1】:

你有很多不必要的层。

sudo -u user1 tee ~user1/.vnc/xstartup > /dev/null <<\EOF
...
EOF

【讨论】:

  • 这绝对更干净,但在我的系统(Ubuntu 18.04)上,它会导致tee: /home/sudouser/.vnc/xstartup: Permission denied。另外,我希望~ 解析为/home/user1 而不是/home/sudouser
  • @mellow-yellow, ~user1 确实解析为/home/user1,即使由sudouser评估。您确定您正在完全按照 chepner 提供的方式运行它(目标用户名紧跟在 ~ 之后)?
  • 对不起,我这边的错字。我不知道除了当前的$(id -u) 之外,您还可以为用户添加~ 前缀路径。效果很好!谢谢!
猜你喜欢
  • 2015-01-27
  • 2012-09-12
  • 2013-08-08
  • 2020-12-24
  • 2012-05-28
  • 1970-01-01
  • 2018-05-09
  • 2016-04-04
  • 2011-05-20
相关资源
最近更新 更多