验证BASH脚本时出现问题

时间:2019-03-03 14:51:12

标签: linux bash

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

#!/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语句似乎出错。

我正在等待参数“开发”或“部署”。

以下错误:

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.

2 个答案:

答案 0 :(得分:1)

您只需删除所有尾随&&。例如,从此:

printf 'Test Server Deploy Started!\n\n' &&
                                         ^^

对此:

printf 'Test Server Deploy Started!\n\n'

答案 1 :(得分:0)

上一行:

for i in indices {
    if (self[i] == value) {
       // do swap
       ...
    }
}

将始终为真。那应该是

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

@Andrea也是正确的,认为if [ "$1" != "dev" -a "$1" != "production" ]; then 没有意义

使用case语句可能会更好:

&&