LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2012, 11:10 AM   #1
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Rep: Reputation: 0
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

Last edited by Mes9; 08-19-2012 at 12:16 PM.
 
Old 08-20-2012, 08:37 AM   #2
rg421
LQ Newbie
 
Registered: May 2009
Location: Strasbourg
Distribution: Gentoo, Debian, Ubuntu, MacOsX
Posts: 2

Rep: Reputation: 0
Hello,

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}

Good luck,
rg
 
Old 08-20-2012, 01:48 PM   #3
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Original Poster
Rep: Reputation: 0
My understanding is that it has to do with word-splitting.
 
Old 08-20-2012, 02:15 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.)

Oh, and the doubled / is not a typo.
 
Old 08-20-2012, 03:43 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.)
Code:
ip='123.45.54.321'

first=${}
second=${}
third=${}
fourth=${}

echo "$first $second $third $fourth"
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[@]}"
 
Old 09-05-2012, 03:04 PM   #6
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Original Poster
Rep: Reputation: 0
Thanks

Thank you all for the reply's. Think it is going to take some time to work through this.
 
Old 09-26-2012, 02:37 PM   #7
Mes9
Member
 
Registered: Jan 2012
Distribution: Fedora 17
Posts: 38

Original Poster
Rep: Reputation: 0
mark thread as solved?

Quote:
Originally Posted by Mes9 View Post
Thank you all for the reply's. Think it is going to take some time to work through this.
I am going through some other books, so it will be awhile until I go back to this question, thank you again for the feedback.
 
Old 09-26-2012, 02:50 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
"Splitting a string into fields" section at http://mywiki.wooledge.org/BashFAQ/100?

Edit: err, maybe not. ("instead of changing IFS")
I'm too eager to help sometimes.

Last edited by Habitual; 09-26-2012 at 02:57 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: when an empty IFS does not work like a default IFS (info) catkin Programming 13 04-19-2012 09:40 AM
[SOLVED] Bash: extended regex pattern 'NOT' disabled inside parameter expansion? romagnolo Linux - General 4 02-22-2012 04:39 PM
Does bash shell parameter expansion run the risk of overflowing command line buffer? david1941 Linux - Software 1 04-28-2009 10:13 PM
LXer: Bash Parameter Expansion LXer Syndicated Linux News 0 10-02-2008 03:40 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration