Link to home
Start Free TrialLog in
Avatar of ashaarvind
ashaarvind

asked on

Korn Shell script problem

I have a strange problem with a Korn shell script running on Digital UNIX v5.1:

for LINE in $(</home/sfdm/arvindk/sfcbom2.txt)
do
echo ${LINE}
done

The file I am reading has:

/home/sfdm/arvindk> cat sfcbom2.txt
[22222B]

If I run this I expect to see [22222B] in output but I see only 2:

/home/sfdm/arvindk> script1
2

On the shell prompt, some numbers work fine but some dont (using \ to escape does work for 32 and 31) -

/home/sfdm/arvindk> echo [44]
[44]
/home/sfdm/arvindk> echo [32]
2
/home/sfdm/arvindk> echo [31]
1

Any idea why this is happening?

Thanks
Avatar of bira
bira

for LINE in  `cat/home/sfdm/arvindk/sfcbom2.txt`
do
echo $LINE
done
for LINE in  `cat /home/sfdm/arvindk/sfcbom2.txt`
do
echo $LINE
done
ASKER CERTIFIED SOLUTION
Avatar of ecw
ecw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use /usr/bin/echo or wherever you have a echo program instead of the shells built-in echo
also quote the variable (as in ecw's suggestion)
Avatar of ashaarvind

ASKER

The comment from ecw had the answer. There was a file called '2'in the same directory. It fixed my problem - but I am still puzzled on why this happened?

Thanks/Arvind
It happened because [ and ] are used by the shell for matching a specfic set of chars during wildcard expansion.

For example
 [Aa]* will match all files beginning with an upper or lower case A
 [A-Z]* will match all files beginning with an upper case char
 *.[aeiou]* will match all files with a . followed by a vowel

The sense of the match is negated if the first char after [ is a ! eg.
 *.[!16-9] will match all files that don't end in .1 .6 .7 .8 or .9
Note the use of - to specify a range.

So in your example,
 echo ${LINE}
expands to
 echo [22222B]
Which is then subject to filename expansion, capable of matching a 'single' character filename of either 2 or B

You just happened to have a file called 2, so it matched that.  If you'd also happened to have a file called B, the output would've been
 2 B
Sounds like a pencil top me.