pass function as argument to a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pass function as argument to a function
# 1  
Old 06-13-2010
pass function as argument to a function

I have the following code :
Code:
function1 ()
{
   print "January"
}

function2()
{
    case $1 in
                  January)
                  print "Dzisiaj mamy styczen"
                  ;;
                  *)                  
                  ;;

}

main()
{
      Month="`function2 $(function1)`"
      print "$Month"
}


# start of a program
main


Above code doesn't work.

Please help how to pass function1 as argument to function2 and save output in variable Month.
# 2  
Old 06-13-2010
What works (for me / in Bash, that is ...):

Code:
#!/bin/bash

function1 ()
{
  echo "January"
}

function2 ()
{
  case $1 in
    January) echo "Dzisiaj mamy styczen" ;;
    *      ) ;;
  esac
}

main ()
{
  Month=$( function2 $(function1) )
  echo "$Month"
}

main

exit 0 
# finis

Code:
[house@leonov] bash test.bash
Dzisiaj mamy styczen

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

2. Shell Programming and Scripting

Strange third argument in shell function

In one of my shell function I found the following set_default_values () { prog=$1 PROC_DT=$2 RESET_ALL="${3-N}" #echo "Processing date as passed = , Program name = " ...... I understand the first and second arguments, but don't understand... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

passing argument from one function to another

Hi all, In the given script code . I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob(). Any help? Thanks #!/bin/bash ... (55 Replies)
Discussion started by: nrjrasaxena
55 Replies

4. Shell Programming and Scripting

Passing commandline argument to a function

Hi, I have 2 ksh scripts. Script1.ksh contains function definition. script1.ksh function f1() { while getopts a:c: args do case $args in a) ARG1=$OPTARG ;; c) ARG2=$OPTARG ;; \?) echo "Error no valid Arguments passed" esac done echo $ARG1 echo $ARG2 script2.sh (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

5. Shell Programming and Scripting

Function call with argument doubt

Hi all, I am having a problem with user defined function call. I am new into the concept of shell script UDFs. My function is: iterate_directory() { cd $1 k=0 for i in * do if then ARR=${i} fi done echo ${ARR } } (4 Replies)
Discussion started by: canishk
4 Replies

6. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies

7. Shell Programming and Scripting

Passing more than one argument in a function

Hi All, Calling a function with one argument and storing the return value in a shell script is as below:( so far I know) value="`fun_1 "argument1"`" Its working perfectly for me. Can u help me with passing more than one argument and storing the return value Thnaks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

8. Shell Programming and Scripting

How to pass variables to FUNCTION ?

Hi... Actually, I want to pass a few variables to the function and print it. But, its looks like not working. Could some body help me how could i do that ?... below is my program... #!/usr/bin/ksh usage() { echo "Usage: $0 -n -a -s -w -d" exit } rename() { echo "rename $1 $2"... (5 Replies)
Discussion started by: bh_hensem
5 Replies

9. UNIX for Dummies Questions & Answers

Pass argument to function

Hi, Can someone please explain to me how I can get a function to recognize a file given as an argument to a script. Suppose the script has the argument as follows: sh script file and the function is as follows: function display_file () { cat $1 } and it s then called #main program... (1 Reply)
Discussion started by: Knotty
1 Replies

10. UNIX for Dummies Questions & Answers

Passing Argument to Function

May i know how to pass an argument to a function in a shell script? Sorry, i din stated that it is in a shell script in my previous post. Means: checkStatus() { ........... } read status; I wanna use the status in the function checkstatus, how... (2 Replies)
Discussion started by: AkumaTay
2 Replies
Login or Register to Ask a Question