Calling Function in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling Function in KSH
# 1  
Old 01-24-2010
Calling Function in KSH

I have a script with 2 functions
1) show_menu
2) create

Ths show_menu function works fine....... Sort of....

When I select option 2 of the menu the code does a few commands and then calls another function called create. It's at this point that I get "create: not found".....

However, when I move this create function to the top of the script, it seems to work?

menu option 2 below:

Code:
2|z|Z)
                #############################################
                #
                # Option 2 bla..bla....
                #             #############################################


echo "\nLooking for a ZFS pool or disks to create a ZFS pool..."
cat <<EOD
        A ZFS pool is needed to store guest domain boot disks.
        The following ZFS pools are available:
EOD
                /usr/sbin/zpool list
        USEZPOOLANSWER=`/usr/bin/ckyorn -p "Would you like to use one of ZFS pools listed for the guest domain boot disk?"`
        if [ "${USEZPOOLANSWER}" = "y" ]
        then
                ZFSPOOLLIST=`/usr/sbin/zpool list -H |\
                /usr/bin/awk '{print $1}'`
                ZFSPOOL=`/usr/bin/ckitem -p "Please select which ZFS pool to use to store the boot disk for this guest domain:" ${ZF
SPOOLLIST}`
                create
        else
                echo "Exiting."
                exit
        fi
#fi
exit
                sleep 5
                show_menu;;

So as you can see from the above code if I move the function create to the top of the script it will work, but if I move the function create to the end of the script I get the "create: not found"...

Thanks for any tips........

Last edited by hxman; 01-24-2010 at 02:57 PM..
# 2  
Old 01-24-2010
Hi.

Is the create function defined after the call to show_menu in your script?

i.e.

OK:
Code:
$ cat Test
create() {
  echo In create function
}

show_menu() {
  echo in show_menu function
  create
}

show_menu

$ ./Test
in show_menu function
In create function

OK:
Code:
$ cat Test
show_menu() {
  echo in show_menu function
  create
}

create() {
  echo In create function
}

show_menu

$ ./Test
in show_menu function
In create function

Not OK:
Code:
$ cat Test
show_menu() {
  echo in show_menu function
  create
}

show_menu

create() {
  echo In create function
}

$ ./Test
in show_menu function
./T: line 4: create: not found

# 3  
Old 01-24-2010
Correct, the create function is declared after the show_menu.

---------- Post updated at 02:34 PM ---------- Previous update was at 02:05 PM ----------

I think you have to delcare the function before it is used........ lol
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

3. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

4. Shell Programming and Scripting

calling function with case

Hi I making a shell script and have some problem to start a function. echo "chose a number to download" read i; case $i in 1) head -1 ~/test3 | tail -1 > ~/test4;; 2) head -2 ~/test3 | tail -1 > ~/test4;; 3) head -3 ~/test3 | tail -1 >... (9 Replies)
Discussion started by: pelle
9 Replies

5. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

6. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

7. UNIX for Dummies Questions & Answers

Calling on function from file??

This may sounds dumb, but can I call on a function from a file? For example, I have a function file full of functions like below (no shell designation): func { echo "blah blah blah 1" } func2 { echo "blah blah blah 2" } func3 { echo "blah blah blah 3" } Am I able to call on any one... (3 Replies)
Discussion started by: douknownam
3 Replies

8. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies

9. Shell Programming and Scripting

Calling other file function

Hi, I am pretty new to unix. Lets say i have a program(run_program) that will call another file function(functiona, in same directory): hence, inside that run_program. i will just call "functiona xx xx" to refer and use that function. this run ok until i run this program from another folder.... (3 Replies)
Discussion started by: maldini
3 Replies

10. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies
Login or Register to Ask a Question