欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

把文件复制N份的2个Shell脚本代码

程序员文章站 2023-11-18 16:01:16
测试时需要大量文件,所以写了脚本进行拷贝。有规律的文件名利于引用。 复制代码 代码如下: #!/bin/sh # file name : batchcp.sh...

测试时需要大量文件,所以写了脚本进行拷贝。有规律的文件名利于引用。


复制代码 代码如下:

#!/bin/sh
# file name : batchcp.sh
# author: zhouhh
# email: ablozhou@gmail.com
# date : 2008.3.31
 
echo "input your file name"
 
read  filename
 
echo "how many times you want copy?"
 
read times
 
echo "your file name is ${filename}, you want to copy ${times} times."
 
base=`echo ${filename}|cut -d "." -f 1`
ext=`echo ${filename}|cut -d "." -f 2`
 
for(( i=0;i<${times};i++))
do
echo "copy ${base}.${ext} to ${base}$i.${ext} ..."
cp "${base}.${ext}" "${base}$i.${ext}"
done

另一个版本

复制代码 代码如下:

#!/bin/sh
# file name : batchcp.sh
# author: zhouhh
# email: ablozhou@gmail.com
# date : 2008.3.31
 
echo "input your file name"
 
read  filename
 
echo "how many times you want copy?"
 
read times
 
echo "your file name is ${filename}, you want to copy ${times} times."
#find . and cut the left part of the file name using ##
ext=${filename##*.}
#find . and cut the right part of the file name using %
base=${filename%.*}
echo "base:$base"
echo "ext:$ext"
 
for(( i=0;i<${times};i++))
do
echo "copy ${base}.${ext} to ${base}$i.${ext} ..."
cp "${base}.${ext}" "${base}$i.${ext}"
done