开发者

A confusing error of executing commands in foreach in Csh

开发者 https://www.devze.com 2023-04-12 23:35 出处:网络
The program is very simple: #!/bin/csh -f foreach path ( fileA.txt fileB.txt ) wc -l $path grep \"test\" $path

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

精彩评论

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

关注公众号