I am trying to add to a script I’m writing a restriction to the input where the user can only input 9 or 10 characters I set it up like this but when I run it, it doesn’t work the way I want it to. Everything I type in comes back as 10 characters even if I just put one number. What is wrong with my code?
#!/bin/bash
#
echo "Please input numbers only"
read inputline
if read -n10 inputline
then
echo "10 chars"
else
if read -n9 inputline
then
echo "9 chars"
else
echo "invalid input length"
fi
Your script is asking for input three times. I’m assuming that the following is closer to what you intend:
The
-noption toreadlimits the input to the specified number of characters but accepts that length without pressing enter. You can enter fewer by pressing Enter though. I’ve found it to be useful for-n 1but rarely otherwise.