Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

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
0
ashaarvind
Asked:
ashaarvind
1 Solution
 
biraCommented:
for LINE in  `cat/home/sfdm/arvindk/sfcbom2.txt`
do
echo $LINE
done
0
 
biraCommented:
for LINE in  `cat /home/sfdm/arvindk/sfcbom2.txt`
do
echo $LINE
done
0
 
ecwCommented:
Have you got a file called 2 in the current dir?
use,
  for LINE in $(</home/sfdm/arvindk/sfcbom2.txt)
  do
    echo "${LINE}"
  done

not the double quotes around the var, this is to stop the shell using [] as special chars.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ahoffmannCommented:
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)
0
 
ashaarvindAuthor Commented:
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
0
 
ecwCommented:
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.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now