Bash 脚本创建下一个最高文件

Ria*_*Ria 5 command-line bash scripts

我的任务是编写一个脚本,在特定目录中创建下一个最高文件。我已设法对目录中的文件进行排序,以便脚本输出最高的文件名。例如从red_01 red_02 red_03它输出的文件red_04

我的下一个任务是创建最高文件,那么如何让它创建red_04文件?我是否需要将数字存储在变量中并以某种方式递增?

Zia*_*zis 2

这将满足您的需要。调整您需要的任何路径的路径,也可以使用变量并将它们解析到脚本中。但是,是的,你可以自己调整;)

#!/bin/bash
#count your existing files
NUMBERATM=`find ./ -type f -name "red_*" | wc -l`
#add +1 to create the next higher file
NUMBERATM=$((NUMBERATM+1))

#check if it's below 10 since you need the 0 infront of it
if [ $NUMBERATM -lt "10" ]; then
   var=0$NUMBERATM
else
   var=$NUMBERATM
fi

#create your file
touch red_$var
Run Code Online (Sandbox Code Playgroud)