如果Shell脚本中的错误不起作用,是否退出?

时间:2019-02-15 13:13:10

标签: bash shell compiler-errors yii2

它会编译文件并忽略所有错误。现在我要在这里的是它必须退出并在存在错误的情况下停止编译

注意:我正在使用docker容器。此代码在.yml文件之后执行。如果有错误,则yml退出编译器,但在此文件中它将忽略所有错误

这是我的shell-scripts-dev.sh文件

#!/bin/bash
#do not enter current dir
#cd $(dirname $0)
BASEDIR=$(dirname "$0")
printf "\n"
printf "###############################################################################\n"
printf "# Running dev script from directory  #\n"
printf "###############################################################################\n"
pwd

printf "\n"
printf "###############################################################################\n"
printf "# Create initial/ needed files  #\n"
printf "###############################################################################\n"
# backend init files
if [ ! -f backend/config/main-local.php ]; then
    printf "File backend/config/main-local.php does not exist, creating it, please edit configuration on server!\n"
    cp backend/config/main-local.example backend/config/main-local.php
fi

if [ ! -f backend/config/params.php ]; then
    printf "File backend/config/params.php does not exist, creating it, please edit configuration on server!\n"
    cp backend/config/params.example backend/config/params.php
fi
if [ ! -f backend/web/index.php ]; then
    printf "File backend/web/index.php does not exist, creating it, please edit configuration on server!\n"
    cp backend/web/index.example backend/web/index.php
fi
if [ ! -f backend/web/index-test.php ]; then
    printf "File backend/web/index-test.php does not exist, creating it, please edit configuration on server!\n"
    cp backend/web/index.example backend/web/index-test.php
fi
# common init files
if [ ! -f common/config/main.php ]; then
    printf "File common/config/main.php does not exist, creating it, please edit configuration on server!\n"
    cp common/config/main.example common/config/main.php
fi

if [ ! -f common/config/main-local.php ]; then
    printf "File common/config/main-local.php does not exist, creating it, please edit configuration on server!\n"
    cp common/config/main-local.example common/config/main-local.php
fi

if [ ! -f common/config/params.php ]; then
    printf "File common/config/params.php does not exist, creating it, please edit configuration on server!\n"
    cp common/config/params.example common/config/params.php
fi


# rest init files
if [ ! -f rest/config/params.php ]; then
    printf "File rest/config/params.php does not exist, creating it, please edit configuration on server!\n"
    cp rest/config/params.example rest/config/params.php
fi
if [ ! -f rest/web/index.php ]; then
    printf "File rest/web/index.php does not exist, creating it, please edit configuration on server!\n"
    cp rest/web/index.example rest/web/index.php
fi
if [ ! -f rest/web/index-test.php ]; then
    printf "File rest/web/index-test.php does not exist, creating it, please edit configuration on server!\n"
    cp rest/web/index.example rest/web/index-test.php
fi
printf "\n"
printf "###############################################################################\n"
printf "# Clearing cache #\n"
printf "###############################################################################\n"
# backend runtime files
rm -rf backend/web/assets/*
rm -rf backend/runtime/debug/*
rm -rf backend/runtime/logs/*
rm -rf backend/runtime/mail/*
rm -rf backend/runtime/URI/*
rm -rf cache/*
# rest runtime files
rm -rf rest/runtime/cache/*
rm -rf backend/runtime/debug/*
rm -rf backend/runtime/logs/*
rm -rf backend/runtime/mail/*


###############################################################################
# Rebuild assets# - don't we need this???
###############################################################################
#chmod +x ../app/Console/cake
#cd ../app && Vendor/bin/cake asset_compress build --force

printf "\n"
printf "###############################################################################\n"
printf "# Composer update #\n"
printf "###############################################################################\n"
php composer.phar install

printf "\n"
printf "###############################################################################\n"
printf "# Running yii migrations #\n"
printf "###############################################################################\n"
yes | php yii migrate

printf "\n"
printf "###############################################################################\n"
printf "#Run PhpSniffer and output any errors #\n"
printf "###############################################################################\n"
php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php backend --colors
php ./vendor/bin/phpcs --encoding=utf-8 --extensions=php common --colors
php

 ./vendor/bin/phpcs --encoding=utf-8 --extensions=php rest --colors

1 个答案:

答案 0 :(得分:1)

万一我理解正确,万一遇到错误,您需要退出bash脚本?如果是这样,您可以使用set -e命令:What does set -e mean in a bash script?