Last active
September 1, 2020 10:26
-
-
Save jtviegas/91e96ab3cdaac33ae1838f56be6ac5d6 to your computer and use it in GitHub Desktop.
basic bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
this_folder="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | |
if [ -z "$this_folder" ]; then | |
this_folder=$(dirname $(readlink -f $0)) | |
fi | |
parent_folder=$(dirname $this_folder) | |
# include file | |
if [ ! -f "$this_folder/variables.inc" ]; then | |
warn "we DON'T have a 'variables.inc' file" | |
else | |
. "$this_folder/variables.inc" | |
fi | |
if [ ! -f "$this_folder/secrets.inc" ]; then | |
warn "we DON'T have a 'secrets.inc' file" | |
else | |
. "$this_folder/secrets.inc" | |
fi | |
if [ -f "$this_folder/commons.inc" ]; then | |
. "$this_folder/commons.inc" | |
else | |
warn "we DON'T have a 'commons.inc' file" | |
fi | |
# parameter check | |
usage() | |
{ | |
cat <<EOM | |
usage: | |
$(basename $0) <parameter> | |
EOM | |
exit 1 | |
} | |
[ -z $1 ] && { usage; } | |
# file existence check | |
if [ ! -e $file ] | |
then | |
echo "!!! no file [$file] !!! ...leaving." | |
return -1 | |
fi | |
# outcome check | |
if [ ! "$?" -eq "0" ] ; then echo "not ok"; return -1; fi | |
# --- lib include file example --- | |
# usage: | |
# . $this_folder/lib_include.sh | |
# | |
# table=test | |
# result=$(createTable "$table") | |
# echo $result | |
createTable() | |
{ | |
local __r=0 | |
local __table=$1 | |
aws dynamodb describe-table --output text --table-name $__table | |
if [ "$?" -eq "0" ] | |
then | |
echo "------- ...table $__table is still there, try to delete it first! ... leaving!" | |
__r=1 | |
else | |
aws dynamodb create-table --table-name $__table --attribute-definitions AttributeName=id,AttributeType=S --key-schema AttributeName=id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 | |
if [ ! "$?" -eq "0" ] | |
then | |
echo "------- ! could not create table $__table !...leaving." | |
__r=1 | |
else | |
echo "------- created table $__table" | |
fi | |
fi | |
echo "$__r" | |
} | |
# --- functions outcome example --- | |
apiStart() | |
{ | |
return 1 | |
} | |
apiStart | |
if [ ! "$?" -eq "0" ] ; then | |
echo "bad" && exit 1 | |
else | |
echo "ok" | |
fi | |
# --- tests --- | |
__s="UNKNOWN" | |
if [ "$__s" -ne "" ]; then echo "ok"; fi ### bash: [: UNKNOWN: integer expression expected | |
if [ "$__s" != "" ]; then echo "ok"; fi ### ok | |
if [ ! -z "$__s" ]; then echo "ok"; fi ### ok | |
if [ "$__s" == "UNKNOWN" ]; then echo "ok"; fi ### ok | |
if [ "$__s" -eq "UNKNOWN" ]; then echo "ok"; fi ### bash: [: UNKNOWN: integer expression expected | |
__s="FF" | |
if [ "$__s" -ne "" ]; then echo "ok"; fi ### bash: [: FF: integer expression expected | |
if [ "$__s" != "" ]; then echo "ok"; fi ### ok | |
if [ ! -z "$__s" ]; then echo "ok"; fi ### ok | |
if [ "$__s" != "UNKNOWN" ]; then echo "ok"; fi ### ok | |
if [ "$__s" -ne "UNKNOWN" ]; then echo "ok"; fi ### bash: [: FF: integer expression expected | |
__s= | |
if [ "$__s" -eq "" ]; then echo "ok"; fi ### bash: [: : integer expression expected | |
if [ "$__s" != "" ]; then echo "ok"; fi ### | |
if [ "$__s" == "" ]; then echo "ok"; fi ### ok | |
if [ ! -z "$__s" ]; then echo "ok"; fi ### | |
if [ -z "$__s" ]; then echo "ok"; fi ### ok | |
if [ "$__s" != "UNKNOWN" ]; then echo "ok"; fi ### ok | |
if [ "$__s" -ne "UNKNOWN" ]; then echo "ok"; fi ### bash: [: : integer expression expected | |
# --- param case control structure --- | |
# ... | |
undeployApi() | |
{ | |
echo "[undeployApi|in]" | |
echo "[undeployApi|out]" | |
} | |
usage() | |
{ | |
cat <<EOM | |
usage: | |
$(basename $0) [state|loader|api] [deploy|undeploy] | |
EOM | |
exit 1 | |
} | |
case "$1" in | |
state) | |
case "$2" in | |
deploy) | |
deployBase | |
;; | |
undeploy) | |
undeployBase | |
;; | |
*) | |
usage | |
esac | |
;; | |
api) | |
case "$2" in | |
deploy) | |
deployApi | |
;; | |
undeploy) | |
undeployApi | |
;; | |
*) | |
usage | |
esac | |
;; | |
*) | |
usage | |
esac | |
.... | |
install-chart) | |
[ "$2" != "admin" ] && [ "$2" != "exch-rate" ] && [ "$2" != "gcss-feed" ] && [ "$2" != "pricing" ] && [ "$2" != "pricing-branch1" ] && [ "$2" != "public" ] && [ "$2" != "quote" ] && [ "$2" != "scheduler" ] && { usage; } | |
service=$2 | |
# assume default values | |
tag=latest | |
release=$service | |
shift && shift | |
while [[ "$#" -gt 0 ]]; do | |
echo "1: $1" | |
case $1 in | |
-i|--image-tag) | |
[ -z "$2" ] && { usage; } | |
tag="$2"; | |
shift | |
;; | |
-n|--name) | |
[ -z "$2" ] && { usage; } | |
release="$2" | |
shift | |
;; | |
*) | |
usage | |
;; | |
esac | |
shift | |
done | |
installChart "$service" "$tag" "$release" | |
;; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment