Skip to content

Instantly share code, notes, and snippets.

@matthewcosgrove
Last active November 18, 2020 12:00
Show Gist options
  • Select an option

  • Save matthewcosgrove/2f94c1fa9b7937f215846d1f27f3ffbc to your computer and use it in GitHub Desktop.

Select an option

Save matthewcosgrove/2f94c1fa9b7937f215846d1f27f3ffbc to your computer and use it in GitHub Desktop.
Bash snippets
Selection of bash snippets
: "${MY_MANDATORY_ENV_VAR:? MY_MANDATORY_ENV_VAR must be set }"
# To use on args $1 etc
# https://stackoverflow.com/a/13864829
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
#!/bin/bash
for folder in $(ls);do
find $folder/.git -type d &> /dev/null
if [[ $? = 0 ]];then
pushd $folder
git status
git remote -v
popd >> /dev/null
fi
done
for folder in $(ls);do
find $folder/.git -type d &> /dev/null
if [[ $? = 0 ]];then
pushd $folder
gitb pull --rebase
git submodule update
git status
git remote -v
git push reference master
popd
fi
done
# https://linuxize.com/post/bash-heredoc/
# With substitution - ie. no "" around EOF delimiter
cat << EOF
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
# Without subsitution i.e. using "" around EOF delimiter
cat <<- "EOF"
The current working directory is: $PWD
You are logged in as: $(whoami)
EOF
#!/bin/bash
# Source: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
# Full directory name of the script no matter where it is being called from
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo $SCRIPT_DIR
# If you are using scripts in a sub-folder such as bin, get the root of the repo
REPO_ROOT_DIR="$(dirname "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )")"
echo $REPO_ROOT_DIR
pushd "${PACKER_FILE_INPUT_ALIAS_DIR}" > /dev/null
commit_sha=$(git rev-parse --short=8 HEAD) # --short=8 to match gitlab convention
echo "Commit SHA = ${commit_sha}"
rem=$(git remote get-url ${KICKSTART_FILE_GIT_REMOTE_NAME})
nodotgit=${rem%.git}
nogitat=${nodotgit#*@}
git_repo_path=${nogitat/://}
echo "Git Repo Path = ${git_repo_path}"
popd > /dev/null
export VM_HOSTNAME="${VM_HOSTNAME}"-"${commit_sha}"
echo "Ensuring uniqueness of template name with commit sha suffix. VM_HOSTNAME="${VM_HOSTNAME}""
if [[ $(govc vm.info "${VM_HOSTNAME}") ]]; then
echo "VM ${VM_HOSTNAME} already exists. Skipping.."
exit 0
else
echo "no VM ${VM_HOSTNAME} found. Proceeding.."
fi
export KICKSTART_FILE_URL="https:/${git_repo_path}/raw/${KICKSTART_FILE_GIT_REMOTE_BRANCH}/${KICKSTART_FILE_RELATIVE_FILEPATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment