Split string in ash shell? (BusyBox)
Clash 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?
shell busybox ash
add a comment |Â
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?
shell busybox ash
echo $string%,*
â Ipor Sircer
Aug 21 at 12:42
@IporSircer That got it, thanks!
â Jamie4840
Aug 21 at 13:03
add a comment |Â
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?
shell busybox ash
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
shell busybox ash
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Aug 22 at 2:22
Isaac
7,1311835
7,1311835
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
echo $string%,*
â Ipor Sircer
Aug 21 at 12:42
@IporSircer That got it, thanks!
â Jamie4840
Aug 21 at 13:03