#!/bin/bash # Todd's standard bash script library. # Contains a bunch of useful scriptlets. # # YMMV ## ## bashing bash ## # Dereferences a variable. # Returns the contents of the variable specified as a parameter. # E.g. if $1=foo this will return the value of the variable $foo # $1 - the variable nam to de-reference dereferenceVar() { varname="$1" eval result=\$${varname} echo "${result}" } ## ## String handling ## # Left pads, with zeros, a number to make # it of a specified length # If instead of a max lenght, you know the max number to # be accomidated, call with ${#maxNumber} # $1 - Number to pad # $2 - length of final result leftZeroPad() { number=$1 resultLength=$2 # This should provide enough potential padding hold="00000000000000000000000000000000000000000000000000000000000000${number}" echo "${hold:${#hold} - ${resultLength}:${resultLength}}" } # Truncates (right side) a string to a maximum length # $1 - String to truncate # $2 - Maximum length truncate() { string="${1}" maxlen="${2}" echo "${string}" | cut -c -${maxlen} } # Convert the given argument into an all lowercase string. # $1 - The String to convert to lowercase toLower() { echo $1 | tr "[:upper:]" "[:lower:]" } # Convert the given argument into an all uppercase string. # $1 - The String to convert to uppercase toUpper() { echo $1 | tr "[:lower:]" "[:upper:]" } # Returns a the needle string if found in haystack # $1 - the haystack string -- whitespace delimited string containing thru which to search # $2 - the needle string -- the string for which to look in the haystack. containsString() { haystack="${1}" needle="${2}" for str in ${haystack}; do if [ "${str}" == "${needle}" ]; then echo "${needle}" return fi done } ### ### Other stuff ### # Gets a chmod-usable parameter from the current permissions of a file. # E.g. if a file 'foo.sh' has the permissions: # -rwxr-xr-x 1 usr grp 0 2000-01-01 16:20 foo.sh # This script would return '0755'. # Note that the sticky bit is unsupported. # Returns the octal string or an empty string if the file doesn't exist. # $1 - The filename. getChmodParamFromFile() { file="${1}" # quick check if [ -d "${file}" ]; then return; fi permString="`/bin/ls -l ${file} | cut -d " " -f 1`" chmodString="0" # process the permString in three chunks for chunk in 0 1 2; do let indx=${chunk}*3+1 subchunk="${permString:${indx}:3}" component=0 if [[ "${subchunk:0:1}" != "-" ]]; then let component=$component+4 fi if [[ "${subchunk:1:1}" != "-" ]]; then let component=$component+2 fi if [[ "${subchunk:2:1}" != "-" ]]; then let component=$component+1 fi # Add in this digit chmodString="${chmodString}${component}" done echo "${chmodString}" } # Sets the permissions of dirs/files within that dir. # The supplied directory and all subdirectories will have # their permissions set to the supplied 'directory permission', # All files in the supplied directory and below will have # their permssion set to the supplied 'file permssion'. # Returns error if directory does not exist. # $1 - The directory permission # $2 - The file permission # $3 - The directory on which to operate radchmod() { dirPerms="${1}" filePerms="${2}" directory="${3}" # Quick check if [ ! -d "${directory}" ]; then # Directory does not exist return 1 fi # Process directory chmod "${dirPerms}" "${directory}" cd "${directory}" find . -type d -exec chmod "${dirPerms}" {} \; find . -type f -exec chmod "${filePerms}" {} \; return 0 }