交互式程序通常可以从标准输入读取输入,例如,
$ echo echo hello | bash
hello
或者
$ echo 1 2 + p | dc
3
但是,nix-shell
似乎没有这样的行为,例如
$ echo hello | nix-shell -p hello
$
而预期的输出是Hello, world!
.
使用建议的技巧nix-shell(1)
:
--command cmd
In the environment of the derivation, run the shell command cmd. This command is executed in an
interactive shell. (Use --run to use a non-interactive shell instead.) However, a call to exit is
implicitly added to the command, so the shell will exit after running the command. To prevent
this, add return at the end; e.g. --command "echo Hello; return" will print Hello and then drop
you into the interactive shell. This can be useful for doing any additional initialisation.
导致错误:
$ echo hello | nix-shell -p hello --command return
/tmp/nix-shell-15399-0/rc: line 1: return: can only `return' from a function or sourced script
$
我的相关程序版本如下:
$ nix --version
nix (Nix) 2.3.2
$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-linux-gnu)
$
因此我的问题是:我如何nix-shell
从标准输入读取,比如bash
or dc
?