简体   繁体   中英

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:

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:

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:

(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

You can try the following: Store the result of the curl command in a temporary file and execute this file. Like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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