problem with redirect stdout to file

Hi all hope you can help as I am going MAD!!! :eek:

The below is in a shell script but the redirection in the sed line does not work and outputs to the screen and the $fname_2 does note get created ?????

Can any one help ??

#!/bin/ksh
cd /app/
for fname in `ls -1 X*`
do
sed 1d $fname > $fname_2
done

if I run

sed 1d filename > filename_2

on the command line it works ???

We ran into the same problem the other day in one of our scripts.

You need to change your $fname to ${fname}

Look at this script to see the behaviour.

[/tmp]$ cat try.sh
#! /bin/sh

a=12
b=34
echo $a_$b
echo ${a}_$b
[/tmp]$ sh try.sh
34
12_34
[tmp]$ 

From man sh

       ${parameter}
              The value of parameter is substituted.  The braces  are  required
              when  parameter  is  a  positional  parameter  with more than one
              digit, or when parameter is followed by a character which is  not
              to be interpreted as part of its name.

Hi thanks that did the trick.

Do you know why this is the case ?? I have used the $fname in loads of scripts before with out having to use the {}

Yes.
$fname_2 expects that fname_2 is the name of the variable - and you want the _2 to be part of the name not part of a new variable name.

You can also use $fname"_2" to force the _2 to be a constant, and not a variable.