├── BASHInitialisation.sh ├── LICENSE ├── README.md ├── SQLite └── create.sql ├── alias ├── bin ├── addSwapFile.sh ├── clojure ├── generalToHistory.sh ├── installGuestAdditions.sh ├── memAndSwap.sh ├── memAndSwapCombined.sh ├── memAndSwapProgram.sh ├── memAndSwapProgramCombined.sh ├── memUsage.sh ├── memUsageCombined.sh ├── memUsageProgram.sh ├── memUsageProgramCombined.sh ├── programsUsingMem.sh ├── programsUsingSwap.sh ├── resources.sh ├── simpleTest.sh ├── suspend.sh ├── swapUsage.sh ├── swapUsageCombined.sh ├── swapUsageProgram.sh ├── swapUsageProgramCombined.sh ├── thinLogging.sh └── updateVBoxExt.sh ├── disk.sh ├── install.sh ├── network.sh ├── random.sh ├── system.sh ├── systemd.sh └── time.sh /BASHInitialisation.sh: -------------------------------------------------------------------------------- 1 | # Functions: 2 | # - addToPath 3 | # - calc 4 | # - canRun 5 | # - chall 6 | # - cdll 7 | # - checkInteger 8 | # - checkNetworkInterface 9 | # - checkReadableDirectory 10 | # - checkReadOnlyVariables 11 | # - cleanPath 12 | # - commandExists 13 | # - convertInput 14 | # - defaultPS_OPTIONS 15 | # - elementInList 16 | # - fatal 17 | # - filterCommand 18 | # - getCPUTemperature 19 | # - getOptionValue 20 | # - getPathDirs 21 | # - isInteger 22 | # - isInteractive 23 | # - isVarSet 24 | # - loadXmodmapExtra 25 | # - logDRY 26 | # - logError 27 | # - logMsg 28 | # - nrOfDirs 29 | # - nrOfFiles 30 | # - nrOfFilesAndDirs 31 | # - psCommand 32 | # - psGrep 33 | # - psPid 34 | # - psPpid 35 | # - psStatus 36 | # - psUser 37 | # - removeFromPath 38 | # - screent 39 | # - showMessage 40 | # - stackTrace 41 | # - stripLeadingZeros 42 | # - taggedMsg 43 | # - taggedMsgAndStackTrace 44 | # - valueInArray 45 | # - variableExist 46 | # - warning 47 | 48 | # Variables: 49 | # - STACK_TRACE_DEPTH 50 | 51 | # Comments: 52 | # I prefer long parameters. If you can not use long parameters, you need to use 53 | # the following substitutions: 54 | # 55 | # grep: 56 | # - --extended-grep -E 57 | # - --max-count=1 -m 1 58 | # - --only-matching -o 59 | 60 | ################################################################################ 61 | # Functions # 62 | ################################################################################ 63 | 64 | # Usage: AddToPath [--start|--end] [--no-reorder] 65 | # Adds a directory to PATH 66 | # Default to the front, but can also be on the end 67 | # Default at the the old place the directory is deleted, 68 | # but with --no-reorder PATH will not be changed if it already contains the directory. 69 | # Needed: 70 | # - BASH functions 71 | # - fatal 72 | function addToPath { 73 | declare -r USAGE="USAGE: ${FUNCNAME} [--start|--end] [--no-reorder] " 74 | 75 | declare ALWAYS_SET=y 76 | declare START=y 77 | declare ADD_DIR 78 | 79 | declare dir 80 | declare path="" 81 | 82 | if [[ ${#} -ge 1 ]] ; then 83 | if [[ ${1} == --start ]] ; then 84 | START=y ; shift 85 | elif [[ ${1} == --end ]] ; then 86 | START=n ; shift 87 | fi 88 | fi 89 | readonly START 90 | if [[ ${#} -ge 1 && ${1} == --no-reorder ]] ; then 91 | ALWAYS_SET=n ; shift 92 | fi 93 | readonly ALWAYS_SET 94 | if [[ ${#} -ne 1 ]] ; then 95 | fatal "${USAGE}" 96 | return 97 | fi 98 | ADD_DIR="${1}" ; shift 99 | 100 | while read dir ; do 101 | if [[ ${dir} == ${ADD_DIR} ]] ; then 102 | if [[ ${ALWAYS_SET} == y ]] ; then 103 | continue 104 | fi 105 | return 106 | fi 107 | path+="${dir}:" 108 | done < <(getPathDirs) 109 | if [[ ${START} == y ]] ; then 110 | path="${ADD_DIR}:${path}" 111 | else 112 | path="${path}${ADD_DIR}:" 113 | fi 114 | PATH="${path:0:-1}" 115 | } 116 | 117 | # Usage: calc 118 | # Gives an expression to be evaluated to bc 119 | # Needed: 120 | # - BASH functions 121 | # - fatal 122 | function calc { 123 | if [[ ${#} -lt 1 ]] ; then 124 | fatal "${FUNCNAME} needs an expression" 125 | return 126 | fi 127 | 128 | printf "${@}\n" | bc 129 | } 130 | 131 | # Usage: canRun 132 | # Often you want to be sure something is only run one time at the time 133 | # This function tries to get a semaphore and report the status 134 | # Needed: 135 | # - BASH functions 136 | # - fatal 137 | function canRun { 138 | declare LOCK_FILE 139 | 140 | if [[ ${#} -ne 1 ]] ; then 141 | fatal "${FUNCNAME} " 142 | return 143 | fi 144 | LOCK_FILE=${1}; shift 145 | 146 | (set -o noclobber; :>${LOCK_FILE}) &>/dev/null || return 1 147 | return 0 148 | } 149 | 150 | # Usage: chall [-R] mode own:group 151 | # Example: chall -R a=rx root:sys /usr/data" 152 | # Based on a script of Everhard Faas 153 | # Combined chown en chmod, an idea of Rolf Sonneveld 154 | # Needed: 155 | # - BASH functions 156 | # - fatal 157 | function chall { 158 | declare mod 159 | declare opts='' 160 | declare own 161 | 162 | if [[ ${#} -ge 1 ]] && [[ ${1} = "-R" ]] ; then 163 | opts=${1}; shift 164 | fi 165 | 166 | if [ ${#} -lt 3 ] ; then 167 | fatal "${FUNCNAME}: Error, not enough arguments 168 | Usage: ${FUNCNAME} [-R] mode own:group 169 | Example: ${FUNCNAME} -R a=rx root:sys /usr/data" 170 | return 171 | fi 172 | 173 | mod=${1}; own=${2}; shift 2 174 | 175 | chmod ${opts} ${mod} ${*} 176 | chown ${opts} ${own} ${*} 177 | } 178 | 179 | # Usage: cdll 180 | # cd and ls -l combined 181 | # Needed: 182 | # - BASH functions 183 | # - fatal 184 | function cdll { 185 | if [[ ${#} -ne 1 ]] ; then 186 | fatal "${FUNCNAME} " 187 | return 188 | fi 189 | 190 | cd ${1} ; shift 191 | ls -l 192 | } 193 | 194 | # Usage: checkInteger 195 | # A fatal error when parameter is NOT an integer 196 | # Needed: 197 | # - BASH functions 198 | # - fatal 199 | # - isInteger 200 | function checkInteger { 201 | if [[ ${#} -ne 1 ]] ; then 202 | fatal "${FUNCNAME} " 203 | return 204 | fi 205 | if ! isInteger ${1} ; then 206 | fatal "'${1}' is not an integer" 207 | return 208 | fi 209 | } 210 | 211 | # Usage: checkNetworkInterface [] 212 | # Shows if errors, dropped or overruns of the specified interface 213 | # are unequal zero 214 | # When no interface is given, check all interfaces 215 | # Needed: 216 | # - BASH functions 217 | # - fatal 218 | function checkNetworkInterface { 219 | if [[ "$#" -gt "1" ]] ; then 220 | fatal "${FUNCNAME} []" 221 | return 222 | fi 223 | if [[ "$#" -eq "1" ]] ; then 224 | declare -r INTERFACE=${1}; shift 225 | else 226 | declare -r INTERFACE="" 227 | fi 228 | 229 | /sbin/ifconfig ${INTERFACE} | grep '\(errors\|dropped\|overruns\):[^0]' 230 | } 231 | 232 | # Usage: checkReadableDirectory [] 233 | # Checks if DIRECTORY is readable 234 | # First parameter is the calling function which is used in the error messages 235 | # If no directory is given the current directory is used 236 | # Needed: 237 | # - BASH functions 238 | # - fatal 239 | function checkReadableDirectory { 240 | if [[ $# -lt 1 ]] ; then 241 | fatal "${FUNCNAME} needs at least the name of the calling function" 242 | return 243 | fi 244 | declare -r CALLING_FN="${1}" ; shift 245 | declare -r USAGE="USAGE: ${CALLING_FN} [DIRECTORY]" 246 | 247 | declare dir 248 | 249 | if [[ ${#} -eq 0 ]] ; then 250 | dir=. 251 | elif [[ ${#} -eq 1 ]] ; then 252 | dir="${1}" 253 | else 254 | fatal "${USAGE}" 255 | return 256 | fi 257 | if [[ ! -d ${dir} ]] ; then 258 | # It is not a directory or not reachable (/root/bin) 259 | fatal "${CALLING_FN}: ${dir} is not a (reachable) directory" 260 | return 261 | fi 262 | if [[ ! -r ${dir} ]] ; then 263 | fatal "${CALLING_FN}: ${dir} is not readable" 264 | return 265 | fi 266 | echo ${dir} 267 | } 268 | 269 | # Usage: checkReadOnlyVariables 270 | # Checks if at least one of the given variables is read only 271 | # Needed: 272 | # - BASH functions 273 | # - fatal 274 | function checkReadOnlyVariables { 275 | if [[ "$#" -ne "1" ]] ; then 276 | fatal "${FUNCNAME} " 277 | return 278 | fi 279 | declare -r VARS_TO_CHECK=${1}; shift 280 | 281 | readonly | awk -v variableNames=${VARS_TO_CHECK} ' 282 | BEGIN { 283 | returnCode = 0; 284 | split(variableNames, variableArray, "#"); 285 | } 286 | 287 | END { 288 | exit returnCode 289 | } 290 | 291 | { 292 | for(i in variableArray) { 293 | variableName = variableArray[i]; 294 | temp = "^" variableName "="; 295 | if( match($3, temp ) ) { 296 | print "ERROR: " variableName " defined as read only" 297 | returnCode = 1; 298 | } 299 | } 300 | } 301 | ' 302 | } 303 | 304 | # Usage: cleanPath 305 | # Makes sure that directories are not more as once in PATH 306 | # Needed: 307 | # - Bash 4 because it uses associative arrays 308 | # - BASH functions 309 | # - fatal 310 | function cleanPath { 311 | declare -r USAGE="USAGE: ${FUNCNAME}" 312 | 313 | declare dir 314 | declare -A found 315 | declare path 316 | 317 | if [[ ${#} -ne 0 ]] ; then 318 | fatal "${USAGE}" 319 | return 320 | fi 321 | 322 | while read dir ; do 323 | if [[ ${found[$dir]} ]] ; then 324 | continue 325 | fi 326 | path+="${dir}:" 327 | found["${dir}"]=1 328 | done < <(getPathDirs) 329 | PATH="${path:0:-1}" 330 | } 331 | 332 | # Usage: commandExists: commandExists 333 | # Checks if a command exist 334 | # Needed: 335 | # - BASH functions 336 | # - fatal 337 | function commandExists { 338 | if [[ ${#} -ne 1 ]] ; then 339 | fatal "${FUNCNAME} " 340 | return 341 | fi 342 | declare -r COMMAND_TO_CHECK=${1}; shift 343 | 344 | type "${COMMAND_TO_CHECK}" &> /dev/null 345 | } 346 | 347 | # Usage: convertInput 348 | # Sometimes you have to compare an input to certain values. 349 | # But for example you want YES, Yes and yes have the same meaning. 350 | # This function removes leading and tailing whitespace 351 | # and converts the rest to lowercase. 352 | # Needed: nothing 353 | function convertInput { 354 | local converted 355 | 356 | if [[ ${#} -ne 1 ]] ; then 357 | fatal "${FUNCNAME} " 358 | return 359 | fi 360 | 361 | read converted < 382 | # Checks if element is contained in the list 383 | # Needed: 384 | # - BASH functions 385 | # - fatal 386 | function elementInList { 387 | if [[ ${#} -lt 2 ]] ; then 388 | fatal "${FUNCNAME} " 389 | return 390 | fi 391 | 392 | local -r ELEMENT=${1}; shift 393 | 394 | local thisElement 395 | 396 | while [[ ${#} -gt 0 ]] ; do 397 | thisElement=${1}; shift 398 | if [[ ${thisElement} == ${ELEMENT} ]] ; then 399 | return 0 400 | fi 401 | done 402 | return 1 403 | } 404 | 405 | # Usage: fatal [ [ 425 | # Filter the output from on . When given a field, 426 | # only filter on that field. 427 | # Needed: 428 | # - BASH function 429 | # - fatal 430 | function filterCommand { 431 | declare command 432 | declare -i field 433 | declare match 434 | 435 | if [[ ${#} -ge 2 ]] && [[ ${1} == '--field' ]]; then 436 | field=${2}; shift 2 437 | if [[ ${field} -lt 1 ]]; then 438 | fatal "${FUNCNAME} field has to be 1 or higher" 439 | return 440 | fi 441 | else 442 | field=0 443 | fi 444 | if [[ ${#} -ne 2 ]] ; then 445 | fatal "${FUNCNAME} [--field FIELD_NO] " 446 | return 447 | fi 448 | command="${1}"; match="${2}"; shift 2 449 | 450 | eval ${command} | awk '{ 451 | if( NR == 1 ) { 452 | print $0; 453 | } else if( match($'${field}', "'${match}'") ) { 454 | print $0; 455 | } 456 | }' 457 | } 458 | 459 | # Usage: getCPUTemperature 460 | # Get the temperature of the processor 461 | # Needed: 462 | # - BASH function 463 | # - fatal 464 | # - External function 465 | # - acpi 466 | function getCPUTemperature { 467 | if [[ ${#} -ne 0 ]] ; then 468 | fatal "${FUNCNAME} does not take parameters" 469 | return 470 | fi 471 | 472 | acpi -t | awk '{ print $4 }' 473 | } 474 | 475 | # Usage: getOptionValue