User Tools

Site Tools


bash_functions

This is an old revision of the document!


Bash Functions

index # Print the position of $2 in $1

index() #@ Print the position of $2 in $1
{ #@ USAGE: index STRING SUBSTRING
local idx
case $1 in
*$2*) idx=${1%%$2*}
echo $(( ${#idx} + 1 )) ;;
*) echo 0 ;;
esac
}

From Article

die # Exit with error code and optional message

die() #@ Exit with error code and optional message
{ #@ USAGE: die RETURN_CODE [MESSAGE]
result=$1
shift
[ -n "$*" ] && printf "%s\n" "$*" >&2
exit "$result"
}

From Article

Place SUBSTRING over STRING beginning at OFFSET

overlay() #@ Place SUBSTRING over STRING beginning at OFFSET 
{ #@ USAGE: overlay VARNAME SUBSTRING OFFSET 
local varname=$1 substr=$2 start=$3 string 
eval "string=\${$varname}" 
left=${string:0:start} 
right=${string:start+${#substr}} 
printf -v "$varname" %s%s%s "$left" "$substr" "$right" 
}

From Article

asc() #@ Print the ASCII value of the first character of each argument 
{ #@ USAGE: asc STRING ... 
for char 
do 
printf "%d${EOV-\n}" "'$char" 
done 
} 

From Article

bash_functions.1371958439.txt.gz · Last modified: 2020/08/10 02:28 (external edit)