简体   繁体   English

bash:当$ 0是进程重定向的结果时,执行

[英]bash: Executing $0 when it is the result of a process redirection

I have a script on GitHub that is normally executed as the result of a process redirection, like so: 在GitHub上有一个脚本该脚本通常由于流程重定向而执行,如下所示:

sh <(curl -sSL http://git.io/sinister)

The script itself may require root privileges if it's not being run from Windows, so I have added the following lines of code to re-execute itself as root if needed: 如果脚本不是从Windows运行的,则脚本本身可能需要root特权,因此,我添加了以下代码行,以便在需要时以root身份重新执行自身:

on_windows()
{
    uname | grep -q '[CYGWIN|MINGW|MSYS]'
}

if ! on_windows && [ "$USER" != 'root' ]; then
    exec sudo "$0"
fi

However, this does not seem to be working smoothly with the process redirection. 但是,这似乎在进程重定向方面无法正常工作。 Running sh -x results in this: 运行sh -x导致以下结果:

(trusty)james@localhost:~$ sh -x <(curl -sSL http://git.io/sinister)
+ set -e
+ ...
+ on_windows
+ grep -q [CYGWIN|MINGW|MSYS]
+ uname
+ [ james != root ]
+ exec sudo /dev/fd/63
sudo: /dev/fd/63: command not found

Why is this happening and how can I fix this? 为什么会发生这种情况,我该如何解决?

You may want to check this answer: https://unix.stackexchange.com/a/156088 您可能需要检查以下答案: https : //unix.stackexchange.com/a/156088

You can try the following: Store the result of the curl command in a temporary file and execute this file. 您可以尝试以下操作:将curl命令的结果存储在一个临时文件中并执行该文件。 Like so: 像这样:

curl -sSL http://git.io/sinister > file.temp
chmod u+x file.temp
./file.temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM