KSH for 循环适用于 Solaris/Mac,但不适用于 Red Hat Linux
以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是
for ... cat
当您确实需要进行命令替换时,您应该使用
$()
而不是反引号,。但您的问题可能是文件
ac
等不存在、具有不同的名称、名称中的不可见字符或files.dat
中的行结尾是CR/LF(DOS/Windows 风格)而不是\n
(仅限 LF - Unix 风格),否则文件中存在奇数字符。Instead of
for ... cat
, you should useAnd 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 infiles.dat
are CR/LF (DOS/Windows-style) instead of\n
(LF only - Unix-style) or there are odd characters in the file otherwise.您应该正确引用您的论点,换句话说,使用
"$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
.