解释 shell 脚本代码“ for 循环”

解释 shell 脚本代码“ for 循环”

有人能为我解释一下这个循环的用途是什么,或者它是如何工作的吗?

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

相关内容