Just enough bash to be dangerous
Tags: bash, linux, ci, productivity
Published: 2018-07-26 13:50:22

---
title: Just enough bash to be dangerous
published: true
tags: bash,linux,ci,productivity
canonical_url: https://codewithhugo.com/just-enough-bash-to-be-dangerous/
cover_image: https://codewithhugo.com/img/guilherme-cunha-unsplash.jpg
---

> 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:
- [Check if file exists](#check-if-file-exists)
- [Check if a (symbolic) link exists](#check-if-a-symbolic-link-exists)
- [Check if environment variable is set](#check-if-environment-variable-is-set)
- [Switch over an environment variable](#switch-over-an-environment-variable)
- [Prompt the user](#prompt-the-user)
- [Bonus](#bonus)
    - [Slap your .env files into your environment](#slap-your-env-files-into-your-environment)

> This was sent out on the [Code with Hugo newsletter](https://buttondown.email/hugo) last Monday.
> [Subscribe](https://buttondown.email/hugo) to get the latest posts right in your inbox (before anyone else).

## Check if file exists

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

## Check if a (symbolic) link exists

```sh
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
```

## Check if environment variable is set

```sh
# 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
```

## Switch over an environment variable

```sh
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
```

## Prompt the user

```sh
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
```

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:

- [ES6 by example: a module/CLI to wait for Postgres in docker-compose](https://dev.to/hugo__df/es6-by-example-a-modulecli-to-wait-for-postgres-in-docker-compose-902)
- [How to make beautiful, simple CLI apps with Node](https://dev.to/hugo__df/how-to-make-beautiful-simple-cli-apps-with-node-c1b)

> This was sent out on the [Code with Hugo newsletter](https://buttondown.email/hugo) last Monday.
> [Subscribe](https://buttondown.email/hugo) 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](https://github.com/motdotla/dotenv)).

Here’s the snippet for a \*NIX shell:

```sh
export $(cat .env | xargs)
```

[Guilherme Cunha](https://unsplash.com/@guiccunha?utm_medium=referral&utm_campaign=photographer-credit&utm_content=creditBadge)