简介
在做项目的时候,有一种需求,class文件只能先以普通用户身份用ftp复制到linux,然后用shell工具以root用户将ftp复制来的文件复制到对应的项目目录,很麻烦,所以写了一段脚本完成这些任务。
功能
只需要把要复制的文件放到同一个临时目录当中,然后修改脚本文件,sourcePath表示临时目录,targetPath表示项目存放的目录
实例
假设:需要替换项目中的多个class,xml等文件,a.class、b.xml、c.class.....,并且存放在/home/mote/temp/文件夹中,web项目在Linux中的路径 /mnt/local/tomcat/webapps/project_a/
步骤:
第一种
1、修改脚本中的“sourcePath”为:/home/mote/temp/
2、修改脚本中的“targetPath”为:/mnt/local/tomcat/webapps/project_a/
3、执行脚本文件 sh shell_name.sh(连续按两次回车键)
第二种
1、执行脚本文件 sh shell_name.sh
2、输入新文件的路径:/home/mote/temp/ 然后回车(注意:最后边加上“/”)
3、输入项目的目录,最好能定位到最小范围:/mnt/local/tomcat/webapps/project_a/ (注意:最后边加上“/”)
4、回车
备注:在替换文件的时候会出现覆盖提示,输入“y”,然后回车即可
脚本
#! /bin/bash
function read_dir(){
#新文件的存放目录
sourcePath="/home/qqt/mote/files/"
#项目目录,最好是webapps的下下级目录
targetPath="/mnt/local/apache-tomcat-7.0.67-sta/webapps/ELSServer_STA/"
#全新文件提示
new_files=""
echo "默认源文件目录为:$sourcePath"
echo "默认应用目录为:$targetPath"
echo "请输入源文件路径(直接点击回车则使用默认目录)"
read source_input
echo "请输入应用目录(直接点击回车则使用默认目录)"
read target_input
if test -n "$source_input"
then
if test -n "$target_input"
then
for file in `ls $source_input`
do
if test -f $source_input${file}
then
bl=$(find $target_input -type f -name $file)
path_sta=${bl%/*}
if [ ! ${#path_sta} -gt 1 ]
then
new_files="${new_files}, ${file}"
else
cp -i -r $source_input$file $path_sta/$file
fi
fi
done
else
for file in `ls $source_input`
do
if test -f $source_input${file}
then
bl=$(find $targetPath -type f -name $file)
path_sta=${bl%/*}
if [ ! ${#path_sta} -gt 1 ]
then
new_files="${new_files}, ${file}"
else
cp -i -r $source_input$file $path_sta/$file
fi
fi
done
fi
else
if test -n "$target_input"
then
for file in `ls $sourcePath`
do
if test -f $sourcePath${file}
then
bl=$(find $target_input -type f -name $file)
path_sta=${bl%/*}
if [ ! ${#path_sta} -gt 1 ]
then
new_files="${new_files}, ${file}"
else
cp -i -r $sourcePath$file $path_sta/$file
fi
fi
done
else
for file in `ls $sourcePath`
do
if test -f $sourcePath${file}
then
bl=$(find $targetPath -type f -name $file)
path_sta=${bl%/*}
if [ ! ${#path_sta} -gt 1 ]
then
new_files="${new_files}, ${file}"
else
cp -i -r $sourcePath$file $path_sta/$file
fi
fi
done
fi
fi
if [ ${#new_files} -gt 1 ]
then
echo "项目中不能存在以下文件,请手动复制!"
echo ${new_files}
fi
}
read_dir