【问题标题】:batch processing files through an interactive script通过交互式脚本批处理文件
【发布时间】:2016-11-26 14:15:02
【问题描述】:

我在Mac 上运行了一个正常运行的交互式脚本,它对位于我桌面上未排序文件夹中的文件进行排序和处理。

按照目前的情况,用户在命令行中键入jpg,脚本会执行并遍历未排序的文件夹,它会在其中获取这些文件类型,并在桌面上创建一个新目录并移动它们。

它工作得很好,但我想进一步开发脚本,以便我可以批处理,而不必一次输入一个终端命令。

即我可以在终端jpggifdocx中输入一系列参数,然后脚本将运行并为jpggifgifdocx创建新的桌面目录,并将所有这些文件类型移动到这样的位置。

唯一需要注意的是,未排序文件夹中的剩余杂项文件(.wav png 和一连串其他扩展名)需要在桌面中创建一个 miscellaneous 文件夹,并在我运行批处理。

实现这一目标的最精简方式是什么。

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

【问题讨论】:

    标签: bash unix terminal


    【解决方案1】:

    这样的事情应该可以工作:

    read -a extensions -p "give me extensions seperated by spaces:  " # read extensions and put them in array $extensions
    for ext in ${extensions[@]}; do  #for each extension stored in the array extensions
    echo -e "- Working with extension $ext"
    destination="/Users/christopherdorman/desktop/folder$ext"
    mkdir -p "$destination"
    mv  -v unsorted/*.$ext "$destination"
    done
    
    miscellaneous="/Users/christopherdorman/desktop/miscellaneous"    
    mv  -v unsorted/*.* "$miscellaneous"; 
    # since previously you moved the required extensions to particular desktop folders
    # move what ever is left under unsorted folder to the desktop miscellaneous folder
    

    【讨论】:

    • 这运行不正常。文件夹已创建,但没有文件移入其中
    • 未测试的移动命令。我刚刚复制了你的移动命令。如果它不起作用,那么它一开始就不起作用。尝试微调移动部分。我稍后会尝试测试它。
    • 在我的代码中,如果你像这样更改移动命令应该可以工作: mv -v unsorted/*.$ext "$destination" (从 $ext 中删除引号)
    【解决方案2】:
    #!/bin/bash
    read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
    if cd /Users/christopherdorman/desktop
      then  while read extension
          do    destination="folder$extension"
            mkdir -p "$destination"
            mv  -v unsorted/*."$extension" "$destination"
          done   <<< "${all_extensions// /$'\n'}"
            mkdir -p foldermisc 
            if mv  -v unsorted/* "foldermisc"
          then  echo "Good News, the rest of Your files have been successfully processed"
            fi
    fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      相关资源
      最近更新 更多