Bash script using default value for empty parameter

Problem

You have a bash script that accepts parameter(s) but you want to give a default value for a missing parameter.

Solution

For example you have a script that accepts a parameter with the year and month (ie 201703), but you want to give it a default of two months ago if the parameter is missing.

#!/bin/bash

LM=$(date -d '2 month ago' +%Y%m)
YEARMONTH=${1:-${LM}}
echo $YEARMONTH

Bash script to add environment and date in Symfony appversion.yml file

Problem

You are using CI/CD to deploy a Symfony application that has the appversion.yml configuration file, and you want to add the environment and a timestamp to it after deployment.

Solution

You can achieve this by creating and running a bash script like the following and passing the environment as a variable (ie script_name env) :

#!/bin/bash
# Script for adding the environment and time in deployment
# Needs the environment as a parameter
# DATE_CREATED=2017.06.07
# DATE_UPDATED=2017.06.08
# VERSION=1.01

CONF_PATH=/var/www/analyse/app/config

sed '/app.version/s/\(.*$\)/\1'" (${1}) $(date +%Y%m%d-%H%M)"'/g' $CONF_PATH/appversion.yml > $CONF_PATH/appversion_new.yml
mv $CONF_PATH/appversion.yml $CONF_PATH/appversion_bak.yml
mv $CONF_PATH/appversion_new.yml $CONF_PATH/appversion.yml