【问题标题】:How to write a bash script to store files from a directory to an output file?如何编写 bash 脚本将文件从目录存储到输出文件?
【发布时间】:2014-11-03 13:53:58
【问题描述】:

我正在尝试编写一个 bash 脚本来读取两个参数...一个目录或一个文件和一个输出文件。我需要比较两者以确保它们不同。如果第一个参数是一个目录,那么我需要将该目录中的文件存储到输出文件中。如果第一个参数是一个文件,那么我需要将该文件存储在输出文件中。此外,第一个参数可以包含中间的空格以及该目录中的任何文件。以下是我到目前为止所拥有的。我能够打印出目录中的所有文件,但是,我遇到了空白以及将目录中的文件或文件本身存储到输出文件中的问题。我该怎么做?

    #!/bin/bash

    INPUT="$1"
    OUTPUT="$2"

    # Check that there are two arguments
    if [ "$#" -ne 2]
    then
       echo "Usage: $0 {dir-name}"
       exit 1
    fi

    # Check that INPUT is different from OUTPUT
    if [ "$INPUT" = "$OUTPUT" ]
    then
       echo "$INPUT must be different from $OUTPUT!"
       exit 1
    fi

    # Check if INPUT is a directory
    if [ -d "$INPUT" ]
    then
       # Store all files from directory into OUTPUT file
       echo "$INPUT directory exists!"
       for name in `ls "$INPUT"`; do
          echo "File -> $name"
          mv "$name" "$OUTPUT"
       done
    fi

    # Check if INPUT is a file
    if [ -f "$INPUT" ]
    then
       # Move INPUT  into OUTPUT file
       echo "$INPUT file exists!" 
       mv "$INPUT" "$OUTPUT"
       exit 1
    else
       echo "$INPUT is not a file!"
       exit 1
    fi

【问题讨论】:

  • Don't parse ls,使用全局...
  • 看来你正在破坏你的$OUTPUT,将每个文件移动到它上面。也许看看cat>>
  • echo "$INPUT is not a file!" 是一个目录时,您似乎也会点击它。也许exit 来自if[ -d "$INPUT" ] 或将文件检查嵌套在else 中,然后您可以说“$INPUT 既不是常规文件也不是目录”。
  • 您能否提供一个您所面临的空白问题的示例,例如您传递给脚本的参数类型?

标签: bash


【解决方案1】:

下面的脚本是否解决了这个问题,我已经用 mv 命令替换了 for 循环,这样您就不必担心目录中文件之间的空白..

 #!/bin/bash

    INPUT="$1"
    OUTPUT="$2"

    # Check that there are two arguments
    if [ "$#" -ne 2 ]
    then
       echo "Usage: $0 {dir-name}"
       exit 1
    fi

    # Check that INPUT is different from OUTPUT
    if [ "$INPUT" = "$OUTPUT" ]
    then
       echo "$INPUT must be different from $OUTPUT!"
       exit 1
    fi

    # Check if INPUT is a directory
    if [ -d "$INPUT" ]
    then
       # Store all files from directory into OUTPUT file
       echo "$INPUT directory exists!"
       # The below command will solve the problem of file containing 
       # white space within the directory
       mv "$INPUT"/* "$OUTPUT"
    elif [ -f "$INPUT" ]
    then
       mv "$INPUT" "$OUTPUT"
    else
       echo "$INPUT is not a file!"
    fi

【讨论】:

  • 不,这对我正在尝试做的事情不起作用......谢谢你的尝试。
【解决方案2】:

试试:

for name in "{$INPUT}"/*; do
      echo "File -> ${name}"
      cat "${name}" >> "${OUTPUT}"
done

这假设 polarysekt 的 cat 想法适合您。否则,您需要操纵 mv 或其他一些命令来获取您正在寻找的输出。

至于在命令行参数(第一个参数或第二个参数)中有一个空格,我看不出bash 如何区分第一个参数和第二个参数之间的区别,应该是空格被包括在内,除非参数被引用。在这种情况下,再次尝试"{$1}""{$2}"

【讨论】:

    【解决方案3】:

    我想通了。这对我有用。

        #!/bin/bash
    
        INPUT="$1"
        OUTPUT="$2"
    
        # Check that there are two arguments
        if [ "$#" -ne 2 ]
        then
           echo "Usage: $0 {dir-name}";
           exit 1
        fi
    
        # Check that INPUT is different from OUTPUT
        if [ "$INPUT" = "$OUTPUT" ]
        then
           echo "$INPUT must be different from $OUTPUT!";
           exit 1
        fi
    
        # Check if INPUT is a directory
        if [ -d "$INPUT" ]
        then
           echo "$INPUT directory exists!";
           find $name -type f  > "$OUTPUT"
        # Check if INPUT is a file
        elif [ -f "$INPUT" ]
        then
           # Move INPUT  into OUTPUT file
           echo "$INPUT file exists!";
           find "$INPUT" > "$OUTPUT"
           exit 1
        else
           echo "$INPUT is not a file!";
           exit 1
        fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多