1

交互式程序通常可以从标准输入读取输入,例如,

$ 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从标准输入读取,比如bashor dc

4

1 回答 1

2

不像它的名字所暗示的那样,nix-shell它不是典型的 UNIX 意义上的 shell;只是在更广泛的意义上,它是一个用于启动程序的程序。

这些-p参数仅用于将软件带入 shell 环境。您可以使用多个,但它们并没有实际运行这些程序的效果*。

nix-shell启动 bash,不管nix-shell自己的逻辑如何,例如使用:

$ echo hello | nix-shell -p hello --run bash
Hello, world!
于 2020-07-03T20:44:47.087 回答