Commit 95c8007e authored by Peter Dave Hello's avatar Peter Dave Hello

Execute `exit 1` when shell script `cd` fails

It's dangerous when `cd` fails but following commands keeps running
which would lead to expected results.

For those shell scripts without `set -e`, use `exit 1` before furthur
refactor could be a safe and efficient workaround, for thoes shell
scripts with `-e`, the shell(script) will exit immediately if any
command has a non-zero exit status, so they don't need `exit 1`

Reference:
- https://github.com/koalaman/shellcheck/wiki/SC2164
parent 441ea69a
#!/usr/bin/env bash #!/usr/bin/env bash
cd $(dirname $0)/.. cd $(dirname $0)/.. || exit 1
if [ -n "$SIDEKIQ_WORKERS" ] ; then if [ -n "$SIDEKIQ_WORKERS" ] ; then
exec bin/background_jobs_sk_cluster "$@" exec bin/background_jobs_sk_cluster "$@"
......
#!/bin/sh #!/bin/sh
cd $(dirname $0)/.. cd $(dirname $0)/.. || exit 1
app_root=$(pwd) app_root=$(pwd)
mail_room_pidfile="$app_root/tmp/pids/mail_room.pid" mail_room_pidfile="$app_root/tmp/pids/mail_room.pid"
......
#!/bin/sh #!/bin/sh
cd $(dirname $0)/.. cd $(dirname $0)/.. || exit 1
app_root=$(pwd) app_root=$(pwd)
unicorn_pidfile="$app_root/tmp/pids/unicorn.pid" unicorn_pidfile="$app_root/tmp/pids/unicorn.pid"
......
---
title: Execute `exit 1` when shell script `cd` fails
merge_request: 46122
author: Peter Dave Hello
type: other
#!/usr/bin/env bash #!/usr/bin/env bash
cd "$(dirname "$0")/.." cd "$(dirname "$0")/.." || exit 1
echo "=> Linting documents at path $(pwd) as $(whoami)..." echo "=> Linting documents at path $(pwd) as $(whoami)..."
echo echo
ERRORCODE=0 ERRORCODE=0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment