Project & Module/Shell
파일 수정 Shell Program
무대포개발자
2015. 1. 16. 09:31
1. Title :
파일 수정 Shell Program
2. Purpose :
파일의 특정 내용을 수정하는 프로그램
3. 사용법 :
1) sh modifyFile.sh 명령어를 입력하면 필수 아규먼트와 옵션 아규먼트 안내가 나온다.
2) 설명대로 아규먼트를 넣어주면 실행이 됨.
3) 변경할 내용이 중복이면 변경할 내용의 특정행을 입력하도록 설정
4. Supplement point :
1) Backup 기능
2) 변경할 내용이 없을 때 예외 처리
modifyFile.sh
#!/bin/ksh ### include shell ### . ./help.sh . ./util.sh #================================== Variable count=0 #================================== Arg Check if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ] then helpModifiedFile exit fi printArgCheckTitle if [ -z "$6" ] then echo "param6 (specified line) is null" fi #================================== Validate # 1. validateFile # 2. validateExisitingContent # 1. validateFile printFileValidateTitle if isFile $1 then echo "" else echo "" echo "shell program exit." exit fi # 2. validateExisitingContent printContentValidateTitle (unembodiment) if isContent $1 (unembodiment) then echo "" else echo "" echo "shell program exit." exit fi #================================== Module # 1. backup (unembodiment) # 2. CheckDuplicateModifyFilePart # 3. ExecuteModifyFile # 1. backup # 2. CheckDuplicateModifyFilePart if [ -z "$6" ] then for i in `grep -n $4 $1 | sed 's/ //g'` do echo "[assume modified part] Line $i" count=`expr $count + 1` done else echo "[assume modified part] Line `sed "$6,$6s/$4/$5/" $1 | grep -n $5`" fi if [ "$count" -ge 2 ] then echo " " echo "result = duplicte" echo "input param4 (specified line)" echo "program exit" exit fi # 3. ExecuteModifyFile echo "" echo "#modify" if [ -z "$6" ] then sed "s/$4/$5/" $1 > $2 echo "complete" else sed "$6,$6s/$4/$5/" $1 > $2 echo "complete" fi
help.sh
function helpModifiedFile { echo "" echo "# modifiedFile_help #" echo "" echo "* Essential" echo "@1=existing FileName" echo "@2=new FileName" echo "@3=backup FileName" echo "@4=existing content" echo "@5=modified content" echo "" echo "* Optional" echo "@6=specified line" echo "" } function help { echo "help" }util.sh
isFile() { status=0 for var in "$@" do if [ -f "$var" ] then echo "" else echo "$var is not exist" status=1 fi done return $status } function printFileValidateTitle { echo "" echo "# file validate #" echo "" } function printArgCheckTitle { echo "" echo "# Arg Check #" echo "" } function printModuleTitle { echo "" echo "# Module #" echo "" }