Skip to content

Instantly share code, notes, and snippets.

@saxenap
Last active August 29, 2015 14:15
Show Gist options
  • Save saxenap/073049d1ead105417837 to your computer and use it in GitHub Desktop.
Save saxenap/073049d1ead105417837 to your computer and use it in GitHub Desktop.
Some bash helper functions for help with server initialization.
#!/bin/bash
#
# Copyright (c) 2014-2017 Praveen Saxena <>
# License: BSD-3-Clause
#
# To get:
# 1. rm -rf helper_functions_for_bash
# 2. wget -O helper_functions_for_bash https://gist.githubusercontent.com/saxenap/073049d1ead105417837/raw
BASENAME=${0##*/}
BASEDIR=$( cd $(dirname $0); pwd -P )
main()
{
# set -o nounset
# set -o errexit
}
if [ "${1}" != "--source-only" ]; then
main "${@}"
fi
newRandomString()
{
echo $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
}
filenameFrom()
{
echo $(basename ${1})
}
removeFile()
{
rm -rf ${1}
}
downloadFileFrom()
{
local url=${1}
local filename=${2}
wget -O ${filename} ${url}
}
executeFile()
{
local filename=${1}
chmod 777 $filename
./$filename
}
cleanlyExecuteFileFromUrl()
{
local url=${1}
local filename=$(filenameFrom ${url})
removeFile ${filename}
downloadFileFrom ${url} ${filename}
executeFile ${filename}
removeFile ${filename}
}
fileExists()
{
local filename=${1}
echo [ -f "${filename}" ]
}
makeNewDirectory ()
{
mkdir -p "$*"
}
searchEverywhereFor ()
{
ls | grep "$*"
}
replaceInFilesString()
{
local search=${1}
local replace=${2}
local files=${@:3}
for file in ${files[@]}; do
if [[ -e "${file}" ]]; then
if ( grep --fixed-strings --quiet "${search}" "${file}" ); then
perl -pi -e "s/\Q${search}/${replace}/g" "${file}"
fi
fi
done
}
commandExists()
{
type "$1" &> /dev/null ;
}
isset()
{
local varname=${1}
[ ! -z ${varname} ]
}
evaluateValueStoredIn()
{
local variable=${1}
eval variable=\$$variable
echo "${variable}"
}
eachFrom()
{
array=("${!1}")
for index in ${array[@]}
do
echo ${index}
done
}
_update()
{
yum -y update
}
_install()
{
yum -y install ${1}
}
setTimezone()
{
local _timezone="${1}"
ln -sf /usr/share/zoneinfo/"${_timezone}" /etc/localtime
}
echoWithColor()
{
for word in "$@"; do
case "$word" in
NORMAL|RESET) echo -n -e "\033[0;00m";;
BLACK) echo -n -e "\033[22;30m";;
RED|--ERROR|--ERR) echo -n -e "\033[22;31m";;
GREEN|--INFO) echo -n -e "\033[22;32m";;
YELLOW|--WARNING|--WARN) echo -n -e "\033[22;33m";;
BLUE) echo -n -e "\033[22;34m";;
MAGENTA) echo -n -e "\033[22;35m";;
CYAN|--DEBUG) echo -n -e "\033[22;36m";;
GRAY|WHITE) echo -n -e "\033[22;37m";;
LGRAY|DARKGRAY|BRIGHTBLACK) echo -n -e "\033[01;30m";;
LRED|LIGHTRED|BRIGHTRED|--FATAL) echo -n -e "\033[01;31m";;
LGREEN|LIGHTGREEN|BRIGHTGREEN) echo -n -e "\033[01;32m";;
LYELLOW|LIGHTYELLOW|BRIGHTYELLOW) echo -n -e "\033[01;33m";;
LBLUE|LIGHTBLUE|BRIGHTBLUE) echo -n -e "\033[01;34m";;
LMAGENTA|LIGHTMAGENTA|BRIGHTMAGENTA) echo -n -e "\033[01;35m";;
LCYAN|LIGHTCYAN|BRIGHTCYAN) echo -n -e "\033[01;36m";;
LWHITE|LIGHTWHITE|BRIGHTWHITE) echo -n -e "\033[01;37m";;
*) echo -n "$word ";;
esac
done
echo -e "\033[0;00m"
}
_echo()
{
echoWithColor "$@"
}
logThat()
{
_echo "$@"
return 0
}
dump()
{
_echo --DEBUG "$@"
return 0
}
recordInfo()
{
_echo --INFO "$@"
return 0
}
recordWarning()
{
_echo --WARN "$@"
}
recordError()
{
_echo --ERROR "$@"
}
die()
{
recordError >&2 -e "$@"
exit 1
}
assertCommandIsAvailable()
{
local cmd=${1}
type ${cmd} >/dev/null 2>&1 || die "Cancelling because required command '${cmd}' is not available."
}
assertFileExists()
{
local file=${1}
if [[ ! -f "${file}" ]]; then
die "Cancelling because required file '${file}' does not exist."
fi
}
assertFileDoesNotExist()
{
local file=${1}
if [[ -e "${file}" ]]; then
die "Cancelling because file '${file}' exists."
fi
}
assert ()
{
E_PARAM_ERR=98
E_ASSERT_FAILED=99
if [ -z "$2" ]
then
return $E_PARAM_ERR
fi
lineno=$2
if [ ! $1 ]
then
echo "Assertion failed: \"$1\""
echo "File \"$0\", line $lineno"
exit $E_ASSERT_FAILED
fi
}
# Taken from: https://github.com/bergerx/do-not-reinvent/blob/master/bash-script-helper
printStack()
{
local i=0
local FRAMES=${#BASH_LINENO[@]}
# FRAMES-2 skips main, the last one in arrays
for ((i=FRAMES-2; i>=1; i--)); do
recordInfo " File \"${BASH_SOURCE[i+1]}\", line ${BASH_LINENO[i]}, in ${FUNCNAME[i+1]}"
# Grab the source code of the line
local LINE=$(printLine "${BASH_SOURCE[i+1]}" "${BASH_LINENO[i]}")
recordInfo " $LINE"
done
}
# Taken from: https://github.com/bergerx/do-not-reinvent/blob/master/bash-script-helper
printLine()
{
local FILE=$1
local LINE=$2
local i=0
while read fileline; do
let i=i+1
if [ "$i" -eq "$LINE" ]; then
echo $fileline
break
fi
done <$FILE
}
# Taken from: https://github.com/bergerx/do-not-reinvent/blob/master/bash-script-helper
UPPERS=ABCDEFGHIJKLMNOPQRSTUVWXYZ
LOWERS=abcdefghijklmnopqrstuvwxyz
# Taken from: https://github.com/bergerx/do-not-reinvent/blob/master/bash-script-helper
# Usage: lowerCase "SOME STRING" -> "some string"
lowerCase()
{
local i=0
local OUTPUT=
while ([ $i -lt ${#1} ]); do
CUR=${1:$i:1}
case $UPPERS in
*$CUR*)
CUR=${UPPERS%$CUR*}
OUTPUT="${OUTPUT}${LOWERS:${#CUR}:1}"
;;
*)
OUTPUT="${OUTPUT}$CUR"
;;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
# Taken from: https://github.com/bergerx/do-not-reinvent/blob/master/bash-script-helper
# Usage: upperCase "some string" -> "SOME STRING"
upperCase()
{
local i=0
local OUTPUT=
while ([ $i -lt ${#1} ]); do
CUR=${1:$i:1}
case $LOWERS in
*$CUR*)
CUR=${LOWERS%$CUR*}
OUTPUT="${OUTPUT}${UPPERS:${#CUR}:1}"
;;
*)
OUTPUT="${OUTPUT}$CUR"
;;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
@saxenap
Copy link
Author

saxenap commented Feb 18, 2015

#!/bin/bash

phpPackages=(
    php55
    php55-devel
    php55-mysqlnd
    php55-pdo
    php55-mcrypt
    php55-mbstring 
    pcre-devel
    php55-pecl-apcu
    php55-opcache
)

_install "$(eachFrom phpPackages[@])"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment