Link to home
Start Free TrialLog in
Avatar of cmurugavel
cmurugavelFlag for United States of America

asked on

can we pass parameter to shell function ?

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.

Avatar of ecw
ecw


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
...
Avatar of cmurugavel

ASKER

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?

 
ASKER CERTIFIED SOLUTION
Avatar of ecw
ecw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes. u r right.

I tried. It works fine in ksh.

Thanks.