【问题标题】:Create a new folder named the next count创建一个名为 next count 的新文件夹
【发布时间】:2021-12-29 18:43:55
【问题描述】:

我有一个项目文件夹,它根据数字标记项目。它从 001 开始并继续计数。我有一个通过 Alfred 运行的 bash 脚本,但是,我目前必须输入文件夹的名称。

QUERY={query}
mkdir /Users/admin/Documents/projects/"$QUERY"

我想让脚本自动将文件夹命名为下一个数字。 例如,如果最新的文件夹是“019”,那么我希望它自动将其命名为“020”

这是我到目前为止所创造的:

nextNum = $(find ~/documents/projects/* -maxdepth 1 -type d -print| wc -l)
numP = nextNum + 1
mkdir /Users/admin/Documents/projects/00"$numP"

我不确定我的变量语法是否正确,或者变量是否是最好的方法。我是一个完全不喜欢 bash 的菜鸟,因此感谢您提供任何帮助。

【问题讨论】:

  • 不,不正确。请查阅文档,或至少通过shellcheck.net 运行您的代码。
  • 还有:阿尔弗雷德是谁?

标签: bash macos shell


【解决方案1】:

这可能是您正在寻找的:

#!/bin/bash

cd /Users/admin/Documents/projects || exit
if ! [[ -d 000 ]]; then mkdir 000;  exit; fi

dirs=([0-9][0-9][0-9])
mkdir $(printf %03d $(( 1 + 10#${dirs[${#dirs[*]} - 1]} )) )

【讨论】:

    【解决方案2】:

    查看代码中的 cmets 以了解代码。

    #!/bin/bash
    
    # Configure your project dir
    projects_dir=/Users/admin/Documents/projects
    
    # Find the last project number. 
    # You can use wc instead of the sort|tail|sed I used. The result is the same.
    last_project=$(\ls $projects_dir | sort -n | tail -n1 | sed 's/^0*//')
    
    # Use printf to add the leading '0'
    # ${last_project:-0} will substitute '0' when $last_project not set,
    # therefore it will work even if your project directory is empty.
    # $(( expr )) evaluates math expression. Also you can not have 
    # subshell expression in $(( expr )) so you can't substitute
    # $last_project with the expression above
    mkdir $projects_dir/$(printf "%03d" $((${last_project:-0} + 1)))
    

    虽然不太可能,但上述脚本的问题在于,如果项目计数超过 999,则会破坏 3 位数目录名称约定。一个简单的解决方法是使用 4 位数约定。无论如何不建议使用大量文件/子目录的目录,因此如果您确实达到 9999 个项目,最好在第二个项目目录中继续。

    【讨论】:

      【解决方案3】:

      试试这个有趣的:

      projects_dir="/Users/admin/Documents/projects/"
      [[ -n "${HOME}" ]] && projects_dir="${projects_dir/#~/$HOME}"
      mkdir "${projects_dir}"/$(printf "%03d" $(find "${projects_dir}" -maxdepth 1 -type d -printf . | wc -c))
      

      (编辑) 带有 bash 跟踪输出的扩展版本:

      set -x
      projects_dir="/Users/admin/Documents/projects/"
      [[ -n "${HOME}" ]] && projects_dir="${projects_dir/#~/$HOME}"
      cd "${projects_dir}" || exit 1
      printf "each dot is a directory: " 
      find "${projects_dir}" -maxdepth 1 -type d -printf .
      printf "\n"
      let next_directory_id=$(find "${projects_dir}" -maxdepth 1 -type d -printf . | wc -c)
      next_directory_name=$(printf "%03d" "${next_directory_id}")
      mkdir "${projects_dir}"/"${next_directory_name}"
      set +x
      

      【讨论】:

      • 当我运行脚本时,它会创建一个 000 文件夹。之后,它不会创建任何新文件夹
      • @JishCodes:已添加扩展版本。输出是什么?
      • @JishCodes:原始行已被修改以考虑在路径名(`projects_dir` 变量)中使用~。还添加了扩展版本。
      【解决方案4】:
      targ_number=$(find ~/documents/projects/ -maxdepth 1 -type d -print| wc -l)
      
      mkdir /Users/admin/Documents/projects/$(printf "%03d" $targ_number)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        相关资源
        最近更新 更多