开发者

A confusing error of executing commands in foreach in Csh

The program is very simple:

#!/bin/csh -f
foreach path ( fileA.txt fileB.txt )
    wc -l $path
    grep "test" $path
end

However, the output is:

fileA.txt/wc: Not a directory.
fileA.txt/grep: Not a directory.
fileB.txt/wc: Not a directory.
fileB.txt/grep: Not a directory.

So what's wrong with the 开发者_StackOverflow社区code and what's the correct way of doing it?


You should never use path as a generic variable name in C-Shell since it contains the current search directories for the shell to find the command programs.

This will work much better than your code:

#!/bin/csh -f
foreach mypath ( fileA.txt fileB.txt )
    wc -l $mypath
    grep "test" $mypath
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜