验证BASH脚本时出现问题 [英] Issue while validating BASH Script

查看:461
本文介绍了验证BASH脚本时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试验证bash脚本时遇到代码掉毛错误:

I have code lint errors while trying to validate my bash script:

#!/bin/bash

if [ $# -eq 0 ]; then
    printf 'No arguments supplied. Available arguments: dev, production\n'

    exit 128
fi

if [ "$1" -ne "dev" ] || [ "$1" -ne "production" ]; then
    printf 'Unsupported arguments supplied. Supported arguments: dev, production\n'

    exit 128
fi

if [ "$1" -eq "dev" ]; then
    printf 'Test Server Deploy Started!\n\n' &&
elif [ "$1" -eq "production" ]; then
    printf 'Production Server Deploy Started!\n\n' &&
fi

printf "=====> [1/7] - Pull Submodules <=====\n\n" &&
git pull --recurse-submodules &&

printf "\n\n=====> [2/7] - Update Submodules <=====\n\n" &&
git submodule update --init --recursive --force &&

printf "\n\n=====> [3/7] - Stop All Servers <=====\n\n" &&
pm2 stop all &&

printf "\n\n=====> [4/7] - Install Frontend Node Modules <=====\n\n" &&
cd ./frontend &&

if [ "$1" -eq "dev" ]; then
    npm ci &&
elif [ "$1" -eq "production" ]; then
    npm ci --only=production &&
fi

printf "\n\n=====> [5/7] - Build Frontend <=====\n\n" &&
npm run build &&

printf "\n\n=====> [6/7] - Install Backend Node Modules <=====\n\n" &&
cd ../backend &&

if [ "$1" -eq "dev" ]; then
    npm ci &&
elif [ "$1" -eq "production" ]; then
    npm ci --only=production &&
fi

printf "\n\n=====> [7/7] - Start All Servers <=====\n\n" &&
cd .. &&

if [ "$1" -eq "dev" ]; then
    pm2 restart ./build/ecosystem.config.js --env dev --update-env &&
elif [ "$1" -eq "production" ]; then
    pm2 restart ./build/ecosystem-prod.config.js --env production --update-env &&
fi

printf "\n\nDone.\n"
exit 0;

我使用 https://www.shellcheck.net/进行检查,但似乎出错if语句.

I used https://www.shellcheck.net/ to check, but it seems error with if statement.

我正在等待参数开发"或部署".

I'm waiting for param "dev" or "deployment".

以下错误:

Line 15:
if [ "$1" -eq "dev" ]; then
^-- SC1009: The mentioned syntax error was in this if expression.
                       ^-- SC1073: Couldn't parse this then clause. Fix to allow more checks.

Line 17:
elif [ "$1" -eq "production" ]; then
     ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

推荐答案

上一行:

if [ "$1" -ne "dev" ] || [ "$1" -ne "production" ]; then

将始终为真.可能应该是

will always be true. That should probably be

if [ "$1" != "dev" -a "$1" != "production" ]; then

@Andrea也是正确的,认为&&毫无意义

@Andrea is also correct that the &&'s don't make sense

使用case语句可能会更好:

You would probably be better off using a case statement:

case "$1" in
  dev)
    echo "found dev"
    ;;
  production)
    echo "found production"
    ;;
  *)
    echo "not dev or prod, or not present"
    ;;
esac 

这篇关于验证BASH脚本时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆