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
3 Answers 3
#!/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 ahahaWickdLotus– WickdLotus2017年09月22日 15:38:17 +00:00Commented Sep 22, 2017 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.2017年09月22日 15:39:59 +00:00Commented Sep 22, 2017 at 15:39
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? :oWickdLotus– WickdLotus2017年09月22日 15:16:11 +00:00Commented Sep 22, 2017 at 15:16
-
Remove the
Exit 1
line under the-*)
part, however you said a negative number should generate an error.jesse_b– jesse_b2017年09月22日 15:20:26 +00:00Commented Sep 22, 2017 at 15:20 -
another question! what does the asterisk do?WickdLotus– WickdLotus2017年09月22日 15:43:04 +00:00Commented Sep 22, 2017 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– jesse_b2017年09月22日 15:48:44 +00:00Commented Sep 22, 2017 at 15:48
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
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 yourCOUNTER
if a person entered5
it would work and then if they entered2
it would not, but2
is a valid positive integer no?