linux - 从 URL 执行 bash 脚本

假设我在 URL http://mywebsite.example/myscript.txt 有一个文件,其中包含一个脚本:

#!/bin/bash
echo "Hello, world!"
read -p "What is your name? " name
echo "Hello, ${name}!"

我想在不先将其保存到文件的情况下运行此脚本。我该怎么做?

现在,我已经看到了语法:

bash < <(curl -s http://mywebsite.example/myscript.txt)

但这似乎不像我保存到文件然后执行那样工作。例如 readline 不起作用,输出只是:

$ bash < <(curl -s http://mywebsite.example/myscript.txt)
Hello, world!

同样,我试过了:

curl -s http://mywebsite.example/myscript.txt | bash -s --

结果相同。

最初我有一个类似的解决方案:

timestamp=`date +%Y%m%d%H%M%S`
curl -s http://mywebsite.example/myscript.txt -o /tmp/.myscript.${timestamp}.tmp
bash /tmp/.myscript.${timestamp}.tmp
rm -f /tmp/.myscript.${timestamp}.tmp

但这似乎很草率,我想要一个更优雅的解决方案。

我知道有关从 URL 运行 shell 脚本的安全问题,但现在让我们忽略所有这些。

最佳答案

source <(curl -s http://mywebsite.example/myscript.txt)

应该这样做。或者,不要对您的初始重定向进行重定向,即重定向标准输入; bash需要一个文件名,无需重定向即可正常执行,<(command)语法提供了路径。

bash <(curl -s http://mywebsite.example/myscript.txt)

如果你看一下 echo <(cat /dev/null) 的输出可能会更清楚。

https://stackoverflow.com/questions/5735666/

相关文章:

python - Django Admin - 更改标题 'Django administratio

python - 为什么使用 Python 的 os 模块方法而不是直接执行 shell 命令?

linux - 如何在 Bash 中给定超时后杀死子进程?

linux - 好的 Linux (Ubuntu) SVN 客户端

python - 为什么在 Pylint 认为不正确的条件值中使用 len(SEQUENCE)?

python - 为什么我不能在 python 中创建一个轮子?

python - 如何将 Pandas 数据添加到现有的 csv 文件中?

linux - 在远程 Linux 机器上编译 C++ - "clock skew detected

python - 在 Python 中获取大文件的 MD5 哈希

python - 您需要安装 postgresql-server-dev-X.Y 来构建服务器端扩展