사용 언어 : bash shell script
환경 : MAC OS
MAC에서는 POSIX sed를 사용하기 때문에 리눅스의 gnu sed보다 기능이 제한적이다.
따라서 MAC에 gnu sed를 설치하고 sed를 대체하게끔 환경 설정이 필요하다
brew install gnu-sed
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
#!/usr/bin/env bash
totalSum=0
repeatFlag=1
numberCount=0
operationCount=0
inputNumber=0
fileName="calculatorResult.txt"
resultCount=1
fileLineLocation=0
fileExistCreate()
function fileExistCreate() {
if [ -f ${fileName} ]; then
echo "file Found"
fileLineLocation=`cat ${fileName} | wc -l | tr -d ' '`
resultCount=$(($fileLineLocation/5+1))
else
echo "file not Found Create New File"
fi
echo "${resultCount} Turn Result" >> $fileName
echo "Input Number :" >> $fileName
echo "Input Operation :" >> $fileName
echo "Mathematical expression :" >> $fileName
echo " " >> $fileName
}
파일 존재여부 확인 및 서식 생성 함수
fileInputNumber()
function fileInputNumber() {
fileInputNumberCommand=`sed -i ''"$(($fileLineLocation+2))"'s/$/ '"$1"'/' "${fileName}"`
echo -n $fileInputNumberCommand
}
연산에 사용된 숫자들을 파일에 기록하는 함수
매개변수
$1 : 숫자(정수)
fileInputOperation()
function fileInputOperation() {
case $1 in
"+")
fileResultSyntaxCommand=`sed -i ''"$(($fileLineLocation+4))"'s/:/: (/' "${fileName}"`
echo -n $fileResultSyntaxCommand
;;
"-")
fileResultSyntaxCommand=`sed -i ''"$(($fileLineLocation+4))"'s/:/: (/' "${fileName}"`
echo -n $fileResultSyntaxCommand
;;
esac
fileInputOperationCommand=`sed -i ''"$(($fileLineLocation+3))"'s/$/ \'"$1"'/' "${fileName}"`
fileResultCommand=`sed -i ''"$(($fileLineLocation+4))"'s/$/ \'"$1"'/' "${fileName}"`
echo -n $fileInputOperationCommand
echo -n $fileResultCommand
}
연산에 사용된 연산자들을 파일에 기록하는 함수
매개변수
$1 : 연산자
fileInputNumberResult()
function fileInputNumberResult() {
fileResultCommand=`sed -i ''"$(($fileLineLocation+4))"'s/$/ '"$2"'/' "${fileName}"`
echo -n $fileInputNumberCommand
case $1 in
"+")
fileResultSyntaxCommand=`sed -i ''"$(($fileLineLocation+4))"'s/$/ )/' "${fileName}"`
echo -n $fileResultSyntaxCommand
;;
"-")
fileResultSyntaxCommand=`sed -i ''"$(($fileLineLocation+4))"'s/$/ )/' "${fileName}"`
echo -n $fileResultSyntaxCommand
;;
esac
}
연산 결과를 기록하는 함수
매개변수
$1 : 연산자, $2 : 숫자(정수)
InputJudge()
function inputJudge() {
read -p "InputNumber : " inputNumber
numberRegex="^-?[0-9]+$"
until [[ $inputNumber =~ $numberRegex ]]; do
echo "[Error] This is not Integer"
read -p "InputNumber : " inputNumber
done
}
숫자(정수)를 입력받아 inputNumber에 저장하는 함수
arithmeticJudge()
function arithmeticJudge() {
local operation=""
local calculateNumber
operationRegex="^[\+|\*|\/|\%|\-]$"
read -p "Input the arithmetic operations(+, -, *, /, %) : " operation
until [[ $operation =~ $operationRegex ]]; do
echo "[Error] This is not right operation"
read -p "Input the arithmetic operations(+, -, *, /, %) : " operation
done
inputJudge
calculateNumber=$inputNumber
while [[ ($operation = "/" && $calculateNumber -eq 0)]] || [[($operation = '%' && $calculateNumber -eq 0) ]]; do
echo "[Error] calculation isn't possible"
inputJudge
calculateNumber=$inputNumber
done
fileInputNumber $calculateNumber
case $operation in
"+")
totalSum=$(($totalSum + $calculateNumber))
;;
"-")
totalSum=$(($totalSum - $calculateNumber))
;;
"*")
totalSum=$(($totalSum * $calculateNumber))
;;
"/")
totalSum=$(($totalSum / $calculateNumber))
;;
"%")
totalSum=$(($totalSum % $calculateNumber))
;;
esac
fileInputOperation "$operation"
fileInputNumberResult "$operation" $calculateNumber
}
연산자를 입력받아 연산 이후 계산 결과를 totalSum에 저장하는 함수
repeatAnswer()
function repeatAnswer() {
local repeatAnswer=""
read -p "Will you stop? (YES, NO) : " repeatAnswer
until [[ $repeatAnswer = "YES" || $repeatAnswer = "NO" ]]; do
echo "[Error] Wrong Input"
read -p "Will you stop? (YES, NO) : " repeatAnswer
done
if [ $repeatAnswer = "NO" ]; then
repeatFlag=1
else
repeatFlag=0
numberCount=$(($numberCount + 1))
fi
}
연산을 계속 할 것인지 물어보고 조건에 맞게 repeatFlag로 변환하는 함수
#main
fileExistCreate
while [ $repeatFlag -eq 1 ]; do
numberCount=$(($numberCount + 1))
if [ $numberCount -eq 1 ]; then
inputJudge
totalSum=$inputNumber
fileInputNumber $totalSum
fileResultCommand=`sed -i ''"$(($fileLineLocation+4))"'s/$/ '"${totalSum}"'/' "${fileName}"`
echo -n $fileReusltCommand
fi
arithmeticJudge
repeatAnswer
done
operationCount=$(($numberCount - 1))
echo " calculate result = " $totalSum
echo " numberCount = " $numberCount
echo " operationCount = " $operationCount
fileTotalResultCommand=`sed -i ''"$(($fileLineLocation+4))"'s/$/ = '"${totalSum}"'/' "${fileName}"`
echo -n $fileInputNumberCommand
각 함수들을 사용하여 사칙연산을 수행하고 파일에 기록한다.
첫 실행
2번째 실행
연산 결과
cat calculatorResult.txt