예 또는 아니오 응답을 true 또는 false (0 또는 1)로 변환하는 함수를 작성했습니다. 그러나 스크립트를 실행할 때마다 'command not found'오류가 발생합니다.반환 값 테스트 : 명령을 찾을 수 없음
bash-3.2$ sh test.sh
Do you want to drop the table of invalids? [n]:y
After read: y
test.sh: line 24: y: command not found
After assignment: y
test.sh: line 26: y: command not found
bash-3.2$ sh test.sh
Do you want to drop the table of invalids? [n]:n
After read: n
test.sh: line 24: n: command not found
After assignment: n
test.sh: line 26: n: command not found
업데이트 : 제가 스크립트를 실행하면, 나는 이러한 오류를 얻을 그것을
get_boolean(){
#==============================================================================================
# Returns false if the first argument is NO and returns true if it is YES.
# If the first argument is not a valid YES or NO,
# then the return value depends on the default specified by argument 2 (Default value)
#==============================================================================================
if [ "$1" == 'NO' ] || [ "$1" == 'no' ] || [ "$1" == 'No' ] || [ "$1" == 'N' ] || [ "$1" == 'n' ]; then
return 1;
elif [ "$1" == 'YES' ] || [ "$1" == 'yes' ] || [ "$1" == 'Yes' ] || [ "$1" == 'Y' ] || [ "$1" == 'y' ]; then
return 0;
elif [ "$2" == 'NO' ] || [ "$2" == 'no' ] || [ "$2" == 'No' ] || [ "$2" == 'N' ] || [ "$2" == 'n' ]; then
return 1;
elif [ "$2" == 'YES' ] || [ "$2" == 'yes' ] || [ "$2" == 'Yes' ] || [ "$2" == 'Y' ] || [ "$2" == 'y' ]; then
return 0;
fi
}
read -p 'Do you want to drop the table of invalids? [n]:' DROP_TABLE_OF_INVALIDS
echo "After read: $DROP_TABLE_OF_INVALIDS"
DROP_TABLE_OF_INVALIDS=get_boolean "$DROP_TABLE_OF_INVALIDS" 'n'
echo "After assignment: $DROP_TABLE_OF_INVALIDS"
if $DROP_TABLE_OF_INVALIDS; then
echo "Hello. I will drop the table"
fi
문제를 해결 도와주세요 (! 감사합니다 Barmar)를 아래 코드는 일
get_boolean(){
#==============================================================================================
# Outputs false if the first argument is NO and outputs true if it is YES.
# If the first argument is not a valid YES or NO,
# then the output value depends on the default specified by argument 2 (Default value)
#==============================================================================================
if [ "$1" == 'NO' ] || [ "$1" == 'no' ] || [ "$1" == 'No' ] || [ "$1" == 'N' ] || [ "$1" == 'n' ]; then
echo false;
elif [ "$1" == 'YES' ] || [ "$1" == 'yes' ] || [ "$1" == 'Yes' ] || [ "$1" == 'Y' ] || [ "$1" == 'y' ]; then
echo true;
elif [ "$2" == 'NO' ] || [ "$2" == 'no' ] || [ "$2" == 'No' ] || [ "$2" == 'N' ] || [ "$2" == 'n' ]; then
echo false;
elif [ "$2" == 'YES' ] || [ "$2" == 'yes' ] || [ "$2" == 'Yes' ] || [ "$2" == 'Y' ] || [ "$2" == 'y' ]; then
echo true;
fi
}
read -p 'Do you want to drop the table of invalids? [n]:' DROP_TABLE_OF_INVALIDS
echo "After read: $DROP_TABLE_OF_INVALIDS"
DROP_TABLE_OF_INVALIDS=$(get_boolean "$DROP_TABLE_OF_INVALIDS" 'n')
echo "After assignment: $DROP_TABLE_OF_INVALIDS"
if $DROP_TABLE_OF_INVALIDS; then
echo "Hello. I will drop the table"
fi
여기에있다 작동하게 한 수정본 :
- functio n '대신에 echo'es.
- 출력 값은 bash true 또는 false입니다. 그렇지 않으면 0을 얻습니다. 명령을 찾을 수 없거나 1 : 명령을 찾을 수 없습니다.
- 함수 호출은 '$ ('및 ')'로 묶입니다.
감사합니다 것입니다! 리턴이 단지 종료 상태를 설정하고 C, C++, Java와는 다른 함수 출력이 아니라는 것을 분명히 알기를 바랍니다. 어쨌든 코드를 수정 한 편집 내용을 게시 할 것입니다. – Shivanand
함수가'' "'를 반환 할 때 if $ DROP_TABLE_OF_INVALIDS가 작동하는지 확신 할 수 없습니다. 'if'에는 인수가 필요합니다. – Barmar
예 .. 제거해야합니다 .. 이미 함수에 전달되는 기본값이 있습니다 .. 다시 한번 감사드립니다! – Shivanand