0

我的情况: 我用 Conda 安装了 bash_kernel 并在 Jupyter Notebook 环境中使用它。我从 Gitbash 运行它。单元魔法“%%bash”有效,执行脚本有效,执行 bash 函数有效。

现在我尝试使用存储在 bash 文件中的参数的函数来执行脚本。运行它会给我“CalledProcessError”。它必须与论点有关吗?我尝试使用 ./ 等来调用带有和不带引号的参数。没有任何效果。

如何解决错误?

如果我在这样的单元格中执行代码,则该代码有效。

```%%bash
find(){
cat $1 | grep -e $2 -e $3
}
find "poetry/poe_raven.txt" "dreary" "weary"``

它给出的结果是:沉闷,而虚弱和疲倦,

这没关系。现在我将它写入一个 Bash 文件。或者,我使用了带有 EOL 和 Line Splitting 的 Notepad ++。

```
%%bash
echo "find(){" > find.bash
echo "cat \$1 | grep -e \$2 -e \$3" >> find.bash
echo "}" >> find.bash
echo "find" >> find.bash```

```

%%bash
cat find.bash
```

```
find(){
cat $1 | grep -e $2 -e $3
}
find```

现在我正在尝试使用参数进行函数调用:

```
%%bash
./find.bash "poetry/poe_raven.txt" "dreary" "weary"
```

这给出了 CalledProcessError:


CalledProcessError Traceback (最近一次调用最后一次) C:\Users\GAMARA~1\AppData\Local\Temp/ipykernel_22152/3531725088.py in ----> 1 get_ipython().run_cell_magic('bash', '', '. /find.bash "诗歌/poe_raven.txt" "沉闷" "疲倦"\n')

...

CalledProcessError: Command 'b'./find.bash "poetry/poe_raven.txt" "dreary" "weary"\n'' 返回非零退出状态 1。

4

1 回答 1

0

我的猜测是它"poetry/poe_raven.txt"被解释为字符串,因此转换为poetry/poe_raven.txt没有"".

你可以试试:./find.bash '"poetry/poe_raven.txt"' '"dreary"' '"weary"'

或更短的使用 f-string:./find.bash f'"poetry/poe_raven.txt" "dreary" "weary"'

于 2021-07-16T14:10:17.260 回答