I ahve many if else functions in my script but no function is avaialbe.
Now I want to write a function and function body should have case statements. But the choice for that case statements should come as a parameter from the each ELSE part of my if-else statements.
for eg:
Function_case() {
case statements.......with choices 1,2,3,4...
case1:
echo "you"
case2:
echo "me"
}
if ["keyword" = "keyword"]
then
do this
else
function_case; #####here I want to pass 1 for my above function, so that case1 will get execute####
fi
if ["keyword1" = "keyword1"]
then
do this
else
function_case; ####here I want to pass 2 for my above function, so that case2 will get execute#####
fi
and so on......
Please help me out with the proper syntax to pass the argument for function_case and with case syntax as well.
variable must be preceed by $ sign
you need a space after [ and before ]
Regarding case statement, here is just a little example (totally useless, just for education purpose)
#!/bin/ksh
# expect Firstname Lastname as argument
fonc_display(){
case "$1" in
[A-D]*) echo "name at beginning of alphabet" ;;
[V-Z]*|N*) echo "name at the end of alphabet or starting with same letter than Norris" ;;
[0-9]*) echo "Error : expected argument is a not a number" ;;
*) echo "Middle of alphabet" ;;
esac
}
echo "$1 , we are going to check your Lastname : $2 ... "
fonc_display "$2"
$ cat chk
#!/bin/ksh
# expect Firstname Lastname as argument
fonc_display(){
case "$1" in
[A-D]*) echo "name at beginning of alphabet" ;;
[V-Z]*|N*) echo "name at the end of alphabet or starting with same letter than Norris" ;;
[0-9]*) echo "Error : expected argument is a not a number" ;;
*) echo "Middle of alphabet" ;;
esac
}
echo "$1 , we are going to check your Lastname : $2 ... "
fonc_display "$2"
$ ksh chk Chuck Norris
Chuck , we are going to check your Lastname : Norris ...
name at the end of alphabet or starting with same letter than Norris
$ ksh chk Tux Greenland
Tux , we are going to check your Lastname : Greenland ...
Middle of alphabet
$ ksh chk Amit Matur
Amit , we are going to check your Lastname : Matur ...
Middle of alphabet
$ ksh chk BadEntry 9043
BadEntry , we are going to check your Lastname : 9043 ...
Error : expected argument is a not a number
$ ksh chk Diego Zorro
Diego , we are going to check your Lastname : Zorro ...
name at the end of alphabet or starting with same letter than Norris
$
When used inside the function , the $1 refer to the first argument passed to the function
When used outside the function, $1 refer to the firsta argument passed to the script
$2 behave the same way but refer to the second argument.
Here, since $2 is used outside the function, $2 refer to the second argument passed to the chk script.
Yes. I thought so.
But my requirement is bit different. I want to pass argument to a function from if-else statement which resides outside the function in the same script.
Any help?
Function_case() {
#statements.......with choices 1,2,3,4...
case $1 in
1) echo "you" ;;
2) echo "me" ;;
*) echo "someone else" ;;
esac
}
echo "Enter keyword ;"
read kw
case $kw in
"keyword") function_case 1 ;; #####here I want to pass 1 for my above function, so that case1 will get execute####
"keyword1") function_case 2 ;; ####here I want to pass 2 for my above function, so that case2 will get execute#####
*) echo "wrong password" ;;
esac
Above part is fine...i got it. but now I need to pass 1, 2, 3 arguements from my each else statement of if-else..
for eg:
if [ dummy = dummy]
then
do this
else
<call Function_case with passing the arguement 1>
fi
if [ dummy1 = dummy1 ]
then
do this
else
<call Function_case with passing the arguement 2>
fi
Please tell me the code to write in this else part.
I want to echo some comntent on the top of my file based on the if-else statement.
for eg:
Function_case() {
#statements.......with choices 1,2,3,4...
case $1 in
1) echo "you" ;;
2) echo "me" ;;
*) echo "someone else" ;;
esac
}
echo "--------------test-----------------"
if [ 1 = 2]
then
do this
else
echo "test123"
<call Function_case with passing the arguement 1>
fi
echo "------test2--------"
if [ 1 = 3 ]
then
do this
else
echo "test234"
<call Function_case with passing the arguement 2>
fi
----------- Now output from above else part will come as below-----
--------------test-----------------
test123
you
------test2--------
test234
me
BUT I WANT THE OUTPUT AS BELOW:
you
me
--------------test-----------------
test123
------test2--------
test234
It each time else part calls function_case then the output from function_case shoul display on top.
disp(){
echo "
#================================
#
# $3
#
#================================
# $1 $2
#================================
"
}
echo "Do you want to run the default test [y/n] (answer no to be prompt for your own values)?"
read ans
case $ans in
y|Y|yes|YES|Yes)
f=Chuck
l=Norris
t="WELCOME"
;;
n|N|no|No|NO)
echo "Enter Firstname:" ; read f
echo "Enter Lastname:" ; read l
echo "Enter the title:" ; read t
;;
esac ; clear
disp "$f" "$l" "$t"
by the way, the way you use the if condition makes no sens the following will just always be false so it make no sens to use a if condition since it will never go in the " then " part only and always in the " else " part