我的代码出错了

时间:2013-10-08 13:55:33

标签: bash awk

#!/bin/bash

# When a match is not found, just present nothing.
shopt -s nullglob

files=(*.wav)

if [[ ${#files[@]} -eq 0 ]]; then
echo "No match found."
fi

for file in "${files[@]}"; do
# We get the date part
find_date=$(stat -c %y $file | awk '{print $1}')`
for t in "${parts[@]}"; do
IFS="-." read -ra parts <<< "$file"
  if [[ $t == [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] ]]; then
        file_date=$t
        break
    fi
done
 # If a value was not assigned, then show an error message and continue to the next     file.
# Just making sure there is nothing in Array and date before it moves on
if [[ -z $file_date ]]; then

    continue
fi

file_year=${file_date:0:4}
file_month=${file_date:6:2}
mkdir -p "$file_year/$file_month"

# -- is just there to not interpret filenames starting with - as options.

echo "Moving: ./"$file "to: " "./"$file_year"/"$file_month
mv  "$file" "$file_year/$file_month"

done

我有一些文件是.wav ....我想把文件放在一个像我做的数组然后Stat -c%y filename | awk $ 1给我YYYY-MM-DD然后我想要把日期放在数组中,然后我可以设置2个变量年和月,这样我就可以制作一个DIR年/月,或者如果DIR已经存在那么只需要它。这是mkdir -p ...在我的代码中获取错误,但我不认为我正在读取我的数组中的文件。

25:继续:仅在for', while'或'until'循环中有意义 我的回声声明移动:./ OUT117-20092025-5845.wav:.//

1 个答案:

答案 0 :(得分:0)

除了一些语法问题之外,主要问题是你不能在{for循环中使用continue

语法错误是:

  • 您不能在作业运算符=的任何一侧留出空格,因此find_date= stat -c %y $file | awk {print $ 1} ` should be find_date = $(stat -c%y $ file | awk'{print $ 1 }')'
  • 正则表达式运算符为=~而不是==

更新:在设置变量之前,您正在开始for循环。

for t in "${parts[@]}"; do
IFS="-." read -ra parts <<< "$file"

应该是:

IFS="-." read -ra parts <<< "$file"
for t in "${parts[@]}"; do