4

我想要一个 bash 脚本来创建名为“exam_folder”的文件夹,如果该文件夹已经存在(例如,我将再次运行该脚本),该脚本将创建“exam_folder_1”,如果该文件夹存在我想要创建“exam_folder_2”的脚本等等。我想聪明地拥有它:)我得到了这样的东西:

function create_folder {

if [ -d "$1" ]; then # if exists
    mkdir $1_`i_max` # function i_max would find out the max number and add 1
else 
    mkdir $1
fi }

谢谢您的帮助。

4

3 回答 3

0

这应该可以在任何符合 POSIX 的 shell 下工作(尽管我不是 100% 确定[ "$foo" -eq "$foo" ]is-number-check)。dash我在和下对其进行了快速测试busybox' ash

create_folder()
{
    if [ -e "$1" ]; then
        i=0
        for d in "$1"_*; do
            _i="${d##*_}"
            [ "${_i}" -eq "${_i}" ] 2>/dev/null && i="${_i}"
        done
        i=$((i+1))
        mkdir "$1_${i}"
    else
        mkdir "$1"
    fi
}
于 2013-04-30T12:23:16.487 回答
0

你可以这样做:

max=`ls -1d exam_folder* | tr -dc '[0-9\n]' | sort -k 1,1n | tail -1`
mkdir exam_folder_$((max + 1))

请注意,如果您同意,它不会填补序列中的“漏洞”。

于 2013-04-30T15:14:22.277 回答
0

两个回答者:兼容的和纯

最后编辑:使正则表达式更精确并更正一些计数错误。

就像是:

create_folder() {
    if [ -e "$1" ] ;then
        counter=1;
        while [ -e "$1-$counter" ] ;do 
            counter=$((counter+1))
            done
        mkdir "$1-$counter";
    else
        mkdir "$1";
    fi;
}

能做:

$ ls -ltr /tmp |grep foo
$ create_folder /tmp/foo\ bar
$ create_folder /tmp/foo\ bar
$ create_folder /tmp/foo\ bar
$ ls -ltr /tmp |grep foo
drwxr-xr-x  2 user user   4096  1 mai 10:53 foo bar
drwxr-xr-x  2 user user   4096  1 mai 10:53 foo bar-1
drwxr-xr-x  2 user user   4096  1 mai 10:53 foo bar-2

或使用更好的格式和另一种计数方式(如果存在许多目录则更快,因为它使用专门的计数功能,但如果因为分叉而更长。):

create_folder() {
    if [ -e "$1" ] ;then
        counter=`/bin/ls -1 "$1" "$1-"* 2>&1 | grep -E "^$1(-[0-9]*)?" | wc -l`;
        nname="`printf "%s-%04d" "$1" $counter`";
        mkdir "$nname";
    else
        mkdir "$1";
    fi;
}

这是在不同的实现下测试的:

$ create_folder /tmp/test
$ ls -ltr /tmp/| grep test
drwxr-xr-x 2 user user 4096 Apr 30 13:25 test

$ create_folder /tmp/test
$ create_folder /tmp/test
$ ls -ltr /tmp/| grep test
drwxr-xr-x 2 user user 4096 Apr 30 13:25 test
drwxr-xr-x 2 user user 4096 Apr 30 13:26 test-0001
drwxr-xr-x 2 user user 4096 Apr 30 13:26 test-0002

并且 dirname 包含空格:

$ create_folder "foo bar"
$ create_folder "foo bar"
$ create_folder "foo bar"
$ create_folder "foo bar"
$ ls -ltr | grep foo
drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar
drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar-0001
drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar-0002
drwxr-xr-x 2 user user 4096 Apr 30 13:35 foo bar-0003

对于粉丝:

create_folder() {
    local -a list
    local counter nname
    if [ -e "$1" ] ;then
        list=("$1-"*)
        list=(${list[@]// /_})
        list=(${list[@]//*\*})
        counter=$((${#list[@]}+1))
        printf -v nname "%s-%04d" "$1" $counter
        mkdir "$nname";
    else
        mkdir "$1";
    fi;
}

非常快,没有分叉和漂亮的格式:

$ create_folder "/tmp/foo bar"
$ create_folder "/tmp/foo bar"
$ create_folder "/tmp/foo bar"
$ create_folder "/tmp/foo bar"

$ ls -ltr /tmp/ | tail -n 4
drwxr-xr-x  2 user user 4096  1 mai 11:17 foo bar
drwxr-xr-x  2 user user 4096  1 mai 11:17 foo bar-0001
drwxr-xr-x  2 user user 4096  1 mai 11:17 foo bar-0002
drwxr-xr-x  2 user user 4096  1 mai 11:17 foo bar-0003
于 2013-04-30T11:20:25.777 回答