LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-06-2018, 03:41 PM   #1
OreCollector
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 https://www.linuxquestions.org/quest...fs-4175422897/ but I can't get my version of the solution to work.

Any ideas?

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

    #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 $?:
Code:
[mdittmer@ops2 bin]$ . isvalidip
[mdittmer@ops2 bin]$
[mdittmer@ops2 bin]$ isvalidip 123.123.123.123; echo $?
1
[mdittmer@ops2 bin]$
 
Old 04-06-2018, 05:22 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

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

Code:
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
Code:
set -- ${1//./ }

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

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


Reply


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 06:12 AM
LXer: Bash: 10 Examples of Parameter Expansion LXer Syndicated Linux News 0 11-17-2014 05:41 AM
BASH:Rewrite a function using parameter expansion instead of changing IFS Mes9 Programming 7 09-26-2012 02:50 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 07:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration