Just do:
echo "Please enter a number "
read number
if ! test "$number" -gt 0 2> /dev/null; then
echo "You must enter an integer greater than 0" >&2
exit 1
fi
yes Name | sed ${number}q
But don't prompt for the number. Take it as a command line argument, and just do
yes Name | sed "${1}q"
Let sed generate the error message if the parameter is invalid.
The trouble with your loop is that for value in $number
takes the string $number
and breaks it on whitespace (depends on IFS, actually, but let's not get bogged down by details) and iterates over each value. That is, if $number
is the string 1 3 dog 5
, then the loop will iterate 4 times with $value
taking the values 1
, 3
, dog
, and 5
. If $number
is 7
, then the loop iterates exactly once. You could do for((i=0; i < $number; i++)); do ...
, but that does not generate any useful error message if $number is not an integer.