KSH for 循环适用于 Solaris/Mac,但不适用于 Red Hat Linux

发布于 2024-10-06 02:12:07 字数 349 浏览 0 评论 0原文

以下 Ksh 脚本在 Red Hat Linux 系统上给出了“没有这样的文件或目录”错误消息。有人有解决办法吗?

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

例如,files.dat 有 3 行数据,每行都是运行脚本的当前目录中的一个文件。

a.c
a.h
b.c

请注意,如果从命令行运行,相同的 for 循环也会生成相同的错误消息。

它适用于 Solaris/Mac 机器,但不适用于 Red Hat 系统。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬﹪忆之殇 2024-10-13 02:12:07

而不是 for ... cat

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

当您确实需要进行命令替换时,您应该使用 $() 而不是反引号,

。但您的问题可能是文件 ac 等不存在、具有不同的名称、名称中的不可见字符或 files.dat 中的行结尾是CR/LF(DOS/Windows 风格)而不是 \n (仅限 LF - Unix 风格),否则文件中存在奇数字符。

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.

送你一个梦 2024-10-13 02:12:07

您应该正确引用您的论点,换句话说,使用 "$f",而不是 $f
关于 cat - 它主要记录在此处:http://porkmail.org/era/unix/award。 html

可能更适合的是 xargs -a thatfile wc。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文