Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

can we pass parameter to shell function ?

Posted on 2002-04-22
4
Medium Priority
?
727 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 60 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

824 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