What is wrong with this simple script?

The script below is supposed to list file and folder names and list the type besides each - It has no practical value - I am just trying to learn KSH (so far sounds like a bad idea!)

Expected output should look like this:
folder 1 *** dir ***
file1 * file *

The code runs but produces strange wrong results.

I am using Microsoft SFU.

I would like to know what is wrog?

Thanks much.

#!/usr/bin/ksh
f=""
typ1=""
for f in `ls`
do
    if [ -d "$f" ]
    then 
        typ1=" *** dir ***"
    elif [ -f "$f" ]
    then
        typ1=" * file *"
    else
        typ1=" Not sure?"
    fi
    print $f 
    print $typ1
done

Change:
print $f
print $typ1

to be:
print "$f $typ1"

What are the 'strange results'?

Hi,
Thanks for your reply

  1. Yes, I should include both variables in 1 print line.
  2. I get the file names correct - The content of typ1 is I guess the one that is causing the problem...
  3. The output seems to be a dump of all file names I have in the directory with every print of typ1, for example I get something like this:

"
l tee tee.exe.stackdump test2 tmp.3736 winrar.exe winwordcontrol_demo winwordcontrol_demo.zip word
net x x.txt x2 xml xml2 xs xx1 z z.$$$ z.sh z.txt z1.txt
Not sure?
2 ADO.NET The Complete Reference AGG.TXT Business Analysis.txt OCOUNTER_RTN.doc Counter_RTN.
Not sure?
"

Tahnks.

GMMike,

Please scroll up a post or two to Perderabo's response - your solution awaits you there!

Cheers
ZB

I tried the script and it stated everything was a file, even though there are directories. Plus, I had already changed the print statement before seeing Perderabo's reply. Not exactly sure what GMMike is getting but that's why my question was asked! :slight_smile:

I gave it a whirl after making the edit (well, I replaced the print's with a single echo) and it works as expected under "pdksh" (Linux) - Haven't tried it under SFU as I removed it from my Windows XP machine a while back to make way for Cygwin :wink:

I was using ksh in Solaris (2.6 or 8) - same logic in csh worked fine in giving file, directory or other.

Hi Guys,

This seams to have solved the problem:

print "$f" "$typ1"

It was a quotation problem...

Thanks for your help :slight_smile:

try

print *

in the current directory ...

You will understand the reason for the weird results.

as Perderabo suggested enclose those variable $f and $typ1 in double quotes.

as

print "$f $typ1"