Split string in ash shell? (BusyBox)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm trying to split a string in ash shell using only the tools provided in BusyBox.



The string is of the format host-name:port,host-name2:port2 but I only care about the first hostname and port, both of which I'd like access to as variables.



I've tried a few options I found on here but most pertain to bash and the equivalent commands/functionality doesn't seem to exist in ash shell.



Any ideas?










share|improve this question





















  • echo $string%,*
    – Ipor Sircer
    Aug 21 at 12:42










  • @IporSircer That got it, thanks!
    – Jamie4840
    Aug 21 at 13:03














up vote
0
down vote

favorite












I'm trying to split a string in ash shell using only the tools provided in BusyBox.



The string is of the format host-name:port,host-name2:port2 but I only care about the first hostname and port, both of which I'd like access to as variables.



I've tried a few options I found on here but most pertain to bash and the equivalent commands/functionality doesn't seem to exist in ash shell.



Any ideas?










share|improve this question





















  • echo $string%,*
    – Ipor Sircer
    Aug 21 at 12:42










  • @IporSircer That got it, thanks!
    – Jamie4840
    Aug 21 at 13:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to split a string in ash shell using only the tools provided in BusyBox.



The string is of the format host-name:port,host-name2:port2 but I only care about the first hostname and port, both of which I'd like access to as variables.



I've tried a few options I found on here but most pertain to bash and the equivalent commands/functionality doesn't seem to exist in ash shell.



Any ideas?










share|improve this question













I'm trying to split a string in ash shell using only the tools provided in BusyBox.



The string is of the format host-name:port,host-name2:port2 but I only care about the first hostname and port, both of which I'd like access to as variables.



I've tried a few options I found on here but most pertain to bash and the equivalent commands/functionality doesn't seem to exist in ash shell.



Any ideas?







shell busybox ash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 21 at 12:36









Jamie4840

1




1











  • echo $string%,*
    – Ipor Sircer
    Aug 21 at 12:42










  • @IporSircer That got it, thanks!
    – Jamie4840
    Aug 21 at 13:03
















  • echo $string%,*
    – Ipor Sircer
    Aug 21 at 12:42










  • @IporSircer That got it, thanks!
    – Jamie4840
    Aug 21 at 13:03















echo $string%,*
– Ipor Sircer
Aug 21 at 12:42




echo $string%,*
– Ipor Sircer
Aug 21 at 12:42












@IporSircer That got it, thanks!
– Jamie4840
Aug 21 at 13:03




@IporSircer That got it, thanks!
– Jamie4840
Aug 21 at 13:03










1 Answer
1






active

oldest

votes

















up vote
0
down vote













An agnostic (for many shells) solution that works correctly with spaces, newlines and empty fields is:



#!/bin/bash
in='One-XX-X-17.0.0'

a=$in; div='-'; set --
while
b=$a#*"$div"
set -- "$@" "$a%%"$div"*"
[ "$a" != "$b" ]
do
a=$b
done
printf 'Element: %sn' "$@"

#split 17.0.0 into NUM
a=$4; div='.'; set --
while
b=$a#*"$div"
set -- "$@" "$a%%"$div"*"
[ "$a" != "$b" ]
do
a=$b
done
printf 'Num: sn' "$@"


Which will print:



$ ./script
Element: One
Element: XX
Element: X
Element: 17.0.0
Num: 17
Num: 0
Num: 0





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463854%2fsplit-string-in-ash-shell-busybox%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    An agnostic (for many shells) solution that works correctly with spaces, newlines and empty fields is:



    #!/bin/bash
    in='One-XX-X-17.0.0'

    a=$in; div='-'; set --
    while
    b=$a#*"$div"
    set -- "$@" "$a%%"$div"*"
    [ "$a" != "$b" ]
    do
    a=$b
    done
    printf 'Element: %sn' "$@"

    #split 17.0.0 into NUM
    a=$4; div='.'; set --
    while
    b=$a#*"$div"
    set -- "$@" "$a%%"$div"*"
    [ "$a" != "$b" ]
    do
    a=$b
    done
    printf 'Num: sn' "$@"


    Which will print:



    $ ./script
    Element: One
    Element: XX
    Element: X
    Element: 17.0.0
    Num: 17
    Num: 0
    Num: 0





    share|improve this answer
























      up vote
      0
      down vote













      An agnostic (for many shells) solution that works correctly with spaces, newlines and empty fields is:



      #!/bin/bash
      in='One-XX-X-17.0.0'

      a=$in; div='-'; set --
      while
      b=$a#*"$div"
      set -- "$@" "$a%%"$div"*"
      [ "$a" != "$b" ]
      do
      a=$b
      done
      printf 'Element: %sn' "$@"

      #split 17.0.0 into NUM
      a=$4; div='.'; set --
      while
      b=$a#*"$div"
      set -- "$@" "$a%%"$div"*"
      [ "$a" != "$b" ]
      do
      a=$b
      done
      printf 'Num: sn' "$@"


      Which will print:



      $ ./script
      Element: One
      Element: XX
      Element: X
      Element: 17.0.0
      Num: 17
      Num: 0
      Num: 0





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        An agnostic (for many shells) solution that works correctly with spaces, newlines and empty fields is:



        #!/bin/bash
        in='One-XX-X-17.0.0'

        a=$in; div='-'; set --
        while
        b=$a#*"$div"
        set -- "$@" "$a%%"$div"*"
        [ "$a" != "$b" ]
        do
        a=$b
        done
        printf 'Element: %sn' "$@"

        #split 17.0.0 into NUM
        a=$4; div='.'; set --
        while
        b=$a#*"$div"
        set -- "$@" "$a%%"$div"*"
        [ "$a" != "$b" ]
        do
        a=$b
        done
        printf 'Num: sn' "$@"


        Which will print:



        $ ./script
        Element: One
        Element: XX
        Element: X
        Element: 17.0.0
        Num: 17
        Num: 0
        Num: 0





        share|improve this answer












        An agnostic (for many shells) solution that works correctly with spaces, newlines and empty fields is:



        #!/bin/bash
        in='One-XX-X-17.0.0'

        a=$in; div='-'; set --
        while
        b=$a#*"$div"
        set -- "$@" "$a%%"$div"*"
        [ "$a" != "$b" ]
        do
        a=$b
        done
        printf 'Element: %sn' "$@"

        #split 17.0.0 into NUM
        a=$4; div='.'; set --
        while
        b=$a#*"$div"
        set -- "$@" "$a%%"$div"*"
        [ "$a" != "$b" ]
        do
        a=$b
        done
        printf 'Num: sn' "$@"


        Which will print:



        $ ./script
        Element: One
        Element: XX
        Element: X
        Element: 17.0.0
        Num: 17
        Num: 0
        Num: 0






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 22 at 2:22









        Isaac

        7,1311835




        7,1311835



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463854%2fsplit-string-in-ash-shell-busybox%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay