命名这个有点棘手......
基本上我有一个程序,它在 STDOUT 上运行时会打印一组 shell 变量:
$ ./settings
SETTING_ONE="this is setting one"
SETTING_TWO="This is the second setting"
ANOTHER_SETTING="This is another setting".
Run Code Online (Sandbox Code Playgroud)
我想从 shell 脚本中运行它,就好像 STDOUT 是用source.
我想做一些像......
source `./settings`
Run Code Online (Sandbox Code Playgroud)
......但这当然行不通。
我知道我可以这样做:
./settings >/tmp/file
source /tmp/file
Run Code Online (Sandbox Code Playgroud)
但我真的不想那样做。
有什么线索吗?
use*_*686 20
在/dev/fd可用的系统上,bash 支持进程替换:
source <(./settings)
Run Code Online (Sandbox Code Playgroud)
在这里,<( )将扩展到一个自动分配的路径,在/dev/fd/...该路径下./settings可以读取的输出。
Kei*_*ith 17
您可以使用eval:
eval "$(./settings)"
eval "`./settings`"
Run Code Online (Sandbox Code Playgroud)
declare `./settings`
Run Code Online (Sandbox Code Playgroud)
或者当然...
export `./settings`
Run Code Online (Sandbox Code Playgroud)
当然是测试...
export `echo -e "asdf=test\nqwerty=dvorak"` ; echo $asdf $qwerty
Run Code Online (Sandbox Code Playgroud)
处理空格:
eval export `./settings`
Run Code Online (Sandbox Code Playgroud)
source /dev/stdin < ./settings
我认为 /dev/stdin 是 Linux 独有的东西。