首页 > 解决方案 > 如何通过在无限循环期间复制文件来更改文件名

问题描述

所以我有一个循环,它将在循环期间复制一个名为 file1 的文件,怎么可能永远复制一个名为 file2、file3 等的文件?

标签: bash

解决方案


假设(我不推荐它(我真的不推荐它(使用风险自负))):

#!/bin/bash

# define 'i' as the variable to contain a number
i=0
# open while loop to cycle condition true (infinitely until you quit the application)
while true; do
# Iterate i = i + 1  (which works because you've defined it as a number previously)
((i++))
# The copy operation which will copy file to file1, file2 file3 ...
cp file file$i
done

推荐阅读