How to Use Function in the shell script + logging using ‘ tee ‘ ?

1. Function in shell script .
– declare the function :
funct_name(){
echo ” fndong ”
}

– called the function ‘ funct_name() ‘
echo ” Hello” funct_name
2. simple program called function + logging using ‘ tee ‘.

2.1. Directory structures
– <My_Directory> , your main directory in linux environment.
– <My_Directory>/config/
config.properties
mysite=fndong.wordpress.com
-<My_Directory>/logs/
log.txt

– <My_Directory>/setToTheLogs.sh

2.2. Source Bash shell script ‘ setToTheLogs.sh ‘
– using tee command to write to ‘ log.txt ‘
– ‘ setLogMsg() ‘ is a function.
– call the function ‘ setLogMsg ‘

base_dir=/<My_Directory>/function_example
config_dir=${base_dir}/config
lOG_DIR=${base_dir}/logs

setLogMsg()
{
echo “\$1 : $1”
echo “\$2 : $2”
echo “$1 : $2 ” | tee -a $LOGFILE

}

# main line code
# use source command to read the parameter key in properties

if [ -f ${config_dir}/config.properties ];then
source ${config_dir}/config.properties
echo “test $mysite”
setLogMsg “INFO” $mysite
else
MSG_ERR=”There is no values in this properties ….”
setLogMsg ” ERROR ” ${MSG_ERR}
fi
3. ‘tee’ command – ‘ echo “$1 : $2 ” | tee -a $LOGFILE ‘ in the method ‘ setLogMsg() ‘ its inserted echo values into ‘ log.txt ‘

Selamat mencuba.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s