Wierd issue using wc -l in a script

Hi experts,
This is what Im doing...

 file_cnt=`cat abc_ | wc -l`
head -$file_cnt abc > abc_2
 
if [ "$?" != "0" ]; then
   error "failed on rename abc_ > abc_2"
   exit 1
fi

 
try_run "rm -f abc_"
try_run "mv abc_2 abc_"

This is what the output on screen, I get on executing---->
+ + wc -l
+ cat abc_
file_cnt= 236
+ head - 236 abc_
-: No such file or directory
236: No such file or directory
+ [ 1 != 0 ]
+ error failed on rename abc_ > abc_2
+ exit 1

I aint able to understand why i get the space between the dash and 236
head - 236 abc_

Please suggest... Many Thanks

Looks like a typo to me:

head -$file_cnt abc > abc_2

should read:

head -$file_cnt abc_ > abc_2

Although that means all the lines in abc_ will end up on abc_2?

What would be better is to define:
FILE=abc_
at the top of the script and the use ${FILE} from then on.

Ignoring the typo abc_ .
This should fix it.

file_cnt=`cat abc | wc -l | sed -e "s/ //g"`

I have seen this issue in various unixes.
Had you stated which Operating System and Shell you were using the answer would be more specific.

That works perfectly... :slight_smile: thanks a lot....

First the shell is ksh

Now please let me tell you why I need to do this....
This is what is happening......
dev$ wc -l abc_
236 abc_
dev$ head -236 abc_ > abc_2
dev$ wc -l abc_2
229 abc_2
dev$

have no clue on why this is happening.....

---------- Post updated at 02:55 AM ---------- Previous update was at 02:53 AM ----------

Sure Tony.. Thanks a lot...
i just post the reason why i need to do this... if you can please take a look on it.. and suggest me ?

It is a trivial bug in some versions the "wc" program.

Here's another detail that may help.

These two commands are equivalent:
head -5 file
head -n 5 file

So if you use -n $file_cnt instead of -$file_cnt the extra space would have no effect.