I'm learning how to write scripts, and am having an issue with an if statement. The script reads in user input for their scores, and calculates their average grade. However, if the user enters a score lower than 0 or greater than 100, the script should ask them to re-enter a valid value. Here is the code below:
#!/bin/bash SCORE=0 AVERAGE=0 SUM=0 NUM=0 while true do # Read score from user echo -n "Enter your score [0-100] ('q' for quit): "; read SCORE # Validate user input if (($SCORE < 0)) || (($SCORE > 100)) then echo "Invalid input, try again: " #elif (( $SCORE == "q" )) elif [ "$SCORE" == "q" ] then echo "Average rating: $AVERAGE%." break else # Perform calculation SUM=$[$SUM + $SCORE] NUM=$[$NUM +1] AVERAGE=$[$SUM / $NUM] echo "Average rating: $AVERAGE%" fi done Notice I have the elif commented out. When I write that elif with [] notation, it works as expected. However, if I switch to the elif which uses (()), and enter "00" as my score, the program executes the "then" block on and exits. Why is it doing that? 00 is not 'q'.
$[ ... ]is legacy syntax. Bash supports it for compatibility with ancient pre-POSIX shells (which is to say, shells from before 1991), but unlike$(( )), it isn't guaranteed to available on baseline-POSIX shells -- it doesn't work, for instance, withashordash.==isn't guaranteed to work inside[ ]either -- the POSIX-compliant string comparison operator is just=. If you want bashisms, use[[ ]]to make it explicit that you're relying on extended syntax rather than intending to use portable constructs.ifstatement but with the command which follows it. The brackets are not exclusively part of theifsyntax.(( ... ))and[[ ... ]]are compound commands whereas[is a shell builtin. The syntax forifis actuallyiflistthenlistfi.if [ "$(grep -e content filename)" ]; then ...rather than the correct, much simpler, and faster-to-executeif grep -q -e content filename; then ....