文件替换(交互式)

前端之家收集整理的这篇文章主要介绍了文件替换(交互式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

3cbcbf9a20d810672710a774355eda6f.jpg

  在增量发版博文里,自动识别目标文件路径是采用了标记目录特征。当部署环境多变时,会多有不便。这个替换程序,在使用时需要手动输入目标目录。当需要进一步自动化时,可以与 fint 配合使用。


  程序发布时,需要借助一个环境变量(DONATELLO_HOME),以保证函数调用的正常。

  程序目录结构

.
├──bin
│├──mTime#暂未启用
│└──screensize#获取终端宽度、高度,一个C语言程序
├──lib
│├──displayHeader#设计用来打印程序头部格式,未启用
│├──doReplace#库文件,提供替换文件函数
│├──findObjectFile#在目标路径中,寻找新文件的路径,处理多个文件的交互选择
│├──getscreensize#处理bin下C语言程序的返回结构
│├──printObject#把替换的对应关系输出到终端
│└──progressBar#打印一个进度条,暂缓程序执行
└──Replace#主程序

  主程序 Replace:

#!/bin/bash
#Replace-1.3
#Theauthor:donatello
#
chkdep(){
[-z$1]&&echo"Directory?"
[-z$1]&&exit4
[!-d$1]&&echo"Withoutdirectory,pleaseagain."
[!-d$1]&&exit6
[-d$1]&&baseDirectory=$1
}
chkdep$1
#Formatthe"baseDirectory".
declare-gbaseDirectory="$(dirname$baseDirectory)"/"$(basename$baseDirectory)"
declare-gfileName=$(ls-1R.)
declare-g-aobjectFileName
declare-g-iobj=1
declare-grfather="${DONATELLO_HOME}/bin"
echo$father

clear
#Callthefunctions.
#.displayHeader
.${father}/lib/findObjectFile
.${father}/lib/printObject
.${father}/lib/doReplace

#Definecountfunction.
#Countthenumberofarrayelements.
count_objectFileName(){
local-ik=${#objectFileName[@]}
[1-ge$k]&&echo-e"\t\fCheckfornewfiles"&&return33
return0
}
#
findFile
#count_objectFileName&&[33-eq$?]&&exit33
count_objectFileName
[33-eq$?]&&exit33
printOjectFileDirName
replaceFile
echo-e'\nShellscriptends.'
#Shellscriptover.


  寻找目标文件 findObjectFile

#!/bin/bash
#Thecommandthatwillreplacethefileifexists.
MV="$(which--skip-aliasmv)-v"

#Checkinputbyuserwhentherearemanyfiledirectorywillbeconfirm.
confirmInfo(){
#echo"k=$k"
whiletrue;do
#Printfilestherearesamename.
if[1-lt$k];then
echo-e"fileofthesamename:\f<<$i>>"
for((i=1;i<=k;i++));do
#name=$(envbasename${file[$i]})
#echo-e"\tNo.$i${name}\n"
echo-e"\tNo.$i${file[$i]}\n"
done
fi

#Confirmfile.
whiletrue;do
read-p"Pleaseconfirmthepathofthefilethatwillbereplace.No.->"-N1confirm1>/dev/null&&echo""
if[$confirm-ge1]&&[$confirm-le$k];then
break
else
echo"No'serror."
fi
done

#Reconfirmfile.
read-p"Doyouconfirmyouchoice?(yY/nN)..."-N1reconfirm&&echo""
if['y'=$reconfirm];then
clear
break
elif['n'=$reconfirm];then
echo"Pleaseconfirmagain."
else
echo"Char'sinvalid."
fi
done
#printf"Goodluck,thefileis%s.\n""file[$confirm]"
return$confirm
}

#Findthefilethatwillbereplaced.
findFile(){
local-afile
local-ik=1
local-iconfirm=0
local-lreconfirm=n

foriin$(echo$fileName);do#cycle:Afileisloopedonce.
[!-f$i]&&continue

objectFileName=$(find$baseDirectory-name$i-print)
foritemin$(echo"${objectFileName}");do
#echo"item$k:$item"
file[$k]=${item}
letk=k+1
done
k=${#file[@]}
#echo$i$k

#Thereisnofilewiththesamename,directreplacement.
if[1-eq$k];then
objectFileName[$obj]=${file[$k]}
letobj=obj+1
fi
#Whenthesamefilenameexist,theuserisrequired
#toconfirmthecorrectfilepath.

#Calllocalfunction:confirmInfo
if[1-lt$k];then
confirmInfo
k=$?
#echo"Thefileis${file[${answer}]}"
objectFileName[$obj]=${file[$k]}
letobj=obj+1
fi

#sleep1
#variablereset.
k=1
unsetfile
#over
done
}

echo"Willfindfiles..."
#Over

  打印输出替换对应关系 printObject

#!/bin/bash

BASENAME=$(which--skip-aliasbasename)

printOjectFileDirName(){
clear
#.displayHeader
echo"Printobjectfiles..."
local-ik=${#objectFileName[@]}
#echo"length,k=$k"
for((i=1;i<k;i++));do
echo"Willmove$(${BASENAME}${objectFileName[$i]})--->>${objectFileName[$i]}"
done

.${father}/lib/progressBar
}
#Over

  打印对象后,调用了进度条效果 progressBar

#!/bin/bash
#
declare-icycle=1
declare-ilength=15
declare-ik=0
#echo-n'-------------------------------------------------------'

printBar(){
for((i=0;i<length;i++));do
echo-n'-'
done

printf"\r"
for((i=0;i<cycle*length;i++));do
echo-n'-'&&sleep0.05
echo-e-n'\b\'&&sleep0.05
echo-e-n'\b-'&&sleep0.05
echo-e-n'\b/'&&sleep0.05
echo-e-n'\b*'&&sleep0.3
echo-e-n'\b-'

letk=k+1
if[$k-eq$length]
then
k=0
printf"\r"
fi
done
}
printBar


  替换文件 doReplace:

#!/bin/bash
MV="$(which--skip-aliasmv)-v"
BASENAME=$(which--skip-aliasbasename)

replaceFile(){
clear
echo"Replaceobjectfiles..."
local-ik=${#objectFileName[@]}
for((i=1;i<k;i++));do
$MV$(${BASENAME}${objectFileName[$i]})${objectFileName[$i]}
done
}
#doit
#Over
原文链接:https://www.f2er.com/bash/389998.html

猜你在找的Bash相关文章