if else and function.

Hi,

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"

Thanks for the reply.

As you have specified, Could you please tell me what does below means, how does it work?

[A-D]*)
[V-Z]*|N*) 
[0-9]*) *) 

thanks.

$ 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
$
# /temp/script $$$.$$ @-@
36812$.36812 , we are going to check your Lastname : @-@ ...
Middle of alphabet

@ ctsgnb please use [code] tags when you post code or data sample :wink:

Come on dude ! i fixed it even before you got the time to finish this post :wink:

Thanks...so helpful.

but I didnt understand below:

echo "$1 , we are going to check your Lastname : $2 ... "
fonc_display "$2"

From where the $1 will come or $2 will come to send this to the function.

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
Function_case() {
#statements.......with choices 1,2,3,4...
case $1 in
1) echo "you" ;;
2) echo "me" ;;
*) echo "someone else" ;;
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.

You don't need to write anything since it will never go into the else part ...

indeed , dummy1 = dumm1 will always be true (as well as dummy = dummy will)

:smiley:

Please give more clue about what you are trying to do

further to this,

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.

What do you want the function to do ?

when you call the function, you want it to print the passed arguments ?
or you want it to print you/me just as it currently does ?

I want the function to does 2 actions:

1.) print the passed arguments
2.) and whatever it will print should be on the top of the file/screen.

See the example I have given aboove with expected output after the reply of user "ctsgnb'":

you ### echo'ed by the function
me ### echo'ed by the function

--------------test-----------------
test123 ### echo'ed by else pat
------test2--------
test234 ### echo'ed by else pat

Something like this ?

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

 if [ 1 = 2 ]

or

if [ 1 = 3 ]