__pgrepf()

This function implementing “pgrep -f” command in pure bash. In term of performance wise, a little bit slow than “pgrep -f”.

# __pgrepf() -- get pid match program from /proc
# Usage: __pgrepf [name]
# alternative depend: basename(): //www.ronggeng.net/index.php/2009/04/24/basename/
# dirname(): //www.ronggeng.net/index.php/2009/04/24/dirname/
#
# Example:
#
# if ! __pgrepf httpd; then
#      /etc/init.d/httpd restart
# fi
#

__pgrepf() {
        [ -n "$1" ] && [ -d "/proc" ] || return 1;
        local _mt="$1" _buf _f _pid _pidr="" _me=$$;
        _mt=${_mt// /};
        for _f in /proc/*/cmdline; do
                _pid=$(basename $(dirname $_f));
                ! [[ $_pid =~ ^([0-9]+)$ ]] && continue;
                (( $_pid < = 9 )) && continue;
                [ "$_pid" = "$_me" ] && continue;
                _buf=$(< $_f);
                [ -z "$_buf" ] && continue;
                [[ $_buf = *$_mt* ]] && _pidr+="$_pid ";
                unset _pid _buf;
        done;
        [ -n "$_pidr" ] && {
                echo "$_pidr";
                return 0;
        };
        return 1;
}

emptydir()

# emptydir() -- return true if directory empty
# Usage: emptydir directory
#
# Example:
#
# if ! emptydir /tmp; do
#  echo "/tmp not empty";
#done
#

emptydir() {
        local _dir="$1" _d;
        [ ! -d "$_dir" ] && return 2;
        for _d in $_dir/* $_dir/.*; do
                [ "$_d" = "$_dir/." ] || [ "$_d" = "$_dir/.." ] && continue;
                [ -e "$_d" ] && return 1;
        done;
        return 0;
}

strlen()

# strlen() -- Get string length
# Usage: strlen string
#
# Example:
#
# string="saya suka makan";
# leng=$(strlen "$string");
#
# echo $leng;
#

strlen() {
	echo ${#1};
}

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(): //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}";
}