簡體   English   中英

bash是grep還是grep elif grep? 如此迷茫

[英]Bash if grep or grep elif grep? So confused

這是我想要做的。 我想根據我發現的內容grep(也許?)一個文件作為關鍵字。

if grep "env=" does not exist OR "env=" exists but is null file.name; then
env=prod 
elif grep "env=dev" OR "env=qa" file.name; then
env=dev/qa respectivly

我正在嘗試一些grep命令,但是被卡住了

不確定是否可以得到,仍然需要測試

if ! grep -q -E "^environment=" "${__file}" | grep -q -e "dev|qa|prod"; then
    if [ -n "${ENV}" ]; then
      echo "environment=${ENV}" >> "${__file}"
    else
      echo "environment=prod" >> "${__file}"
    fi
  fi

使用命令。 將所有局部變量放在名為local_env.sh文件中。

local_env.sh樣本內容:

environment=dev

現在您可以創建一個腳本:

source env.sh
# if environment is empty string set it to prod
[ -z "${environment}" ] && environment=prod
echo ${environment}

暫無
暫無

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

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