Operating System - HP-UX
1748006 Members
4480 Online
108757 Solutions
New Discussion юеВ

Korn shell script problem

 
SOLVED
Go to solution
Henry Chua
Super Advisor

Korn shell script problem

I can't seems to make this program work. It is suppose to read line by line of file TAR until EOF. something seems to be run with the awk command, I dun seems to know whether to put the ` ' or " . anyone can help?

y=1
x=1
while [ y -ne 0 ]
do
awk "NR==$x" $TAR > $BUF
let "x=x+1"
y=`wc -c $BUF`
done
9 REPLIES 9
Jean-Luc Oudart
Honored Contributor

Re: Korn shell script problem

would that be
while [ $y -ne 0 ]
instead of
while [ y -ne 0 ]

Regards
Jean-Luc
fiat lux
Henry Chua
Super Advisor

Re: Korn shell script problem

Thanks Jean,

But is the awk command I issued correct?

thanks!
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Korn shell script problem

Hi Henry,

I don't know what you are trying to do. Why do you want to use a script to read it line by line? Can you do something like

while read line
do
echo $line

done < $TAR

to read line by line?.

If you insist on using the script, then there are couple of issues.

1. while [ y -ne 0 ] should be while [ $y -ne 0 ]. Use [[ and ]] instead.
2. awk "NR==$x" won't work. You can't assing a shell variable inside awk. Do
awk -v x=$x 'NR==x' $TAR > $BUF
No $ for variables inside awk.
3. Try (( x = $x + 1 )) instead of let.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Henry Chua
Super Advisor

Re: Korn shell script problem

Hi Sri

as advised I used
"awk -v x=$x 'NR==x' $TAR > $BUF"
and it produces

awk: syntax error near line 1
awk: bailing out near line 1

what could be wrong?


thanks!
Sridhar Bhaskarla
Honored Contributor

Re: Korn shell script problem

It works for me. Try with sh -x <script> and see if you get more information.

$TAR=testfile
$x=2
$awk -v x=$x 'NR==x' $TAR
testline 2

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
MarkSyder
Honored Contributor

Re: Korn shell script problem

Henry,

When I first started working with unix, one of my colleagues said "A shell script doesn't deserve the name shell script unless it contains at least one awk".

I don't agree with him! Sri's "while read line" suggestion is just as effective as awk and easier to script.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Gregory Fruth
Esteemed Contributor

Re: Korn shell script problem

You can indeed use shell variables in
an awk program, but only because the shell
substitutes inside of double quotes:

x=2
awk "NR == $x" some_file

If you use single quotes the shell won't
substitute for $x.

However, I don't understand why you are
doing this in such a roundabout manner.
Replacing your entire program with
"cat $TAR" will also read from TAR until
EOF. What are you trying to accomplish?
Henry Chua
Super Advisor

Re: Korn shell script problem

hi Sri,

DOes that mean I can read line 1 of the file this way:
"
TAR = filename
x=1
while read x
do
echo $x

done < $TAR
"
Thanks!


Henry Chua
Super Advisor

Re: Korn shell script problem

I got it! thank!!!