编辑

编辑

我有以下 bash 脚本,它迭代目录中的所有图像,并尝试使用 exif 确定创建它的日期和时间。如果 exif 信息不可用,它会询问用户是否应该使用图像的上次修改日期。如果用户拒绝,则应跳过该图像:

for file in *.jpg; do
    if datetime="$(exif --debug --machine-readable --tag=DateTimeOriginal "$file" 2>&1)"; then
        datetime="${datetime##*$'\n'}"
    else
        datetime="$(stat --format=%y "$file")"
        datetime="${datetime%\.*}"

        echo "Warning: $file does not contain tag DateTimeOriginal"
        echo "Last modified date is $datetime - use it instead?"

        select yn in "Yes" "No"; do
            case "$yn" in
                Yes)
                    echo "Info: using last modified date for $file"
                    break
                    ;;
                No)
                    echo "Info: $file skipped - not using last modified date"
                    break
                    continue
                    ;;
            esac
        done
    fi

    echo "Renaming $file..."
done

问题是即使用户拒绝,仍然到达最后一行(即“重命名 $file...”被打印到标准输出)。我认为这是因为break阻止continue了到达。我尝试翻转这两个语句,但这会导致无限循环:

                No)
                    echo "Info: $file skipped - not using last modified date"
                    continue
                    break
                    ;;

希望我想做什么很清楚。

编辑

我发现了一些可行的方法,但请告诉我是否有更好的方法:

for file in *.jpg; do
    answer=

    if datetime="$(exif --debug --machine-readable --tag=DateTimeOriginal "$file" 2>&1)"; then
        datetime="${datetime##*$'\n'}"
    else
        datetime="$(stat --format=%y "$file")"
        datetime="${datetime%\.*}"

        echo "Warning: $file does not contain tag DateTimeOriginal"
        echo "Last modified date is $datetime - use it instead?"

        option1="Use last modified date"
        option2="Skip file"

        select answer in "$option1" "$option2"; do
            case "$answer" in
                "$option1"|"$option2")
                    break
                    ;;
            esac
        done

        if [ "$answer" = "$option2" ]; then
            echo "Info: $file skipped - not using last modified date"
            continue
        fi
    fi

    echo "Renaming $file..."
done

答案1

编辑: 你可以试试这个:

for file in *.jpg; do
    if datetime="$(exif --debug --machine-readable  --tag=DateTimeOriginal "$file" 2>&1)"; then
        datetime="${datetime##*$'\n'}"
    else
        datetime="$(stat --format=%y "$file")"
        datetime="${datetime%\.*}"

        echo "Warning: $file does not contain tag DateTimeOriginal"
        echo "Last modified date is $datetime - use it instead?"

        select yn in "Yes" "No"; do
            case "$yn" in
                Yes)
                    echo "Info: using last modified date for $file"
                    break
                    ;;
                No)
                    echo "Info: $file skipped - not using last modified date"
                    break && continue
                    ;;
            esac
        done

    fi
   echo "Renaming $file..."
done

相关内容