Calls

Calls

Post by Michael Newcom » Sat, 18 Dec 1999 04:00:00



Hi all,

 I'm trying to figure out it the Korn shell allows the programmer to use
calls and returns much like
in C programming?

My problem is I'm working on a delimited database, and I want to create a
shell to perform
additions, deletions, display, and modifications of the data with a menu
that is centered and
prompts the user for a choice, and then allows the various functions to
occur.

Anyone's help would be appreciated.

Regards,
Mike

 
 
 

Calls

Post by Chris Costell » Sat, 18 Dec 1999 04:00:00


[message reformatted for readability]


> Hi all,
> I'm trying to figure out it the Korn shell allows the programmer to
> use calls and returns much like in C programming?
> My problem is I'm working on a delimited database, and I want
> to create a shell to perform additions, deletions, display, and
> modifications of the data with a menu that is centered and prompts the
> user for a choice, and then allows the various functions to occur.

   I can't help you with designing your menu, only your question
about functions.

   Try this:

---
#!/bin/ksh

function myfunc {
        if [ -z "$1" ]
        then
                return 2
        fi

        if [ "$1" = "OK" ]
        then
                return 0    # OK
        else
                return 1    # Not OK
        fi

Quote:}

myfunc
echo "Return value for myfunc with no args: $?"
myfunc OK
echo "Return value for 'myfunc OK': $?"
myfunc NOT_OK
echo "Return value for 'myfunc NOT_OK': $?"
---



 
 
 

Calls

Post by Michael Newcom » Sat, 18 Dec 1999 04:00:00


Chris,

     here is my new problem,

    I need the functions to be in the same file as the script.

Example:

menu () {
echo "this is the menu"
read yourselection
case $line in
[Aa]) return 11;;
[Dd]) return 12;;
[Mm]) return 13;;
[Ii}]) return 14;;
[Xx} return 15;;
esac

Quote:}

add () {
echo "this is a test of add"

Quote:}

delete() {
echo "this is a test of delete"

Quote:}

modify () {
echo "this is a test of modify"

Quote:}

inquiry() {
echo "this is a test to inquiry"

Quote:}

exitprgm () {
exit 1

Quote:}

"end of example"

How should the main program look like and what is wrong with the current
scheme?

Any help will be appreciated.

Mike


> [message reformatted for readability]


> > Hi all,

> > I'm trying to figure out it the Korn shell allows the programmer to
> > use calls and returns much like in C programming?

> > My problem is I'm working on a delimited database, and I want
> > to create a shell to perform additions, deletions, display, and
> > modifications of the data with a menu that is centered and prompts the
> > user for a choice, and then allows the various functions to occur.

>    I can't help you with designing your menu, only your question
> about functions.

>    Try this:

> ---
> #!/bin/ksh

> function myfunc {
>         if [ -z "$1" ]
>         then
>                 return 2
>         fi

>         if [ "$1" = "OK" ]
>         then
>                 return 0    # OK
>         else
>                 return 1    # Not OK
>         fi
> }

> myfunc
> echo "Return value for myfunc with no args: $?"
> myfunc OK
> echo "Return value for 'myfunc OK': $?"
> myfunc NOT_OK
> echo "Return value for 'myfunc NOT_OK': $?"
> ---



 
 
 

Calls

Post by Chris Costell » Sat, 18 Dec 1999 04:00:00



> How should the main program look like and what is wrong with the current
> scheme?

   There's nothing wrong with it and you appear capable of
figuring out how to actually do this on your own.  Here's a hint,
though:  Use a case statement on the return value of menu.