While do case and getopts shell script linux example

While do case and getopts shell script linux example

1. ‘ getopts ‘ is a syntax to get parse command line argument to our script , this also as an option of argument.

2. parse an argument -a, -b to the script.

while getopts “:ab” opt; do
case ${opt} in
a )
option_a=${OPTARG}
echo ” You choose : $option_a”
;;
b )
option_b=${OPTARG}
echo ” You choose : $option_b”
;;
\? ) echo “Usage: cmd [-a] [-b]”
;;
esac
done

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.

How to read a parameter from properties file by shell script ?

I used ‘source’ syntax to called the parameter from property file.

1. create your directory on your linux OS environment.
– this <mydir> you need to change.

/<mydir>/call_dir/
call_dir.sh
/<mydir>/call_dir/config/
config.properties

/<mydir>/call_dir/logs/
log.txt

2. source bash file – ‘ call_dir.sh ‘

base_dir=/mydir/call_dir
config_dir=${base_dir}/config

# use source command to read the parameter key in properties
if [ -f ${config_dir}/config.properties ];then
source ${config_dir}/config.properties
else
MESSAGE=”Script END”
echo “$MESSAGE”
fi
echo “Log directory $LOG_DIR”

3. properties file config.properties
– values:
LOG_DIR=<mydir>/logs/logs.txt
Terima kasih,
Tubek blog ti…

Using Linux Terminal On Windows 10

Using Linux Terminal On Windows 10

  1. To get a feeling a Linux environment on your windows,  you need to to download Cygwin software. Go here & Download : https://cygwin.com/install.html
  2. Installed  and open the terminal on your desktop after you checked as a shorcut.
  3. How to set variable of my workspace directory?
    1. If your want to set your workspace directory into variable, so it will make your easy way to directly go to your workspace.
    2. just set the alias on your .bashrc file:
      1. alias wspc=’cd /cygdrive/d/users/aku/workspace
    3. Everytime you wanna go to your workspace just type wspc variable.

cd_todirectory

2. Example command to grep the records with total of files?

uid=* dn | grep -i = | wc -l

what command dn for ?
dn  – is command for ldap command for distinguish name  –  uniquely identifies an entry (unique id from ldap directory)

why we need to set pipe for next command ?
| – then add pipe | , connect two commands together so that the output from one program becomes the input of the next program.

what grep command for?
grep – globally search for a regular expression and print all lines containing it

what wc command for?
wcwc, or “word count,” prints a count of newlines, words, and bytes for each input file.

There are various options which you can use along with grep command −
Option     Description
v     Print all lines that do not match pattern.
n     Print the matched line and its line number.
l     Print only the names of files with matching lines (letter “l”)
c     Print only the count of matching lines.
i     Match either upper- or lowercase.

Enjoy Of Learning