Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

can we pass parameter to shell function ?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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