
"-y" 또는 "--yes"와 같은 특정 인수가 전달되면 스크립트를 비대화형으로 만들고 사용자 확인을 건너뛰고 싶습니다. 또한 내가 소스로 사용하는 다른 스크립트에 인수를 전달하는 방법도 알고 싶습니다.
SHORT=yq
LONG=yes,quick
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
eval set -- "$PARSED"
q=0; autoConfirm=0
while true; do
case "$1" in
-q|--quick)
q=1
shift
;;
-y|--yes)
autoConfirm=1
shift
;;
--)
shift
break
;;
*)
echo "Invalid option. Use -h for help"
exit 3
;;
esac
done
assertConfirmation () {
local promptMsg=$1 autoConfirm=$2
if (( autoConfirm )); then
return
else
clear
read -n 1 -p "$promptMsg (yes/No) "
printf '\n========================================================================'
if [[ $REPLY =~ ^([Yy])$ ]]; then
return
fi
fi
return 1
}
if assertConfirmation "Install this?" "${autoConfirm:?}"; then
install
fi
source installation "${autoConfirm:?}" "${q:?}"
답변1
설치 스크립트의 예상 내용에 따라 다릅니다. 일을 단순하게 유지하려면 이를 인수 -y
로 받아들이고 인수로 사용하는 것이 가장 좋습니다 -q
. 이를 통해 q
및가 1(기본적으로 null) 대신 및 autoConfirm
로 설정 되도록 사례 설명을 변경하고 다음 과 같이 설치 스크립트를 호출할 수 있습니다.-q
-y
source installation $autoConfirm $q
해당 변경으로 인해 . [[ $autoconfirm ]]
대신 설정되어 있는지 확인하는 데 사용됩니다 (( autoConfirm ))
.