user527414
user527414

Reputation: 23

KSH for loop works on Solaris/Mac but not on Red Hat Linux

The following Ksh script gives me "No such file or directory" error message on Red Hat Linux system. Does anyone has a solution?

#!/usr/bin/ksh
for f in `cat files.dat`
do
  wc $f
done

For example, files.dat has 3 lines of data and each line is a file in the current directory where the script is running from.

a.c
a.h
b.c

Note, the same for loop generated the same error message if running from command line too.

It works on Solaris/Mac box but not on Red Hat system.

Thanks.

Upvotes: 2

Views: 685

Answers (2)

user502515
user502515

Reputation: 4444

You should properly quote your arguments, in other words, use "$f", not $f. About cat - it's mostly documented in here: http://porkmail.org/era/unix/award.html

What is probably better suited is xargs -a thatfile wc.

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360113

Instead of for ... cat, you should use

while read -r f
do
    wc "$f"
done < files.dat

And you should use $() instead of backticks when you do need to do command substitution.

But your problem is probably that the files a.c, etc., are not there, have different names, invisible characters in their names, or the line endings in files.dat are CR/LF (DOS/Windows-style) instead of \n (LF only - Unix-style) or there are odd characters in the file otherwise.

Upvotes: 1

Related Questions