bash - I need to get the amount of numbers entered from a parameter and assign them to variables -
i making program have user provide 3 digit number when running program. if not have 3 numbers, im give error message. how can test length of number , how can assign each number out of digits variable?
thanks in advance!
i tried doing: grep '^[0-9][0-9][0-9]$'
did not work.
consider following starting point. add more error checking getting practice.
#!/bin/bash while : read -p "enter 3 digit number or q quit: " input if (( input >= 100 && input <= 999)); echo "good entry" digit1=${input:0:1} && echo "digit1 $digit1" digit2=${input:1:1} && echo "digit2 $digit2" digit3=${input:2:1} && echo "digit3 $digit3" elif [[ $input == "q" ]]; break else echo "bad entry" fi done
output:
enter 3 digit number or q quit: 4256 bad entry enter 3 digit number or q quit: 242 entry digit1 2 digit2 4 digit3 2 enter 3 digit number or q quit: 562 entry digit1 5 digit2 6 digit3 2 enter 3 digit number or q quit: q ###program breaks here
Comments
Post a Comment