![解释 shell 脚本代码“ for 循环”](https://linux22.com/image/1477560/%E8%A7%A3%E9%87%8A%20shell%20%E8%84%9A%E6%9C%AC%E4%BB%A3%E7%A0%81%E2%80%9C%20for%20%E5%BE%AA%E7%8E%AF%E2%80%9D.png)
有人能为我解释一下这个循环的用途是什么,或者它是如何工作的吗?
for file in `ls *SC*`; do
for content in `cat $file `; do
scid=`echo $file|awk -F'.' '{print $1}'`
printf "%-10s%s\n" $scid $content
done
done > text1
答案1
奇怪的代码,如果文件名中有空格将会失败,并用空格分割文件。
for file in `ls *SC*`; do # parses ls for files with SC in the name
# - not the best way to do it (don't parse ls, fails if spaces in filename)
for content in `cat $file `; do # copies file to $content, SEPARATED BY SPACES
scid=`echo $file|awk -F'.' '{print $1}'` # cuts filename to first .(?)
printf "%-10s%s\n" $scid $content # prints cut filename + content
done
done > text1