Dear Friends,
I need a help regarding User defined function in shell script.
My problem is as follows:
my_func.sh
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 ,
my_func(FILE){
grep 'mystring' ${FIlE}.dat
}
my_func(I)
my_func(Q)
Please help me wiht a code..
Thanks in advance
user_prady
porter
November 15, 2007, 9:30pm
2
#!/bin/sh
my_func()
{
grep 'mystring' $1
}
my_func I.dat
my_func Q.dat
or
#!/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
How do do with this 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" .
porter:
Try "${1}_plt"
I am afraid Its giving me the same output as "$1_plt" ..
porter
November 16, 2007, 12:07am
7
Um, then try "$1"_plt or $1"_plt"
Thanks but I am afraid it is also not working..
showing the below error
cat: cannot open /tmp/I_out.plt20546
Now I am doing like this
I_plt=/tmp/I_out.plt$$
Q_plt=/tmp/Q_out.plt$$
plot(){
echo "#!/usr/local/bin/gnuplot -persist" > /tmp/"$1"_out.plt$$
echo "plot \"$1_indat.txt\" " >> /tmp/"$1"_out.plt$$
}
plot I
cat $I_plt
plot Q
cat $Q_plt
But I think there should be a better way to doing this ..
Thanks for your help..
porter
November 16, 2007, 12:19am
9
What shell are you using?
Hi,
Previously, you encountered the error [cat: cannot open /tmp/I_out.plt20546] for the simple reason that the file - /tmp/I_out.plt20546 - mentioned in the first line of your code was NOT being created. The plot function was redirecting output to I_plt in the current working directory and not in /tmp.
I believe the following code should work:
I_plt=/tmp/I_out.plt$$
Q_plt=/tmp/Q_out.plt$$
plot(){
echo "#!/usr/local/bin/gnuplot -persist" > /tmp/$1_out.plt$$
echo "plot \"$1_indat.txt\" " >> /tmp/$1_out.plt$$
}
plot I
cat $I_plt
plot Q
cat $Q_plt
Do let me know
sandy0077:
Hi,
Previously, you encountered the error [cat: cannot open /tmp/I_out.plt20546] for the simple reason that the file - /tmp/I_out.plt20546 - mentioned in the first line of your code was NOT being created. The plot function was redirecting output to I_plt in the current working directory and not in /tmp.
I believe the following code should work:
I_plt=/tmp/I_out.plt$$
Q_plt=/tmp/Q_out.plt$$
plot(){
echo "#!/usr/local/bin/gnuplot -persist" > /tmp/$1_out.plt$$
echo "plot \"$1_indat.txt\" " >> /tmp/$1_out.plt$$
}
plot I
cat $I_plt
plot Q
cat $Q_plt
Do let me know
Dear Sandy ,
Thank for your reply.. But Sandy you can see my previous quote much similar to you.. Anyway thanks a lot again..
user_prady
--