Long ago, when I had the chance to build by myself a set of scripts that performed certain operations I ended up with a nice set of small things.
I would like to share, but it needs some explanation.
This library, group of tools, they do nothing by themselves, their only serve a single purpose which is “To standardize” .
At that time I was a “one man army”, I built stuff by myself, for myself and to myself, but then I realized that after me would come someone else, with different styles, different habits and so on,..
Since I needed to change as less code possible this library can be sourced and its functions called many times, they become read only , or they are atomic, so nothing has be worried about using its functions several times in a row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
#!/bin/bash # Marcs library #TODO # there are two lower case converters # funcion to download all the scripts at once # funcion to clean up all the scripts # set variables declare -r TRUE=0 declare -r FALSE=1 declare -r PASSWD_FILE=/etc/passwd declare -r RED='\033[0;41;30m' declare -r STD='\033[0;0;39m' declare -r CYAN='\e[1;37;44m' declare -r TMPDIR='/tmp/' ################################################################## # Purpose: Converts a string to lower case # Arguments: # $1 -> String to convert to lower case ################################################################## function to_lower() { local str="$@" local output output=$(tr '[A-Z]' '[a-z]'<<<"${str}") echo $output } ################################################################## # Purpose: Display an error message and die # Arguments: # $1 -> Message # $2 -> Exit status (optional) ################################################################## function die() { local m="$1" # message local e=${2-1} # default exit status 1 echo "$m" exit $e } ################################################################## # Purpose: Return true if script is executed by the root user # Arguments: none # Return: True or False ################################################################## function is_root() { [ $(id -u) -eq 0 ] && return $TRUE || return $FALSE } ################################################################## # Purpose: Return true $user exits in /etc/passwd # Arguments: $1 (username) -> Username to check in /etc/passwd # Return: True or False ################################################################## function is_user_exits() { local u="$1" grep -q "^${u}" $PASSWD_FILE && return $TRUE || return $FALSE } ################################################################## # Purpose: Inform the user that this functions is not implemented # Arguments: No arguments # Return: No returns ################################################################## function notImplemented() { echo "This functions is not implemented yet." echo "Press any Key to continue or press Ctrl-C." read x } ################################################################## # Purpose: Converts a string to lower case # Arguments: # $1 -> String to convert to lower case ################################################################## lowercase(){ echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" } ################################################################## # Purpose: Recover important information # Arguments: If $1 == "debug" then output for each var will be printed # Return: TRUE is first time execution. FALSE if already executed # Read Only variables set: # OS # DIST # DistroBasedOn # PSUEDONAME # REV # KERNEL # MACH ################################################################## shootProfile(){ # This funcion creates readonly variables, if one of them has been set before it will crash # check that all variables are unset. [ -n "${OS+x}" ] && [ -n "${DIST+x}" ] && [ -n "${DistroBasedOn+x}" ] && [ -n "${PSUEDONAME+x}" ] && [ -n "${REV+x}" ] && [ -n "${KERNEL+x}" ] && [ -n "${MACH+x}" ] && echo " !!! shootProfile was called before. The variables are already available" && return $FALSE OS=`lowercase `uname`` KERNEL=`uname -r` MACH=`uname -m` if [ "{$OS}" == "windowsnt" ]; then OS=windows elif [ "{$OS}" == "darwin" ]; then OS=mac else OS=`uname` if [ "${OS}" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="${OS} ${REV}(${ARCH} `uname -v`)" elif [ "${OS}" = "AIX" ] ; then OSSTR="${OS} `oslevel` (`oslevel -r`)" elif [ "${OS}" = "Linux" ] ; then if [ -f /etc/redhat-release ] ; then DistroBasedOn='RedHat' DIST=`cat /etc/redhat-release |sed s/\ release.*//` PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//` REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//` elif [ -f /etc/SuSE-release ] ; then DistroBasedOn='SuSe' PSUEDONAME=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//` REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //` elif [ -f /etc/mandrake-release ] ; then DistroBasedOn='Mandrake' PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//` REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//` elif [ -f /etc/debian_version ] ; then DistroBasedOn='Debian' DIST=`cat /etc/lsb-release | grep '^DISTRIB_ID' | awk -F= '{ print $2 }'` PSUEDONAME=`cat /etc/lsb-release | grep '^DISTRIB_CODENAME' | awk -F= '{ print $2 }'` REV=`cat /etc/lsb-release | grep '^DISTRIB_RELEASE' | awk -F= '{ print $2 }'` fi if [ -f /etc/UnitedLinux-release ] ; then DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi OS=`lowercase $OS` DistroBasedOn=`lowercase $DistroBasedOn` readonly OS readonly DIST readonly DistroBasedOn readonly PSUEDONAME readonly REV readonly KERNEL readonly MACH fi fi local debug=${1:-nodebug} if [[ $debug = "debug" ]]; then echo $OS echo $DIST echo $DistroBasedOn echo $PSUEDONAME echo $REV echo $KERNEL echo $MACH fi return $TRUE } ################################################################## # Purpose: Make sure package on $1 is installed # Arguments: # $@ -> Packages wich should be installed ################################################################## installPkg() { [ -n "${DistroBasedOn-x}" ] && shootProfile for i in $@ do echo -n "Installing $i on $DistroBasedOn." case $DistroBasedOn in Debian | debian ) apt-get -y install $i ;; SuSe | suse ) zypper install $i ;; Mandrake | mandrake ) echo "do not know how to install $i package on mandrake " ;; RedHat | redhat ) yum -y install $i ;; *) echo "\$DistroBasedOn variable has not a valid value. Value : ||$DistroBasedOn||" ;; esac done return 0 } ################################################################## # Purpose: Compare hostname # Arguments: # $1 => string # Return : TRUE if $1 = `hostname` false otherwhise. ################################################################## hostnameGet(){ [[ "`hostname`" = "$1" ]] && return $TRUE || return $FALSE } ################################################################## # Purpose: Return TRUE if $1 is confirmed, or keypresed # Arguments: # $1 => string # Return : TRUE if $1 user presses the key. ################################################################## confirm(){ read -sn 1 -p "$1 [y/N]? " [[ $REPLY = [Yy] ]] && return $TRUE|| return $FALSE } #=== FUNCTION ================================================================ # NAME: downloadAndExec # DESCRIPTION: downloads and launches a script # PARAMETERS: $1 = script name # RETURNS: TRUE if OK #=============================================================================== downloadAndExec () { scriptname=$1 workdir="/tmp" workscript=$workdir/$scriptname [ -f $workscript ] && rm $workscript wget $MASTERURL/bin/$scriptname -O $workscript chmod +x $workscript $workscript [[ $? -eq 0 ]] && rm $workscript && return $TRUE || return $FALSE } # ---------- end of function downloadAndExec ---------- return 0 |
One Reply to “The scripts that become functions, and the functions that belong to a library .”