The below code snippet shows how to compare two strings
#!/bin/ksh
str1="Chidam";
str2="Chidam";
if [ "$str1" = "$str2" ] ; then
echo -e "String str1 and str2 are same"
echo -e "string1=$str1 and string2=$str2"
fi
str1="Chidam";
str2="Chidamk";
if [[ "$str1" != "$str2" ]] ; then
echo -e "String str1 and str2 are not equal"
echo -e "string1=$str1 and string2=$str2"
fi
OUTPUT :
-bash-3.2$ sh str_comp
String str1 and str2 are same
string1=Chidam and string2=Chidam
String str1 and str2 are not equal
string1=Chidam and string2=Chidamk
= --> EQUAL OPERATOR
!= --> NOT EQUAL OPERATOR
A Language is not worth knowing unless it teaches you to think differently.
Tuesday, January 1, 2013
How to return the value to the function calling using return key
The below code snippet shows how to return the value to the function calling and how to override the delcared variable value.
funReturn1()
{
x=10;
echo "$x"
}
rvalue=`funReturn1`
echo -e "function funReturn return rvalue = $rvalue\n"
y=10;
funReturn()
{
y=20;
return;
}
funReturn
echo -e "function funReturn return y = $y\n"
OUTPUT :
-bash-3.2$ sh func_return
function funReturn return rvalue = 10
function funReturn return y = 20
-bash-3.2$
Explanation :
function funReturn1 is just return the value to the function calling and assign the return value into rvalue variable here only one echo statement is used dont use more than one echo statement.
y=10; # declared the value in global
funReturn()
{
y=20; # here the value y=10 is reassigned to y=20 after the function call the value should be updated to y=20
return;
}
funReturn
funReturn1()
{
x=10;
echo "$x"
}
rvalue=`funReturn1`
echo -e "function funReturn return rvalue = $rvalue\n"
y=10;
funReturn()
{
y=20;
return;
}
funReturn
echo -e "function funReturn return y = $y\n"
OUTPUT :
-bash-3.2$ sh func_return
function funReturn return rvalue = 10
function funReturn return y = 20
-bash-3.2$
Explanation :
function funReturn1 is just return the value to the function calling and assign the return value into rvalue variable here only one echo statement is used dont use more than one echo statement.
y=10; # declared the value in global
funReturn()
{
y=20; # here the value y=10 is reassigned to y=20 after the function call the value should be updated to y=20
return;
}
funReturn
Global & Local Variable in Shel Script
The below code snippet shows how can we declare global and local variables in shell scripts.
#!/bin/bash
x=10
increment()
{
echo "global variable : before increment value x=$x"
(( x++ ))
}
increment
echo -e "global variable : after increment value x=$x\n"
localVar()
{
local var_local=50;
echo "inside localVar function value of var_local = $var_local"
}
localVar
echo "inside localVar function value of var_local = $var_local"
OUTPUT :
-bash-3.2$ sh local_global_var_ex
global variable : before increment value x=10
global variable : after increment value x=11
inside localVar function value of var_local = 50
inside localVar function value of var_local =
global variable is declared outside the function
local variable is delcared in side the function with key word local.
we cant use local variable outside the function but global variable can use inside/outside the function
#!/bin/bash
x=10
increment()
{
echo "global variable : before increment value x=$x"
(( x++ ))
}
increment
echo -e "global variable : after increment value x=$x\n"
localVar()
{
local var_local=50;
echo "inside localVar function value of var_local = $var_local"
}
localVar
echo "inside localVar function value of var_local = $var_local"
OUTPUT :
-bash-3.2$ sh local_global_var_ex
global variable : before increment value x=10
global variable : after increment value x=11
inside localVar function value of var_local = 50
inside localVar function value of var_local =
global variable is declared outside the function
local variable is delcared in side the function with key word local.
we cant use local variable outside the function but global variable can use inside/outside the function
For Loop in Shell Script
The below code snippet shows how to use for loop in shell script .
List="one two three"
for a in $List # Splits the variable in parts at whitespace.
do
echo "$a"
done
# one
# two
# three
echo "---"
for a in "$List" # Preserves whitespace in a single variable.
do # ^ ^
echo "$a"
done
# one two three
echo "---"
for file in `ls * 2>/dev/null`
do
echo "File name : $file"
done
OUTPUT :
-bash-3.2$ sh for-loop-ex
one
two
three
---
one two three
---
File name : arithmatic_sh
File name : case_example
File name : check_min_params
File name : comparion_op_ex
File name : for-loop-ex
File name : passing_parameter_mech
File name : simple_function
File name : simple_multiplication
Explanation :
ls * 2>/dev/null
it will try to list all the files in current directory if there is no files found in current directory means it surpress the warning message
/dev/null this is the 0 device file
List="one two three"
for a in $List # Splits the variable in parts at whitespace.
do
echo "$a"
done
# one
# two
# three
echo "---"
for a in "$List" # Preserves whitespace in a single variable.
do # ^ ^
echo "$a"
done
# one two three
echo "---"
for file in `ls * 2>/dev/null`
do
echo "File name : $file"
done
OUTPUT :
-bash-3.2$ sh for-loop-ex
one
two
three
---
one two three
---
File name : arithmatic_sh
File name : case_example
File name : check_min_params
File name : comparion_op_ex
File name : for-loop-ex
File name : passing_parameter_mech
File name : simple_function
File name : simple_multiplication
Explanation :
ls * 2>/dev/null
it will try to list all the files in current directory if there is no files found in current directory means it surpress the warning message
/dev/null this is the 0 device file
Dereferenced Passing Parameter Mechanism in Shell Script Function
The below code snippet shows how to pass the parameters to the function in shell script. If we used $1,$2,,,$n with in the function
$1 says first argument of this function
$2 says second argument of this function
.
.
.
$n says nTh argument of this function.
#!/bin/bash
add()
{
x=$1;
y=$2;
z=`expr $x + $y`;
echo $z
}
sum=`add 10 20`
echo "sum x=10 y=20 x+y=$sum"
OUTPUT:
-bash-3.2$ sh passing_parameter_mech
sum x=10 y=20 x+y=30
sum=`add 10 20`
calling the function add with passed two parameters 10 and 20 and return value is assigned into variable sum here need to put it both side of function call `(tilde) sign.
x=$1
values of 10 is assigned into the variable x
y=$2
values of 20 is assigned into the variable y
we can call x and y as dereferenced variables in shell function.
$1 says first argument of this function
$2 says second argument of this function
.
.
.
$n says nTh argument of this function.
#!/bin/bash
add()
{
x=$1;
y=$2;
z=`expr $x + $y`;
echo $z
}
sum=`add 10 20`
echo "sum x=10 y=20 x+y=$sum"
OUTPUT:
-bash-3.2$ sh passing_parameter_mech
sum x=10 y=20 x+y=30
sum=`add 10 20`
calling the function add with passed two parameters 10 and 20 and return value is assigned into variable sum here need to put it both side of function call `(tilde) sign.
x=$1
values of 10 is assigned into the variable x
y=$2
values of 20 is assigned into the variable y
we can call x and y as dereferenced variables in shell function.
Check minimum Command Line Parameters in Shell Script
The below code snippet shows how can check whether the minimum number for paramters are passed to the shell script or not.
-bash-3.2$ cat check_min_params
MINPARAMS=10
echo "All the command-line parameters are: "$*""
if [ $# -lt "$MINPARAMS" ]
then
echo
echo "This script needs at least $MINPARAMS command-line arguments!"
fi
echo
exit 0
OUTPUT :
-bash-3.2$ sh check_min_paramsAll the command-line parameters are:
This script needs at least 10 command-line arguments!
-bash-3.2$ sh check_min_params p1 p2 p3All the command-line parameters are: p1 p2 p3
This script needs at least 10 command-line arguments!
-bash-3.2$ sh check_min_params p1 p2 p3 p4 p5 p6 p7 p8 p9 p10All the command-line parameters are: p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
$# --> Total number of parameter passed from shell script command line to the program.
-bash-3.2$ cat check_min_params
MINPARAMS=10
echo "All the command-line parameters are: "$*""
if [ $# -lt "$MINPARAMS" ]
then
echo
echo "This script needs at least $MINPARAMS command-line arguments!"
fi
echo
exit 0
OUTPUT :
-bash-3.2$ sh check_min_paramsAll the command-line parameters are:
This script needs at least 10 command-line arguments!
-bash-3.2$ sh check_min_params p1 p2 p3All the command-line parameters are: p1 p2 p3
This script needs at least 10 command-line arguments!
-bash-3.2$ sh check_min_params p1 p2 p3 p4 p5 p6 p7 p8 p9 p10All the command-line parameters are: p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
$# --> Total number of parameter passed from shell script command line to the program.
Number Comparison Operater in Shell Script
The below code snippet shows how can we use comparison operator in shell script for number.
-bash-3.2$ cat comparion_op_ex
#!/bin/bash
x=10
y=10
echo -e " value x = $x and y = $y"
if [ $x -eq $y ] ; then
echo -e " x and y are same value\n "
fi
x=10
y=20
echo -e " value x = $x and y = $y"
if [ $x -ne $y ] ; then
echo -e " x and y are not equal \n"
fi
x=20
y=10
echo -e " value x = $x and y = $y"
if [ $x -gt $y ] ; then
echo -e " x is greater than y \n"
fi
x=10
y=20
echo -e " value x = $x and y = $y"
if [ $x -lt $y ] ; then
echo -e " x is less than y\n"
fi
OUTPUT :
-bash-3.2$ sh comparion_op_ex
value x = 10 and y = 10
x and y are same value
value x = 10 and y = 20
x and y are not equal
value x = 20 and y = 10
x is greater than y
value x = 10 and y = 20
x is less than y
-bash-3.2$
gt --> greater than
lt --> less than
eq --> equal
ne --> not equal
-bash-3.2$ cat comparion_op_ex
#!/bin/bash
x=10
y=10
echo -e " value x = $x and y = $y"
if [ $x -eq $y ] ; then
echo -e " x and y are same value\n "
fi
x=10
y=20
echo -e " value x = $x and y = $y"
if [ $x -ne $y ] ; then
echo -e " x and y are not equal \n"
fi
x=20
y=10
echo -e " value x = $x and y = $y"
if [ $x -gt $y ] ; then
echo -e " x is greater than y \n"
fi
x=10
y=20
echo -e " value x = $x and y = $y"
if [ $x -lt $y ] ; then
echo -e " x is less than y\n"
fi
OUTPUT :
-bash-3.2$ sh comparion_op_ex
value x = 10 and y = 10
x and y are same value
value x = 10 and y = 20
x and y are not equal
value x = 20 and y = 10
x is greater than y
value x = 10 and y = 20
x is less than y
-bash-3.2$
gt --> greater than
lt --> less than
eq --> equal
ne --> not equal
Subscribe to:
Posts (Atom)
Table Lock - Query in Oracle APPS
SELECT client_identifier, module, action, s.* FROM v$session s WHERE sid IN (SELECT session_id FRO...
-
1. To download Ldt file for a Concurent Program FNDLOAD [username]/[password] O Y DOWNLOAD $FND_TOP/patch/115/import/afcpprog.lct file_nam...
-
The profile information available in the HZ_CUSTOMER_PROFILES can be created in three levels namely Party, Customer Account and Customer...
-
Here the article will describe about how to register WebADI to Responsibility menu Go to System Administrator --> Security --> Respo...