Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-06-2018, 04:41 PM   #1
LQ Newbie
Registered: Jan 2017
Posts: 8

Rep: Reputation: Disabled
Rewrite bash function to use parameter expansion instead of custom IFS

I am trying to rewrite the isvalidip() from Pro Bash Programming to use parameter expansion as suggested by the exercise at the end of the chapter and I am stuck.

I found but I can't get my version of the solution to work.

Any ideas?

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 5 ;;

    #IFS=.  ## original code

    set "${1//./ }" ## my attempt at parameter expansion instead - for fun and science

    #set -- $1  ## more orignal code

    [ $# -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 ]

Execution attempt;I am expecting 0 for $?:
[mdittmer@ops2 bin]$ . isvalidip
[mdittmer@ops2 bin]$
[mdittmer@ops2 bin]$ isvalidip; echo $?
[mdittmer@ops2 bin]$
Old 04-06-2018, 06:22 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
The idea maybe is to replace dots with spaces so you end up with a numbers list separated with spaces.
With original IFS, you can assign this value into an array easily (autosplit with spaces)

ip_arr=(${1//./ })
From that you can count elements in $ip_arr, iterate values etc...

Oh sorry, I read too quickly. I overlooked you want to overwrite the arguments list...
I never did that, not my programming style but why not

Correct syntax would be
set -- ${1//./ }

Last edited by keefaz; 04-06-2018 at 06:39 PM.
Old 04-06-2018, 07:41 PM   #3
LQ Newbie
Registered: Jan 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
You are correct! My set syntax was off. Thank you very much!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: An introduction to parameter expansion in Bash LXer Syndicated Linux News 0 06-13-2017 07:12 AM
LXer: Bash: 10 Examples of Parameter Expansion LXer Syndicated Linux News 0 11-17-2014 06:41 AM
BASH:Rewrite a function using parameter expansion instead of changing IFS Mes9 Programming 7 09-26-2012 03:50 PM
LXer: Bash Parameter Expansion LXer Syndicated Linux News 0 10-02-2008 04:40 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:52 AM.

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