0

这就是我现在所拥有的

#!/bin/bash

# This would match files that begin with YYYYMMDD format.
files=([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*)

# If you want to match those in the current year, start it with that year instead.
# current_year=$(date +%Y)
# files=("$current_year"[0-9][0-9][0-9][0-9]*)

#expands its values to multiple arguments "${files[@]}"
for file in "${files[@]}"; do
 file_year=${file:0:4}

#  Adding -p option to mkdir would create the directory only if it doesn't exist.
  mkdir -p "$file_year"  

  file_month=${file:4:2}
  mkdir -p "$file_month”
  mv "$files" "$file_year"/"$file_month"

done

出现错误 第 19 行:在寻找匹配的 `"' 时出现意外 EOF 第 22 行:语法错误:文件意外结束

4

2 回答 2

1

这是满足您要求的概念脚本。我希望它有所帮助。

#!/bin/bash

# This would match files that begin with YYYYMMDD format.
files=([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*)

# If you want to match those in the current year, start it with that year instead.
# current_year=$(date +%Y)
# files=("$current_year"[0-9][0-9][0-9][0-9]*)

for file in "${files[@]}"; do
    file_year=${file:0:4}

    # You could just simply do this. Adding -p option to mkdir would create the directory only if it doesn't exist.
    # mkdir -p "$file_year"  

    if [[ -d $file_year ]]; then
        echo "Directory exists."
    else
        echo "Creating directory ${file_year}."
        mkdir "$file_year" || {
            # If it fails, send a message to user and break the loop.
            echo "Failed to create directory ${file_year}."
            break
        }
    fi

    # After creating the directory perhaps you want to move the file to it so:
    # mv "$file" "$file_year"
done
于 2013-09-13T11:34:26.160 回答
0
于 2013-09-13T15:22:14.937 回答