Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with variable substitution in Korn Shell 3

Status
Not open for further replies.

Amazator

Programmer
Mar 8, 2006
13
IN
Hi friends,

Am trying to write a lil script that reverses a text file top-to-bottom. It's:

Code:
#!/usr/bin/ksh
                                                                                                                                                                   
i=$(cat $1 | wc -l)
                                                                                                                                                                   
while [[ $i -ge 1 ]]
do
 head -$i $1 | tail -1 >> $2
 i=`expr $i - 1`
done

When I run this script, it just hangs (probably waiting for some more input??).....if I replace

Code:
head -$i ...

with

Code:
head -`echo $i` ...

it works! Why is this difference? Am I missing something in my understanding of how a command line is parsed and processed by the Shell? Please help me...would be grateful.

Thnx,
Amazator.
 
Your script works fine on AIX 5.1 ML 9. What OS are you running on?

Try amending the first line to
Code:
#!/usr/bin/ksh -xv
To see what's going on

Columb Healy
 
You may try this:
typeset -i i=$(cat $1 | wc -l)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

The [tt]wc[/tt] command outputs the result left padded with spaces, so [tt]-$i[/tt] has some spaces in the middle, making it to be interpreted as two separate parameters of the [tt]head[/tt] command. When you [tt]echo[/tt]ed it, the spaces disappeared.

By the way, if your above script was for productivity, not just exercise, take a look at the [tt]tac[/tt] command. ( Is GNU utility, possible to exist only on Linuxes. )

Feherke.
 
SED 1liners
Code:
 # reverse order of lines (emulates "tac")
 # bug/feature in HHsed v1.5 causes blank lines to be deleted
 sed '1!G;h;$!d'               # method 1
 sed -n '1!G;h;$p'             # method 2

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
tail -r neatly does the reversing of a text file, though doesn't address your variable substitution question.

I hope that helps.

Mike
 
Another way:
awk '{a[NR]=$0}END{for(i=NR;--i;)print a}' $1 > $2

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, all you wonderful folks who helped me!

As Columb pointed out, I used the switches -xv for /usr/bin/ksh and saw the debug output and it revealed (let's say i is 9):
Code:
 + head - 9 | ...

Probably due to not quoting the "-" character.....it was waiting for my interactive input...the moment I did this:
Code:
head "-$i" $1 | ...

it worked.
 
Folks,

Double-quoting -$i didn't help.....guess Feherke was right. :(

PHV's suggestion worked out:
Code:
typeset -i i=$(cat $1 | wc -l)
[/ccode]

Thnks....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top