您的位置:首页 > 运维架构 > Shell

Shell文件自动替换脚本

2013-06-20 23:56 489 查看
文件替换脚本

使用方法:
./file-update.sh 文件名 大致路径(确定再那个路径下搜索)

#! /bin/bash
date1=`date +%Y%m%d%s`
date2=`date +%Y%m%d`
if [ -e /mnt/backup/$date2 ] ; then
:
else
mkdir -p /mnt/backup/$date2
fi
# backup-file
backup-file () {
echo "Do you want to backup this file? please select `echo "[y/n]"` "
read FILE
case $FILE in
y)
file1=$i
file2=$1
s=${file1##/*/}
r=${file2:=$s}
cp $i /mnt/backup/$date2 && echo "backup file ok" || echo "backup file fail"  ;;
n)echo "NOT BACKUP file"  ;;
*) backup-file ;;
esac
}
# update-file
update-file () {
echo "do you want to UPDATE this file ? please select `echo [y/n]`"
read FILE
case $FILE in
y)
file1=$i
file2=$1
s=${file1##/*/}
r=${file2:=$s}
cp $r $i && echo "update file ok" && rm -rf $r  || echo "update file fail" ;;
n) echo "NOT UPDATE file" ;;
*) update-file ;;
esac
}
# find backup update  update-file
Path=${2:?请输入替换文件大致路径}
select i in `find $Path -name $1`; do
backup-file
update-file
break
done


脚本演示:




执行过后会再/mnt/backup/创建一个时间的文件夹
并将所要替换文件移到上步新建的文件夹里
将新文件替换过去
最后删除新文件

本文出自 “浮躁的产品汪” 博客,请务必保留此出处http://haolulu.blog.51cto.com/3164472/1226299
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: