# strlen() -- Get string length
# Usage: strlen string
#
# Example:
#
# string="saya suka makan";
# leng=$(strlen "$string");
#
# echo $leng;
#
strlen() {
echo ${#1};
}
Monthly Archives: April 2009
strtolower()
# strtolower() -- — Make a string lowercase
# Usage: strtolower string
#
# Example 1:
#
# echo $(strtolower "SAYA SUKAN MAKAN");
#
# Example 2:
#
# string="$(strtolower "SAYA SUKAN MAKAN")";
# echo $string;
if [ -n "${BASH_VERSINFO[0]}" ] && [ ${BASH_VERSINFO[0]} -gt 3 ]; then
# Updated: 20-Aug-2009 - for bash version 4
strtolower() {
[ $# -eq 1 ] || return 1;
echo ${1,,};
return 0;
}
else
strtolower() {
[ $# -eq 1 ] || return 1;
local _str _cu _cl _x;
_cu=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
_cl=(a b c d e f g h i j k l m n o p q r s t u v w x y z);
_str=$1;
for ((_x=0;_x<${#_cl[*]};_x++)); do
_str=${_str//${_cu[$_x]}/${_cl[$_x]}};
done;
echo $_str;
return 0;
}
fi
strtoupper()
# strtoupper() -- — Make a string uppercase
# Usage: strtoupper string
#
# Example 1:
#
# echo $(strtoupper "saya suka makan");
#
# Example 2:
#
# string="$(strtoupper "saya sukan makan")";
# echo $string;
if [ -n "${BASH_VERSINFO[0]}" ] && [ ${BASH_VERSINFO[0]} -gt 3 ]; then
# Updated: 20-Aug-2009 - for bash version 4
strtoupper() {
[ $# -eq 1 ] || return 1;
echo ${1^^};
return 0;
}
else
strtoupper() {
[ $# -eq 1 ] || return 1;
local _str _cu _cl _x;
_cu=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
_cl=(a b c d e f g h i j k l m n o p q r s t u v w x y z);
_str=$1;
for ((_x=0;_x<${#_cl[*]};_x++)); do
_str=${_str//${_cl[$_x]}/${_cu[$_x]}};
done;
echo $_str;
return 0;
}
fi
stristr()
# stristr() -- Case-insensitive strstr()
# Usage: stristsr string match
# depend: strtolower(): http://www.ronggeng.net/index.php/2009/04/24/strtolower/
#
# Example 1:
#
# string="saya SUKA Makan";
# if stristr "${string}" "suk"; then
# echo "OK";
# fi
#
# Example 2:
#
# buff="$(ps ax)";
# if ! stirstr "${buff}" "httpd"; then
# echo "service httpd down";
# fi
stristr() {
[ $# -eq 2 ] || return 1;
! type -t strtolower &>/dev/null && return 1;
local _str1=$(strtolower "$1");
local _str2=$(strtolower "$2");
[ "$_str1" = "$_str2" ] || [[ "$_str1" = *$_str2* ]] && return 0;
return 1;
}
strstr()
# strstr() -- locate a substring
# Usage: strstsr string match
#
# Example 1:
#
# string="saya suka makan";
# if strstr "${string}" "suk"; then
# echo "OK";
# fi
#
# Example 2:
#
# buff="$(ps ax)";
# if ! strstr "${buff}" "httpd"; then
# echo "service httpd down";
# fi
strstr() {
[ $# -eq 2 ] || return 1;
[ "$1" = "$2" ] || [[ "$1" = *$2* ]] && return 0;
return 1;
}
dirname()
# dirname() -- Returns directory name component of path
# Usage: dirname filename
#
# Example 1:
#
# echo $(dirname "/usr/bin/chak");
#
# Example 2:
#
# string="$(dirname "/usr/bin/chak")";
# echo $string;
dirname() {
local dir="${1%${1##*/}}";
[ "${dir:=./}" != "/" ] && dir="${dir%?}";
echo "$dir";
}
yesno()
# yesno() -- prompt user to accept yes or no
# Usage: yesno [prompt msg]
#
# Example:
#
# while ! yesno "sila taip yes"; do
# echo "yes ditaip";
# done
#
yesno() {
[ $# -eq 1 ] || return 1;
local _prompt="$1";
local _ANS="";
read -p "${_prompt} [yes/no]: " _ANS;
while [ "$_ANS" != "yes" ] && [ "$_ANS" != "no" ]; do
read -p "Please enter 'yes' or 'no': " _ANS;
done;
[ "$_ANS" = "yes" ] && return 0;
return 1;
}
basename()
# basename() -- Returns filename component of path
# Usage: basename filename
#
# Example 1:
#
# echo $(basename "/usr/bin/chak");
#
# Example 2:
#
# string="$(basename "/usr/bin/chak.exe" ".exe")";
# echo $string;
basename() {
local name="${1##*/}";
local opt="$2";
[ -n "$opt" ] && name="${name%.$opt}";
echo "${name%$2}";
}
ucfirst()
# ucfirst() -- Make a string's first character uppercase
# Usage: ucfirst string
# depend: strtoupper(): http://www.ronggeng.net/index.php/2009/04/24/strtoupper/
#
# Example:
#
# ucfirst "test test test";
#
if [ -n "${BASH_VERSINFO[0]}" ] && [ ${BASH_VERSINFO[0]} -gt 3 ]; then
# Updated: 20-Aug-2009 - for bash version 4
ucfirst() {
[ $# -eq 1 ] || return 1;
echo ${1^};
return 0;
}
else
ucfirst() {
[ $# -eq 1 ] || return 1;
! type -t strtoupper &>/dev/null && return 1;
echo "$(strtoupper "${1:0:1}")${1:1:${#1}}";
return 0;
}
fi
ucwords()
# ucwords() -- Uppercase the first character of each word in a string
# Usage: ucwords string
# depend: strtoupper(): http://www.ronggeng.net/index.php/2009/04/24/strtoupper/
#
# Example:
#
# ucwords "test test test";
#
ucwords() {
[ $# -eq 1 ] || return 1;
! type -t strtoupper &>/dev/null && return 1;
local _x _c _p _ret="" _str="$1";
_p=0;
for ((_x=0;_x<${#_str};_x++)); do
_c=${_str:$_x:1};
if [ "$_c" != " " ] && [ "$_p" = "0" ]; then
_ret+="$(strtoupper "$_c")";
_p=1;continue;
fi;
[ "$_c" = " " ] && _p=0;
_ret+="$_c";
done;
if [ -n "${_ret:-}" ]; then
echo "${_ret}";
return 0;
fi;
return 1;
}