Bash 的源命令不适用于来自 Internet 的 curl'd 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10520605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:14:24  来源:igfitidea点击:

Bash's source command not working with a file curl'd from internet

bashshellurl

提问by Gurjeet Singh

I am trying to source a script file from the internet using curl, like this: source <( curl url ); echo done, and what I see is that 'done' is echoed beforethe curl even starts to download the file!

我正在尝试使用 curl 从 Internet 获取脚本文件,如下所示:source <( curl url ); echo done,我看到的是,在 curl 甚至开始下载文件之前,“完成”就被回显了!

Here's the actual command and the output:

这是实际的命令和输出:

-bash-3.2# source <( curl --insecure https://raw.github.com/gurjeet/pg_dev_env/master/.bashrc ) ; echo done
done
-bash-3.2# % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2833 100 2833 0 0 6746 0 --:--:-- --:--:-- --:--:-- 0

I am not too worried about 'done' being echoed before or after anything, I am particularly concerned why the source command wouldn't read and act on the script!

我不太担心在任何事情之前或之后回响“完成”,我特别担心为什么源命令不会读取脚本并对其执行操作!

This command works as expected on my LinuxMint's bash, but not on the CentOS server's bash!

这个命令在我的 LinuxMint 的 bash 上按预期工作,但在 CentOS 服务器的 bash 上不起作用!

回答by Paused until further notice.

At first, I failed to notice that you're using Bash 3.2. That version won't source from a process substitution, but later versions such as Bash 4 do.

起初,我没有注意到您使用的是 Bash 3.2。该版本不会来自进程替换,但更高版本(例如 Bash 4)会这样做。

You can save the file and do a normal source of it:

您可以保存文件并对其进行常规来源:

source /tmp/del

(to use the file from your comment)

(使用您评论中的文件)

Or, you can use /dev/stdinand a here-string and a quoted command substitution:

或者,您可以使用/dev/stdin一个 here-string 和一个带引号的命令替换:

source /dev/stdin <<< "$(curl --insecure https://raw.github.com/gurjeet/pg_dev_env/master/.bashrc)"; echo done

回答by Gurjeet Singh

Try this:

尝试这个:

exec 69<> >(:);
curl url 1>&69;
source /dev/fd/69;
exec 69>&-;

This should force yer shell to wait for all data from the pipe. If that doesn't work this one will:

这应该强制您的外壳等待来自管道的所有数据。如果这不起作用,这个将:

exec 69<> >(:);
{ curl url 1>&69 & } 2>/dev/null;
wait $!
source /dev/fd/69;
exec 69>&-;

回答by huon

Does the following work?

以下是否有效?

file=$(mktemp)
curl --insecure -o $file https://raw.github.com/gurjeet/pg_dev_env/master/.bashrc 
source $file
rm $file