DEV Community

Cover image for Just enough bash to be dangerous
Hugo Di Francesco
Hugo Di Francesco

Posted on • Originally published at codewithhugo.com on

Just enough bash to be dangerous

A bash cheat sheet for developers who just want to get by.

Writing setup, CI and deployment flows means a bit of the old bash scripting.

Despite my deep interest in the intricacies of Bash (/sarcasm), I’ve kept hitting up Google and StackOverflow for solutions to the same couple of situations.

To avoid having to do this again myself and for your reading pleasure, here they are.

To be dangerous in terms of setup, CI and depoyment flows we will encounter the following:

This was sent out on the Code with Hugo newsletter last Monday.
Subscribe to get the latest posts right in your inbox (before anyone else).

Check if file exists

if [! -f ./pdfgen/pdfgen]; then
    echo "Building pdfgen binary"
    npm run --prefix pdfgen build:linux
else
    echo "Pdfgen binary already exists, skipping build"
fi
Enter fullscreen mode Exit fullscreen mode

Check if a (symbolic) link exists

if [! -L /usr/local/bin/heroku];
then
    wget https://cli-assets.heroku.com/branches/stable/heroku-linux-amd64.tar.gz
    sudo mkdir -p /usr/local/lib /usr/local/bin
    sudo tar -xvzf heroku-linux-amd64.tar.gz -C /usr/local/lib
    sudo ln -s /usr/local/lib/heroku/bin/heroku /usr/local/bin/heroku
fi
Enter fullscreen mode Exit fullscreen mode

Check if environment variable is set

# long
if [[ -z "${CIRCLE_BRANCH}"] ]; then
    npm run redis-cli flushall
fi

npm run sync

# one-liner
[-z "${CIRCLE_BRANCH}"] && npm run redis-cli flushall; npm run sync
Enter fullscreen mode Exit fullscreen mode

Switch over an environment variable

case $CIRCLE_BRANCH in
    "develop")
        export ENVIRONMENT="dev"
        export HEROKU_APP=dev-app
        ;;
    "staging")
        export ENVIRONMENT="staging"
        export HEROKU_APP=staging-app
        ;;
    "production")
        export ENVIRONMENT="production"
        export HEROKU_APP=production-app
        ;;
esac
Enter fullscreen mode Exit fullscreen mode

Prompt the user

read -p "Are you sure you want to merge 'develop' into 'staging'? (y/N)" -n 1 -r
echo # we like having a new line

if [[$REPLY =~ ^[Yy]$ ]]
then
  git merge develop --ff-only
  git push
fi
Enter fullscreen mode Exit fullscreen mode

A final bit of advice, if it’s more than a couple of lines, try to use something like JavaScript, or Python to write your script.

I’ve got some resources to do that in modern JavaScript/Node:

This was sent out on the Code with Hugo newsletter last Monday.
Subscribe to get the latest posts right in your inbox (before anyone else).

Bonus

Slap your .env files into your environment

We’ve got .env files laying around, Docker Compose deals with this for use usually but say we want to get something running outside of Docker (and without using something like dotenv).

Here’s the snippet for a *NIX shell:

export $(cat .env | xargs)
Enter fullscreen mode Exit fullscreen mode

Guilherme Cunha

Top comments (5)

Collapse
 
moopet profile image
Ben Sinclair

You should put spaces between the test brackets and the test conditions like [ -f foo.txt ] otherwise you'll get a command not found error.

The environment variable one is over-complicated - you don't need the else, and you don't need to use false to ensure that the last command is run if you use a semicolon:

# long
if [[ -z "${CIRCLE_BRANCH}"] ]; then
    npm run redis-cli flushall
fi

npm run sync


# one-liner
[-z "${CIRCLE_BRANCH}"] && npm run redis-cli flushall; npm run sync
Collapse
 
hugo__df profile image
Hugo Di Francesco • Edited

Thanks I've updated it.

Collapse
 
nebojsac profile image
Nick Cinger

Good stuff! I feel like I know how to get around Bash, but I keep learning new tricks ever day, and I learned a few here, thanks!

Also, seeing that you have to end case block with the reverse esac just makes me giggle!

Collapse
 
vlasales profile image
Vlastimil Pospichal

I don't like ask the user. The best answer is the one I will not ask.

Collapse
 
blvkoblsk profile image
BLVKOBLSK

BASH Docs are your best friend; especially peeping others legit scripts too of course.

Thanks for this insight too!