【问题标题】:How to develop this sort script further如何进一步开发这种排序脚本
【发布时间】:2016-11-17 17:45:20
【问题描述】:

我有一个小的交互式 unix 脚本,它为所选文件类型接受终端命令输入,并在收到此类迭代时遍历我桌面上未排序文件的大文件夹,并将所选选择的文件拉到新的排序文件夹中。

即用户在命令中键入 jpg ,所有 jpg 文件都被拉出未排序的文件夹并进入已排序的文件夹。

就目前而言,它工作得非常好,但我想进一步开发我的脚本,以便我可以将 jpg 文件推送到专用文件夹中,而不是将所有文件类型推送到公共排序文件夹中 jpg、png 文件推送folderpng,最后将所有 docx 文件移入 docxfolder。

假设这些文件类型的专用文件夹已在我的桌面上创建,我该如何以最精简的方式实现这一目标。

#!/bin/bash

echo "Good Morning, Please enter your file type name for sorting [ENTER]:"
read extension
mv  -v /Users/christopherdorman/desktop/unsorted/*.${extension} /Users/christopherdorman/desktop/sorted/
if [[ $? -eq 0 ]]; then
    echo "Good News, Your files have been successfully processed"
fi

【问题讨论】:

  • 不使用"folder${extension}/"?
  • 我是这门语言的新手。这会是一个解决方案。我是否不必修改读取命令。如果可能,请在上面更新代码

标签: shell unix terminal


【解决方案1】:

我会这样写:

read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" extension
if cd /Users/christopherdorman/desktop; then
    destination="folder$extension"
    # ensure the destination folder exists
    mkdir -p "$destination"
    if mv  -v unsorted/*."$extension" "$destination"; then
        echo "Good News, Your files have been successfully processed"
    fi
fi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多