简体   繁体   English

UNIX Shell脚本读取文件,复制文件和删除文件

[英]UNIX shell script to read files, copy files and delete files

Hi I am writing a shell script in UNIX which reads a directory and copies the files in to another directory and then deletes the files in the first directory 嗨,我正在UNIX中编写一个Shell脚本,该脚本读取目录并将文件复制到另一个目录,然后删除第一个目录中的文件

I have tried this but it does not work:- 我已经尝试过了,但是没有用:

files"=/project/scripts/input/"

for f in $files; do
    [ mv /project/scripts/input/$f.txt /project/scripts/output/$f.xml

    rm /project/scripts/input/$f.txt ]
else

    echo "Nothing to process will try later"

    exit

fi

It could be something like 可能是这样的

cd $SourceDir && ( tar cfz - * | ( cd $TargetDir && tar xfz - ) )

(written from head/not tested...) (从头写/未测试...)

Because the structure of the script is so badly broken, it is quite hard to determine what the real intent of the script is. 由于脚本的结构被严重破坏,因此很难确定脚本的真正意图。 So with a bit of guesswork, I'm going to give a psuedo-answer with some hints: 因此,通过一些猜测,我将给出一些提示的伪答案:

  • files"=/project/scripts/input/" - I assume the intent of this line is to set the variable files to all the files in the /project/scripts/input/ directory. files"=/project/scripts/input/" -我假设这一行的目的是将变量files设置为/project/scripts/input/目录中的所有文件。 But this line won't do that. 但是这条线不会那样做。 Instead, because the "=/project/scripts/input/" part is in double quotes, it is expanded literally with no special meaning (variable assignment) given to the = . 相反,由于"=/project/scripts/input/"部分用双引号引起来,因此它在字面上被扩展,而没有给=赋予特殊含义(变量赋值)。 So the whole line ends up being an attempt to execute files=/project/scripts/input/ , ie execute the directory input/ which is found under the relative path files=/project/scripts/ . 因此,整行最终都是尝试执行files=/project/scripts/input/ ,即执行目录input/ ,该目录位于相对路径files=/project/scripts/ A variable $files will not be set at all. 根本不会设置变量$files
  • As I think you already know the else and the fi must be proceeded somewhere with some kind of if statement. 我认为您已经知道elsefi必须以某种if语句进行处理。 Your question will make much more sense if you edit it to include the actual if statement (edit the answer itself, don't put in a comment) 如果您将问题编辑为包括实际的if语句,那么问题将变得更加有意义(编辑答案本身,请勿添加评论)
  • for ...; do for ...; do statements must always be followed by a done statement to indicate the end of your loop for ...; do语句后必须始终有一个done语句,以指示循环结束
  • [ and ] are always used specifically to test conditionals. []始终专门用于测试条件。 See http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html . 参见http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html The condition which is being tested will be within the [ and ] and then some conditional action will happen, depending on the outcome of the conditional. 要测试的条件将在[] ,然后将根据条件的结果发生一些条件操作。
  • as mentioned in my comment, mv will automatically remove the source files, so there is no need for the rm . 如我的评论所述, mv将自动删除源文件,因此不需要rm

In short you would do well to edit your question to provide the full script you are asking about, and what exactly you expect it to do. 简而言之,您最好编辑问题以提供所要询问的完整脚本,以及您希望它执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM