The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Return an array of strings from user defined function in awk user_prady Shell Programming and Scripting 2 12-04-2007 12:03 AM
awk printf for user defined variables jabberwocky Shell Programming and Scripting 1 10-02-2007 09:07 AM
User defined service markdrury UNIX for Dummies Questions & Answers 1 02-09-2006 01:08 AM
User defined signal 1 nitesh_raj AIX 0 01-29-2005 02:20 AM
Nawk user-defined function NewbieGirl Shell Programming and Scripting 1 06-20-2003 11:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-15-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Smile need help with User Defined Function

Dear Friends,

I need a help regarding User defined function in shell script.

My problem is as follows:

my_func.sh

Code:
my_funcI(){
        grep 'mystring' I.dat
}
my_funcQ(){
       grep 'mystring' Q.dat
}
myfuncI
myfuncQ

But As both the function has same function only the name of the input file is changing I want to pass the file name to the function and merge them to one function. I want something like below ,

Code:
my_func(FILE){
      grep 'mystring' ${FIlE}.dat
}
my_func(I)
my_func(Q)

Please help me wiht a code..

Thanks in advance
user_prady
  #2 (permalink)  
Old 11-15-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965

Code:
#!/bin/sh

my_func()
{
        grep 'mystring' $1
}

my_func  I.dat
my_func  Q.dat

or


Code:
#!/bin/sh

my_func()
{
        grep 'mystring' $1.dat
}

my_func  I
my_func  Q

  #3 (permalink)  
Old 11-15-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by porter View Post
Code:
#!/bin/sh

my_func()
{
        grep 'mystring' $1
}

my_func  I.dat
my_func  Q.dat

or


Code:
#!/bin/sh

my_func()
{
        grep 'mystring' $1.dat
}

my_func  I
my_func  Q

Thank you very much Mr. Porter.. Will try now shortly...

Regards,
user_prady
  #4 (permalink)  
Old 11-15-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by user_prady View Post
Thank you very much Mr. Porter.. Will try now shortly...

Regards,
user_prady
How do do with this code,

Code:
I_plt=/tmp/I_out.plt$$
Q_plt=/tmp/Q_out.plt$$
 
plot(){
  echo "#!/usr/local/bin/gnuplot -persist" > $1_plt
  echo "plot \"$1_indat.txt\" " >> $1_plt
  
 }

  plot I
  cat $I_plt 
  plot Q
  cat $Q_plt

I tried that one it works fine inside double quotes but when I want to replace "$1"in the output redirection it wont works.

user_prady
shall I have to put single qute or double quote for that $1 or > $"$1_plt" .

Last edited by user_prady; 11-16-2007 at 12:03 AM..
  #5 (permalink)  
Old 11-16-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Try "${1}_plt"
  #6 (permalink)  
Old 11-16-2007
user_prady user_prady is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 163
Quote:
Originally Posted by porter View Post
Try "${1}_plt"
I am afraid Its giving me the same output as "$1_plt" ..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0