简单的 Bash 脚本文件复制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15077483/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:22:33  来源:igfitidea点击:

Simple Bash Script File Copy

bash

提问by lbrendanl

I am having trouble with a simple grading script I am writing. I have a directory called HW5 containing a folder for each student in the class. From my current directory, which contains the HW5 folder, I would like to copy all files starting with the word mondial, to each of the students' folders. My script runs but does not copy any of the files over. Any suggestions?

我正在编写一个简单的评分脚本时遇到问题。我有一个名为 HW5 的目录,其中包含班级中每个学生的文件夹。从包含 HW5 文件夹的当前目录中,我想将所有以单词 mondial 开头的文件复制到每个学生的文件夹中。我的脚本运行但不复制任何文件。有什么建议?

#!/bin/bash                                                                                                         

for file in ./HW5; do
    if [ -d $file ]; then
        cp ./mondial.* ./$file;
    fi
done

Thanks,

谢谢,

回答by piokuc

The first loop was executing only once, with fileequal ./HW5. Add the star to actually select the files or directories inside it.

第一个循环只执行一次,file等于./HW5. 添加星号以实际选择其中的文件或目录。

#!/bin/bash                                                                                                         

for file in ./HW5/*; do
  if [ -d "$file" ]; then
    cp ./mondial.* ./"$file"
  fi
done

As suggested by Mark Reed, this can be simplified:

正如 Mark Reed 所建议的,这可以简化:

for file in ./HW5/*/; do 
  cp ./mondial.* ./"$file"
done