Operating System - HP-UX
1833744 Members
2949 Online
110063 Solutions
New Discussion

Shell Scripting Help .. Please Help

 
SOLVED
Go to solution
Praveen Hari
Advisor

Shell Scripting Help .. Please Help

Hi All,
I am trying write a shell script to accomplish following:

This is what I want:
I have a file1 with following content:
xxx1 100 105
xxx2 200 210
xxx3 150 175

For each line of this file, I need to genearte a new file with contenats and file name
in the pattern:

file: xxx1.bif
http://yyy.com/test.cgi?cmd=item-100&group=xxx1
http://yyy.com/test.cgi?cmd=item-101&group=xxx1
..
..
http://yyy.com/test.cgi?cmd=item-105&group=xxx1



file: xxx2.bif
http://yyy.com/test.cgi?cmd=item-200&group=xxx2
http://yyy.com/test.cgi?cmd=item-201&group=xxx2
..
..
http://yyy.com/test.cgi?cmd=item-210&group=xxx2


I am trying following script and it doesn't work:

infile=test.txt
url="http://yyy.com/test.cgi?cmd=item-"
uri="group="
suffix=".bif"

cat $infile | while read name start end
do
filename=`expr $name$suffix`
if [$end -gt $start]
then
while [$end -ge $start]
do
echo $url$end$uri$name >> $filename
echo "<>" >> $filename
end=`expr $end-1`
done
fi

done



Any help is appreciated. The inner loop fails.
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Shell Scripting Help .. Please Help

Whenever you say a script fails, provide the error message. That way maybe someone can diagnose by site instead of having to copy the script and data and run the script.

Not knowing the error code, I'll suggest this:

change this:
echo $url$end$uri$name >> $filename

to

echo "${url} ${end} ${uri} ${name}" >> $filename

The spaces may not be necessary. If I can't sleep I'll actually try and run your script and debug it later.

SEP
Will work for points.
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Praveen Hari
Advisor

Re: Shell Scripting Help .. Please Help

I tried changing it as you specified:
I get following message when I execute the script:

./test.csh: [105: not found
./test.csh: [210: not found
./test.csh: [175: not found


I believe it may be due to some syntax error. But I am not able to figure out what it is...
Thanks
Praveen
Mark Grant
Honored Contributor
Solution

Re: Shell Scripting Help .. Please Help

Problem seems to be lack of spaces in your expr.

Assuming you don't mind the "<>" after each entry in each file, the following works.

infile=test.txt
url="http://yyy.com/test.cgi?cmd=item-"
uri="&group="
suffix=".bif"

cat $infile | while read name start end
do
filename=`expr $name$suffix`
if [ $end -gt $start ]
then
while [ $end -ge $start ]
do
echo $url$end$uri$name >> $filename
echo "<>" >> $filename
end=`expr $end - 1`
done
fi

done
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: Shell Scripting Help .. Please Help

Problem seems to be lack of spaces in your expr.

Assuming you don't mind the "<>" after each entry in each file, the following works.

infile=test.txt
url="http://yyy.com/test.cgi?cmd=item-"
uri="&group="
suffix=".bif"

cat $infile | while read name start end
do
filename=`expr $name$suffix`
if [ $end -gt $start ]
then
while [ $end -ge $start ]
do
echo $url$end$uri$name >> $filename
echo "<>" >> $filename
end=`expr $end - 1`
done
fi

done
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: Shell Scripting Help .. Please Help

Problem seems to be lack of spaces in your expr.

Assuming you don't mind the "<>" after each entry in each file, the following works.

infile=test.txt
url="http://yyy.com/test.cgi?cmd=item-"
uri="&group="
suffix=".bif"

cat $infile | while read name start end
do
filename=`expr $name$suffix`
if [ $end -gt $start ]
then
while [ $end -ge $start ]
do
echo $url$end$uri$name >> $filename
echo "<>" >> $filename
end=`expr $end - 1`
done
fi

done
Never preceed any demonstration with anything more predictive than "watch this"
ALPER ONEY
Advisor

Re: Shell Scripting Help .. Please Help

hi,
try to give a space before the value being tested in every "test" command.
Ex:instead of if [$end -gt $start] or while [$end -ge $start], please use if [ $end -gt $start ], while [ $end -ge $start ].
Regards.
ALPER ONEY

never ever give up.
Massimo Bianchi
Honored Contributor

Re: Shell Scripting Help .. Please Help

I think the inner loop fails because you re-assign the variable of the for, and at exit there is a big mess.

try this:

cat $infile | while read name start end
do
START=0
FINAL=0
filename=$name$suffix
if [$end -gt $start]
then
START=$start
FINAL=$end
while (($FINAL >= $START))
do
echo $url$START$uri$name >> $filename
echo "<>" >> $filename
let START=START+1
done
fi

done


Massimo
john korterman
Honored Contributor

Re: Shell Scripting Help .. Please Help

Hi,
if you combine the advice given by Alper and Massimo, I think you should be there.

regards,
John K.
it would be nice if you always got a second chance
Rodney Hills
Honored Contributor

Re: Shell Scripting Help .. Please Help

Here is a short script that uses a perl 1-liner to do what you want.

#!/usr/bin/sh
for $file in file* ; do
perl -ane 'print "http://yyy.com/test.cgi?cmd=item-$_\&group=$F[0]\n" foreach ($F[1]..$F[2])' $file >${file}.bif
done

perl is handy for breaking up text and generating other data.

HTH

-- Rod Hills
There be dragons...
Praveen Hari
Advisor

Re: Shell Scripting Help .. Please Help

Mark,Alpher,MAssimo,

Your all suggestions worked.
Rodney, I haven't tried Perl. I will try.

Thanks all for quick answers.
I like this forum.

Praveen