Skip to main content
edited body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval IPv4_all=( "$(set |
    grep -E '^IPv4_[_[:alnum:]]*=\]]*=([^(]\|$\]|$)' |
    sed 's/\([^=]*\).*/${\1+"$\1"} /')"
)

grep only gets safe lines that match your target var. sed surrounds them in parameter expansion tokens so they evaluate away to nothing if they're not actually current shell variable names.

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval IPv4_all=( "$(set |
    grep '^IPv4_[_[:alnum:]]*=\([^(]\|$\)' |
    sed 's/\([^=]*\).*/${\1+"$\1"} /')"
)

grep only gets safe lines that match your target var. sed surrounds them in parameter expansion tokens so they evaluate away to nothing if they're not actually current shell variable names.

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval IPv4_all=( "$(set |
    grep -E '^IPv4_[_[:alnum:]]*=([^(]|$)' |
    sed 's/\([^=]*\).*/${\1+"$\1"} /')"
)

grep only gets safe lines that match your target var. sed surrounds them in parameter expansion tokens so they evaluate away to nothing if they're not actually current shell variable names.

deleted 84 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval "IPv4_all=IPv4_all=( "$( set |                               
    tr -dcgrep '['^IPv4_[_[:alnum:]_\n=']]*=\([^(]\|$\)' | 
    sed '/=.*/!d;s///;s's/^IPv4_\([^=]*\).*/${&+"$&"\1+"$\1"} /p;d'
')"  
)"

trgrep ensures that no characters are returned that could not exist in a variable name (excepting =\n which are retained as delimiters) toonly gets safe the eval statement for reparselines that match your target var. And the shell ${expands+}sed any false positivessurrounds them in parameter expansion tokens so they evaluate away to nothing if they're not actually current shell variable names.

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval "IPv4_all=( "$( set |                               
    tr -dc '[:alnum:]_\n=' | 
    sed '/=.*/!d;s///;s/^IPv4_.*/${&+"$&"} /p;d'
)" )"

tr ensures that no characters are returned that could not exist in a variable name (excepting =\n which are retained as delimiters) to safe the eval statement for reparse. And the shell ${expands+} any false positives away to nothing.

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval IPv4_all=( "$(set |
    grep '^IPv4_[_[:alnum:]]*=\([^(]\|$\)' |
    sed 's/\([^=]*\).*/${\1+"$\1"} /')" 
)

grep only gets safe lines that match your target var. sed surrounds them in parameter expansion tokens so they evaluate away to nothing if they're not actually current shell variable names.

added 120 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval "IPv4_all=( "$( set |                               
sed 's/^\(IPv4_[[   tr -dc '[:alnum:]]*\)=[^(]]_\n=' | 
    sed '/=.*/"$!d;s///;s/^IPv4_.*/${\1-&+"$&"}" /p;d'
)" )"

tr ensures that no characters are returned that could not exist in a variable name (excepting =\n which are retained as delimiters) to safe the eval statement for reparse. And the shell ${expands+} any false positives away to nothing.

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval "IPv4_all=( "$(set |
sed 's/^\(IPv4_[[:alnum:]]*\)=[^(].*/"${\1-}" /p;d'
)" )"

This works for me, I think, though I'll accept that I could be missing some fundamental point:

IPv4_first=1.1.1.1
IPv4_second=2.2.2.2
IPv4_third=3.3.3.3

IPv4_all=( $(set | sed '/IPv4_.*[=)]/!d;s///') )

printf "'%s'\n" "${IPv4_all[@]}"

###OUTPUT###

'1.1.1.1'
'2.2.2.2'
'3.3.3.3'

This is better:

eval "IPv4_all=( "$( set |                               
    tr -dc '[:alnum:]_\n=' | 
    sed '/=.*/!d;s///;s/^IPv4_.*/${&+"$&"} /p;d'
)" )"

tr ensures that no characters are returned that could not exist in a variable name (excepting =\n which are retained as delimiters) to safe the eval statement for reparse. And the shell ${expands+} any false positives away to nothing.

added 120 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading