Solved

can we pass parameter to shell function ?

Posted on 2002-04-22
4
721 Views
Last Modified: 2013-12-26
I would like to create a generalized function in KSH to issue an error message.

I am having files like file1, file2,file3.....

main script start here :
==========================================

if (test $flg -eq 1) then
....
check_file
....
elseif (test $flg -eq 2) then
.....
check_file
.....
elseif (test $flg -eq 3) then
.....
check_file
.....
else
       echo "Nothing passed"

fi

echo "DONE"


called function
----------------

function check_file{
       if (test -f $file_n ) then
           echo "file : $file_n exist"
       else
           echo "file not exist"
       fi
}

My question is how to pass those files (file1, file2, file3, etc) into function "check_file" ?


Any Help ?

Thanks in advance.

0
Comment
Question by:cmurugavel
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 6959330

function check_file{
      if (test -f "$1" ) then
          echo "file : $1 exist"
      else
          echo "file not exist"
      fi
}

...
check_file file1
...
check_file file2
...
check_file file3
...
0
 
LVL 1

Author Comment

by:cmurugavel
ID: 6959354
How do we send the $1 from calling place?

sometime I may pass the "file" in 4th or 5th.. parameter, In this case, again I have to change $n  (n can be 1,2 ..n)
in the function definition.

In C,
-------

int var=10;

main ()
{
   fn(var)
}

fn (int a_var)
{
  if (var != 10) {
    return 1;
  eles
    return 0;
}


------------------------

I would like create a shell like above menthod. Is it possible?

 
0
 
LVL 5

Accepted Solution

by:
ecw earned 20 total points
ID: 6960780
In ksh, during the execution lifetime of a function, positional parameters are the arguments to the function, not the arguments to the script.  Bourne shell isn't as advanced, if one passes arguments to a function, these replace the global positonal parameters.  Try,

pargs()
{
  echo "pargs: first arg is $1, all args are $*"
}

pargs hello world

echo "script: first args is $1, all args are $*"

run the above under ksh, and plain old sh (not POSIX sh).

As your example is obviously not bourne shell, you can user
  check_file "$1"
if the file is the first arg to the script (or calling function), or
  check_file "$2"
if the second arg, etc.
0
 
LVL 1

Author Comment

by:cmurugavel
ID: 6962061
Yes. u r right.

I tried. It works fine in ksh.

Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 106
VBA "SendKeys" Syntax for Multiple Keystrokes 7 125
wordcount challenge 11 144
"Black Box" Testing of Control System Software 2 89
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question