通过管道将文件传输到 sh 和调用 shell 文件之间的区别
这就是我们想要做的:
$ wget -qO- www.example.com/script.sh | sh
它悄悄地下载脚本并将其打印到 stdout,然后通过管道传输到 sh。不幸的是,这并不完全有效,无法在各个点等待用户输入,以及一些语法错误。
这实际上是有效的:
$ wget -qOscript www.example.com/script.sh && chmod +x ./script && ./script
但是有什么区别呢?
我想也许管道文件不会执行该文件,而是单独执行每一行,但我对这种事情很陌生,所以我不知道。
This is what was trying to do:
$ wget -qO- www.example.com/script.sh | sh
which quietly downloads the script and prints it to stdout which is then piped to sh. This unfortunately doesn't quite work, failing to wait for user input at various points, aswell as a few syntax errors.
This is what actually works:
$ wget -qOscript www.example.com/script.sh && chmod +x ./script && ./script
But what's the difference?
I'm thinking maybe piping the file doesn't execute the file, but rather executes each line individually, but I'm new to this kind of thing so I don't know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您通过管道连接到
sh
时,该 shell/脚本的标准输入将是管道。因此,该脚本无法从控制台获取用户输入等。当您正常运行脚本时,stdin 是控制台 - 您可以在其中输入输入。When you pipe to
sh
, stdin of that shell/script will be the pipe. Thus the script cannot take e.g. user input from the console. When you run the script normally, stdin is the console - where you can enter input.您可以尝试告诉 shell 进行交互:
You might try telling the shell to be interactive:
我遇到了同样的问题,经过修补和谷歌搜索后,这对我有用。
I had the same issue, and after tinkering and googling this is what worked for me.