簡體   English   中英

使用存儲在 bash 文件腳本中的參數調用 bash 函數會在 Jupyter 筆記本中產生 CalledProcessError

[英]Calling a bash function with arguments stored in bash file script gives CalledProcessError in a Jupyter notebook

我的情況:我用 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 文件。 或者,我已經將 Notepad ++ 與 EOL 和 Line Splitting 一起使用。

```
%%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 "poetry/poe_raven.txt" "沉悶" "疲倦"\\n')

...

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

我的猜測是"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"'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM