如何编写bash脚本来将文件从目录存储到输出文件?

时间:2014-09-10 02:47:13

标签: bash

我正在尝试编写一个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

3 个答案:

答案 0 :(得分:0)

下面的脚本是否解决了这个问题,我已经用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

答案 1 :(得分:0)

尝试:

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

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

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

答案 2 :(得分:0)

我明白了。这对我有用。

    #!/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