bash unix shell 脚本中的 cat 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10608625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cat command in unix shell script
提问by Win Man
I have 2 lines of code
我有两行代码
1) With the following code:
1) 使用以下代码:
for i in `ls *.properties`; do cat $i; done
I get the error:
我收到错误:
cat: file_name.properties: No such file or directory.
2) On the other hand:
2)另一方面:
for i in *.properties; do cat $i; done
Works fine!
工作正常!
I thought both were the same. Can someone help me understand the difference between the two? Using bash shell.
我以为两者是一样的。有人可以帮助我理解两者之间的区别吗?使用 bash 外壳。
回答by nosid
What does the following command print?
以下命令打印什么?
cat -v <<< `ls *.properties`
I guess the problem is, that lsis a strange alias, e.g. something like
我想问题是,这ls是一个奇怪的别名,例如像
ls='ls --color'
Edit:I have seen this before. The alias should be: alias ls='ls --color=auto'
编辑:我以前见过这个。别名应该是:alias ls='ls --color=auto'
回答by A.H.
Most probably there is a directory which matches *.properties. Then lswill output the files in this directory without the directory name. Then the catwill not find the given filename.
很可能有一个目录匹配*.properties. 然后ls将输出该目录中的文件,而没有目录名。然后cat将找不到给定的文件名。
So please check, whether file_name.propertiesis in the actual directory or in some subdirectory.
所以请检查,file_name.properties是在实际目录中还是在某个子目录中。
EDIT
编辑
To reproduce the problem you can try this:
要重现该问题,您可以尝试以下操作:
# cd /tmp
# mkdir foo.properties
# touch foo.properties/file_name.properties
# for i in `ls *.properties`; do cat $i; done
cat: file_name.properties: No such file or directory

