BASH CheatSheet

This is the world’s lamest explanation of what BASH is, however it is a helpful resource for a newbie trying to do a few tricks with BASH. I’ll be the newbie because it is fitting and this is a cheat sheet that I created (at least in part) for myself to refer to.

Base64 Encode from file



sudo cat /etc/letsencrypt/live/mydomain.com/fullchain.pem | base64

BASH: Get contents of the pem file and pipe it to the base64 command. The Base64 encoded contents are output to the stdout.


Write output to stdout



echo "Setting variables."

Comments



# To write comments start your line with '#'

# This is a comment
echo "This is not a comment because it is code."

Set Variables



# Here we will set some variables
NAMESPACE="default"                    
APP_NAME="mysite"                       
DOMAIN="mysite.com"                     
PRIMARY_URL="https:\/\/${DOMAIN}"       


# Interpolate a variable in a string
echo "Namespace = ${NAMESPACE}"

If Statement



# Here is an example If statement. It checks to see if a folder exists and exits if it does.

TEMPFOLDER="temp"
if [ -d "$TEMPFOLDER" ]; then
  # Take action if $DIR exists. #
  echo ""
  echo "ABORTING because /${TEMPFOLDER} exists. Either delete the /${TEMPFOLDER} folder or change the config.sh file."
  echo ""
  exit 1
fi

How to create a folder:



# Make a new folder named 'temp' 

TEMPFOLDER="temp"
mkdir $TEMPFOLDER

### Replace a string in a file with a variable value
# this script 
# will find a string 'CONFIG_NAMESPACE' and replace it with the value NAMESPACE which is 'default', 
# in every .yaml file that exists in a folder named 'temp' 

NAMESPACE="default" 
TEMPFOLDER="temp"

find $TEMPFOLDER -name '*.yaml' -exec sed -i '.bak' "s/CONFIG_NAMESPACE/${NAMESPACE}/g" {} +

How to Delete a folder:



# this will delete a folder named 'temp' and all its contents.

TEMPFOLDER="temp"
rm -r $TEMPFOLDER


Posted

in

by

Tags:

Comments

Leave a comment