bash:for循环检索目录中的所有文件只返回一个

时间:2015-11-22 10:02:10

标签: bash for-loop

我创建了一个包含大量文件的目录:

samplefile_111222015_reporting_{1..13}

我正在尝试创建一个vi脚本,当我输入目录作为命令的参数时,例如

sh myScript $HOME/theDir/*

然后它将该目录中的所有文件复制到我制作的新文件中。虽然现在,我只有for循环问题。 这就是我在剧本中的内容:

for f in $1;
do
echo "$f"
done

但是当我输入sh myScript $ HOME / theDir时,我只返回第一个文件的名称(samplefile_111222015_reporting_1)。为什么第一个文件?这不是for循环>

2 个答案:

答案 0 :(得分:2)

# Because of the wild card expansion, all the files in the directory are  
# already made available to the script  through arguments  
# So do the following to get all the file listing  
for f ; do echo $f; done

答案 1 :(得分:0)

这是因为每个文件都作为一个单独的参数传递,而你只是循环遍历var computer = new Product({ title: 'Computer', price: 1000, attributes:{ year:2000, cpu:'test', power:'75 RMS' // Other Attributes goes here. } }); var shirt = new Product({ title: 'T-Shirt', price: 999, attributes:{ size:30, color:'black' // Other Attributes goes here. } }); ,这是第一个参数。

相反,您很可能想要遍历$1,这是从"$@"开始的每个参数。

bash的手册页,在$1部分下,详细介绍了可用的特殊参数。