When dealing with user input, you may want to give user a default value, which often is used when user just pressing Enter for empty value. Often you will see a prompt like:
number [100]: Delete file [y/N]?
It is sort of convention, so users understand what those mean. But it will require a line or two to check and update the value. The read provides a better way for default input value:
read -p "number: " -i 100 -e value
You can put the value to different variable, so you can check before update:
value=50 read -p "number: " -i $value -e new_value (( new_value > 100 )) && new_value=100 (( new_value < 0 )) && new_value=0 value=$new_value
You can put the input process inside of a loop and feed the current value to read, so user can simply press Enter to keep the value or edit for new value.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.