0

I just started programming on UNIX this week and one of my homework task is to create a script that reads positive numbers until -99 is entered and negative numbers show an error ( 0 to -98). This is what I've come up with but kinda stuck. thanks :)

#!/bin/bash

COUNTER=1

echo "Enter a positive integer number (-99 to quit):"
read NUMBER

echo "$NUMBER: "
while [ $NUMBER -ge $COUNTER ]; do
    echo $COUNTER
    ((COUNTER++))
done
  • 2
    Can you reword your assignment? "reads positive numbers until -99 is entered and negative numbers show an error ( 0 to -98). " That isn't very clear to me. – Jesse_b Sep 22 '17 at 14:17
  • Like a python script? Entering valid values until quit. so -99 is quit and negative numbers are errors :9 – WickdLotus Sep 22 '17 at 15:12
  • What is the COUNTER for then? That is not explained by this assignment. Do the positive numbers have to be in order? Can they not be entered twice...with your COUNTER if a person entered 5 it would work and then if they entered 2 it would not, but 2 is a valid positive integer no? – Jesse_b Sep 22 '17 at 15:13
0

If I understood you correctly, this should work:

#! /bin/bash -

read -p "Enter a positive integer (-99 to quit): " USR_INT

while [[ -n "$USR_INT" ]]; do

    case "$USR_INT" in
        -99)
            echo "Exiting..."
            exit 0
            ;;
        -*)
            echo "Error, please enter a positive integer."
            exit 1
            ;;
        0)
            echo "Error, please enter a positive integer."
            exit 1
            ;;
        [0-9]*)
            echo "You have entered $USR_INT"
            ;;
        *)
            echo "Error, please enter a positive integer."
            exit 1
            ;;
    esac

    read -p "Enter a positive integer (-99 to quit): " USR_INT

done
  • how would I keep script running if I entered a negative number? :o – WickdLotus Sep 22 '17 at 15:16
  • Remove the Exit 1 line under the -*) part, however you said a negative number should generate an error. – Jesse_b Sep 22 '17 at 15:20
  • another question! what does the asterisk do? – WickdLotus Sep 22 '17 at 15:43
  • It's a wildcard: "Matches any string, including the null string. When the globstar shell option is enabled, and ‘’ is used in a filename expansion context, two adjacent ‘’s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a ‘/’, two adjacent ‘*’s will match only directories and subdirectories." See bash reference manual: [3.5.8.1 Pattern Matching] & [3.2.4.2 Conditional Constructs] – Jesse_b Sep 22 '17 at 15:48
2
#!/bin/bash

while true; do
    read -p 'Number (-99 to quit): '

    if ! [[ "$REPLY" =~ ^-?[0-9]+$ ]]; then echo 'Error: Not an integer' >&2
    elif (( REPLY == -99 ));           then break
    elif (( REPLY <= 0   ));           then echo 'Error: Need positive integers >0' >&2
    else
        printf 'Got "%d", that is number %d\n' "$REPLY" "$(( ++c ))"
    fi
done

This is an infinite loop that is exited when the user enters -99. Any positive integer response will prompt the code to say Got "some number" followed by how many valid numbers read so far, whereas a negative integer, zero, or a non-numeric input will give a diagnostic message on standard error. The code uses the variable REPLY which is the variable written to by read if not given any other variable name.

The test for correct numeric input is done with matching the response against the regular expression ^-?[0-9]+$. This expression will match if the response is on the form that we expect (an optional dash followed by at least one digit). If it doesn't match, a diagnostic message is issued on standard error.

Up until the first elif we can not be sure that $REPLY is an integer. After that, we use (( ... )) for arithmetic evaluation of the comparisons.

The test for -99 needs to come before the test for negative integers, as we otherwise would not have any way of exiting the loop.

  • thanks for this informative post! still learning, used to python indentation ahaha – WickdLotus Sep 22 '17 at 15:38
  • @WickdLotus You can break and indent the lines after each then if you wish. I just wanted it to be compact. I wouldn't know Python if it jumped up and bit me in the nose. – Kusalananda Sep 22 '17 at 15:39
0

Keeping it simple:

!/bin/bash

while :
do
   echo "Enter a positive integer number (-99 to quit):"
   read NUMBER

   if (( NUMBER == -99 ))
   then
      exit
   fi

   echo "$NUMBER: "

   COUNTER=1
   while (( COUNTER <= NUMBER ))
   do
       echo $COUNTER
       ((COUNTER++))
   done
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.