BASH:Rewrite a function using parameter expansion instead of changing IFS
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH:Rewrite a function using parameter expansion instead of changing IFS
I am learning bash from a book and im stuck at this exercise from the current chapter.
The exercise is: rewrite function isvalidip using parameter expansion instead of changing IFS.
Here is the funtion:
Code:
isvalidip() #@ USAGE: isvalidip DOTTED-QUAD
{
case $1 in
## reject the following:
## empty string
## anything other than digits and dots
## anything not ending in a digit
"" | *[!0-9.]* | *[!0-9]) return 1 ;;
esac
## Change IFS to a dot, but only in this function
local IFS=.
## Place the IP address into the positional parameters;
## after word splitting each element becomes a parameter
set -- $1
[ $# -eq 4 ] && ## must be four parameters
## each must be less than 256
# A default of 666 (which is invalid) is used if a parameter is empty
# All four parameters must pass the test
[ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] &&
[ ${3:-666} -le 255 ] && [ ${4:-666} -le 255 ]
}
Here is what I've done so far(knowing this has probably nothing to do with the solution.
Code:
pre=:
post=:
#var=192.168.0.123
if [ -z "$*" ]
then
printf "You need to type command line arguments\n" >&2
exit 1
else
for var in $@
do
printf "$pre%s$post\n" "$var"
##printf "$pre%s$post\n" "${var:4:3}"
done
fi
unset var
If your code has nothing to do with the solution, why do you post it ?
If I read correctly, you must write a function, so you could do, at least something like this:
Code:
function anotherIsValidIp() # anotherIsValidIp DOTTED-QUAD
{
case $1 in
# ...
# I let you copy
esac
myIp="$1"
## Some other nice stuff and that's where I can't figure what to do
}
So, now is the difficult part. It is an exercice, so you probably have to read your book. But you have an answer in the bash man
Code:
$ man bash
You can find it at chapter Parameter Expansion, and particularly at the definition of ${parameter#word}, ${parameter##word}, or ${parameter%word} or ${parameter%%word}
Look at your description (in your book) of ${parameter//pattern/string} construct, and consider what would happen if your function called another with an argument in that form. (There's no reason why a function you write can't call another function that you also write.)
It seems to me that the requirement is...you have an ip address string, and you need to separate this string into four sections. Now, using parameter substitution, how do you get each of the four sections?
So just complete the ${}'s, below. (Hint, it will probably actually take two steps each to get the middle two.)
Then it's just a matter of looping through them for the final part of the function. Although actually it would be easier to store them in an array rather than four separate variables.
Incidentally, a cleaner way would be to still use IFS, but with read instead:
Code:
ip='123.45.54.321'
IFS=. read -a slice <<<"$ip"
printf '%s\n' "${slice[@]}"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.