运行 作为另一个用户使用此处文档在脚本中执行命令

Running commands in a script as another user using a here doc

我希望能够在脚本中间切换用户。这是一种尝试:

su - User << EOF

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null

EOF

我的目标是执行 EOF 分隔符之间的代码,就像我实际以用户身份登录一样。

中间一行应该是安装Homebrew。如果我以用户身份登录并 运行 自己的中间行,它安装正常。但是 运行 上面的完整脚本给我带来了问题:

-e:5: unknown regexp options - lcal
-e:6: unknown regexp options - lcal
-e:8: unknown regexp options - Cach
-e:9: syntax error, unexpected tLABEL
BREW_REPO = https://github.com/Homebrew/brew.freeze
                  ^
-e:9: unknown regexp options - gthb
-e:10: syntax error, unexpected tLABEL
CORE_TAP_REPO = https://github.com/Homebrew/homebrew-core.freeze
                      ^
-e:10: unknown regexp options - gthb
-e:32: syntax error, unexpected end-of-input, expecting keyword_end
-bash: line 34: end: command not found
-bash: line 36: def: command not found
-bash: line 37: escape: command not found
-bash: line 38: end: command not found
-bash: line 40: syntax error near unexpected token `('
-bash: line 40: `  def escape(n)'

我尝试了不同的命令,而不仅仅是 Homebrew 安装,但大多数时候都会出现问题。将我尝试执行的命令传递给 'su' 与实际上 运行 以该用户身份执行命令有什么区别?

发生的事情是,嵌入的 $(...) 命令在 执行之前 此处文档被传递给 su。也就是说,传递给 suactual 脚本更像这样:

/usr/bin/ruby -e "#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere you can just
# untar https://github.com/Homebrew/brew/tarball/master anywhere you like or
# change the value of HOMEBREW_PREFIX.
HOMEBREW_PREFIX = "/usr/local".freeze
HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
...

等等。换句话说,$(...) 的输出被插入到 here-document 中。

为避免这种情况,您需要转义 $:

su - User << EOF

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null

EOF

或者,您可以通过将起始 EOF 括在双引号内,告诉 shell 按字面意思处理整个此处文档而不进行任何插值:

su - User << "EOF"

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null

EOF