Solved

can we pass parameter to shell function ?

Posted on 2002-04-22
4
713 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now