【问题标题】:Bash if grep or grep elif grep? So confusedBash if grep 或 grep elif grep?如此迷茫
【发布时间】:2018-01-25 07:05:07
【问题描述】:

这就是我想要做的。我想根据我发现的内容来 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

【问题讨论】:

    标签: bash if-statement grep


    【解决方案1】:

    使用source 命令。将所有局部变量放入名为 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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 2023-03-30
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多