사용자에게 값 확인을 요청할 수있는 많은 구성 변수가 있습니다. 따라서 존재하는 실행 번호를 지정하는 변수가있을 수 있으며 스크립트의 현재 변수 값이 ok인지 묻는 스크립트를 원합니다. 사용자가 값이 잘못되었다고 응답하면 스크립트는 새 값을 요청하고이를 변수에 지정합니다.사용자와 기존 변수의 값을 확인하는 Bash 함수를 작성하는 방법
나는 이것을하기위한 함수에서 초기 시도를했지만 그 실행에는 약간의 어려움이있다; 그것은 노점. 문제 해결에 대한 도움과 내가 사용하고있는 접근법에 대한 비판을 소중히 여깁니다. 다음과 같이 코드는 다음과 같습니다
confirmVariableValue(){
variableName="${1}"
variableValue="${!variableName}"
while [[ "${userInput}" != "n" && "${userInput}" != "y" ]]; do
echo "variable "${variableName}" value: "${variableValue}""
echo "Is this correct? (y: continue/n: change it/other: exit)"
read userInput
# Make the user input lowercase.
userInput="$(echo "${userInput}" | sed 's/\(.*\)/\L\1/')"
# If the user input is "n", request a new value for the variable. If the
# user input is anything other than "y" or "n", exit. If the user input
# is "y", then the user confirmation loop ends.
if [[ "${userInput}" == "n" ]]; then
echo "enter variable "${variableName}" value:"
read variableValue
elif [[ "${userInput}" != "y" && "${userInput}" != "n" ]]; then
echo "terminating"
exit 0
fi
done
echo "${variableValue}"
}
myVariable="run_2014-09-23T1909"
echo "--------------------------------------------------------------------------------"
echo "initial variable value: "${myVariable}""
myVariable="$(confirmVariableValue "myVariable")"
echo "final variable value: "${myVariable}""
echo "--------------------------------------------------------------------------------"
이 질문은 코드 검토에 관한 내용이므로 주제가 아닌 것으로 보입니다. – chepner
아니요 코드가 작동하지 않습니다.나는 이유를 모르며 도움을 요청하고 있습니다. – d3pd
@chepner : 부분 만 코드 리뷰입니다. 오류 보고서가 있습니다. "멈 춥니 다." – bishop