Computing.Net > Forums > Unix > Unix - Ksh

Unix - Ksh

Reply to Message Icon

Original Message
Name: Apple
Date: March 19, 2005 at 05:02:22 Pacific
Subject: Unix - Ksh
OS: ?
CPU/Ram: ?
Comment:

Dear All,
After I exec the script, how come does not show - Processing $fname.... I try to debug, I found the script stop at ->
if test -f $fname ;

Would anyone help me what's wrong I have?
Thank you.

while true
do
for fname in $(ls /home/file/)
do
if test -f $fname ; then
echo "Processing $fname..."
fi
done
sleep 60
done



Report Offensive Message For Removal


Response Number 1
Name: WilliamRobertson
Date: March 19, 2005 at 15:26:57 Pacific
Reply: (edit)

Can't see anything wrong with it. Works for me.

Not that it's any help, but isn't this:

for fname in $(ls /home/file/)

the same as this:

for fname in /home/file/*


Report Offensive Follow Up For Removal

Response Number 2
Name: gurubit
Date: March 21, 2005 at 06:27:15 Pacific
Reply: (edit)

It works fine for me as well.
Try removing the semi colon. That might be stopping the whole thing there.

Use like this.

#!/bin/ksh
while true
do
for fname in $(ls /home/pandera/)
do
if test -f $fname
then
echo "Processing $fname..."
fi
done
sleep 6
done


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim Boothe
Date: March 21, 2005 at 06:47:33 Pacific
Reply: (edit)

The ls is returning simple file names, not fullpath names, so you need to change the if-statement to:

if test -f /home/file/$fname ; then


Report Offensive Follow Up For Removal

Response Number 4
Name: Apple
Date: March 21, 2005 at 07:59:32 Pacific
Reply: (edit)

Dear All,

Thank you for yours help.

When I run the script, I find that the error shows : "test" command not found.
If my OS-Unix may not have the command - "test", what's other command I can use?

I have another question?

inputfile name : infile
contain: sky moon sun star

while read
do
$1=cat infile | cut -c1-3
$2=cat infile | cut -c5-4
$3=cat infile | cut -c10-3
$4=cat infile | cut -14-4
done < infile

How can I get each field in the input line from a text file?

Thank you.
Apple



Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: March 21, 2005 at 08:33:00 Pacific
Reply: (edit)

Hi:

I'm not certain what you're doing with that while loop, but if you have a file with a known number of columns, you can do this:

#!/bin/ksh

while read col1 col2 col3 col4
do
echo $col1
.
.
done < infile

If you don't, you can always use cut, but I prefer to use the set command which sets the command line arguments $1, $2 ..:

#!/bin/ksh

while read line
do
set - $(echo $line)
echo $1 # sky
echo $2 # moon
.
.
done < infile



Report Offensive Follow Up For Removal


Response Number 6
Name: WilliamRobertson
Date: March 21, 2005 at 16:21:29 Pacific
Reply: (edit)

In ksh and others,

[[ -f filename ]]

is equivalent to

test -f filename

btw, why "cat infile | cut -c1-3"? Seems odd when cut can take a filename argument.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Unix - Ksh

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge