In the header of my bash script, I have to ask for the user name and his IP address.
I need to validate entries in this way:
- The user name have to be stored in a
namevariable, having his space replaced by underscores. For sample if user hit"Albert Smith", I have to store"Albert_Smith"into my variablename. - For the ip address, I need to know if the ip address is consistent with the format.
If the format is not good, I must report the error and prompt the user to enter a good ip address to continue the operation.
I know that i need to use “read”, but i don’t know to realize my short script (replace and verify).
Thank in advance 🙂
I suppose your entries will be line based (ie. the name will be the first line and the IP the second). You can do this:
The IP regex is rather complex, but it can be decomposed as:
The loop forms will negate
!the result of thegrep, so it will continue looping while thegrepcommand fails. We useprintfin order to pass the variable intogrep. The-Eflag on the secondgrepallows the use of extended regular expressions, which include the|OR operator,()grouping and{}repetitions.Hope this helps a little =)