我有一個類似的 bash 腳本
#!/bin/bash
DATABASE_NAME=my_database
export DATABASE_NAME
run_some_other_command
他們首先宣告變數并將其設定為一個值,然后在單獨的行中匯出它。就個人而言,我喜歡在一行中完成,例如:
export DATABASE_NAME=my_database
是否有一些風格規則反對這個?我見過申報和出口被別人分開,但不知道為什么。
如果有幫助,這個腳本可以在 Linux Mint 上運行,但也可以在其他 Linux 甚至 Mac 上運行。
uj5u.com熱心網友回復:
因為export是命令;當分配來自命令或子 shell 輸出時,它自己的回傳碼將覆寫/屏蔽分配命令的回傳碼:
# the return-code of the getDBName function call
# is masked by the export command/statement
export DATABASE_NAME=$(getDBName)
# Separate export
export DATABASE_NAME
# Allow capture and use of the getDBName return code
if ! DATABASE_NAME=$(getDBName)
then printf 'Failed getDBName with RC=%d\n' "$?" >&2
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/404405.html
標籤:
上一篇:使用bash腳本中的正則運算式查找字串是否以特定模式開頭
下一篇:從讀取命令獲取值并分配給變數
