What is wrong with this simple script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers What is wrong with this simple script?
# 1  
Old 01-24-2005
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.

Code:
#!/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

# 2  
Old 01-24-2005
Change:
print $f
print $typ1

to be:
print "$f $typ1"
# 3  
Old 01-24-2005
What are the 'strange results'?
# 4  
Old 01-24-2005
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.
# 5  
Old 01-24-2005
GMMike,

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

Cheers
ZB
# 6  
Old 01-24-2005
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! Smilie

Last edited by RTM; 01-24-2005 at 07:07 PM..
# 7  
Old 01-24-2005
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 Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

2. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

3. Shell Programming and Scripting

I have no idea what is wrong with my simple script.

I am doing a simple "recycle bin" script. It is to check and see if the directory .TRASH exists within the home directory. If not then it makes the directory then appends a date and time to file and then finally moves the file in. However, when I run this script, it is not making the directory as... (5 Replies)
Discussion started by: iamdeman
5 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Shell Programming and Scripting

What is wrong with this script

Good day all I have a UNIX script that I created to set a couple of environment variables for my Oracle installation. When I initially run the script, I get the correct values on the inbuilt echo command but when the script completes and I issue an echo statement echo $ORACLE_SID, the... (9 Replies)
Discussion started by: tt1611
9 Replies

6. Shell Programming and Scripting

What is wrong with this script?

I write this script and do not no whrer the wrong!! Can anyone hlep me? #========================================= Script Name: shell_script1 #========================================= looptrack=y while do echo -n “Type in the account number:” read account echo -n “Type the first and... (3 Replies)
Discussion started by: Oman_Member
3 Replies

7. Shell Programming and Scripting

ps script - What is wrong with it please?

Hi All, I am trying to do a script that prompts the user for the PID and it displays information about that process, this is what I have, but is not working: echo "Please enter your PID:" read pid case $pid in ps | grep $pid esac done * I also needed that this... (1 Reply)
Discussion started by: linuxrose
1 Replies

8. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

9. UNIX for Dummies Questions & Answers

What's wrong with this simple program in APUE?

I start wetting my toes in Linux programming. I tried the first program myls.c in Advanced Programming in the Unix Environment. #include <sys/types.h> #include <dirent.h> #include "apue.h" int main(int argc, char *argv) { DIR *dp; struct... (1 Reply)
Discussion started by: cqlouis
1 Replies

10. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies
Login or Register to Ask a Question