Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (*?[]) will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on itExpansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shellsSecurity implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

This would use | as a separator and split the string. You could use any character not needed in the values, maybe even some control character like \001 (Use $'\001' in Bash to create it).

#!/bin/bash
string='foo|bar'
IFS='|'; set -f
array=($string)        # split it

IFS='|'
string="${array[*]}"   # merge it back together

Also, like they say in the comments, if your variable only has hostnames and you are fine with splitting on whitespace, you don't need the awk to split it. The shell will do it when assigning to array or starting the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (*?[]) will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

This would use | as a separator and split the string. You could use any character not needed in the values, maybe even some control character like \001 (Use $'\001' in Bash to create it).

#!/bin/bash
string='foo|bar'
IFS='|'; set -f
array=($string)        # split it

IFS='|'
string="${array[*]}"   # merge it back together

Also, like they say in the comments, if your variable only has hostnames and you are fine with splitting on whitespace, you don't need the awk to split it. The shell will do it when assigning to array or starting the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (*?[]) will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

This would use | as a separator and split the string. You could use any character not needed in the values, maybe even some control character like \001 (Use $'\001' in Bash to create it).

#!/bin/bash
string='foo|bar'
IFS='|'; set -f
array=($string)        # split it

IFS='|'
string="${array[*]}"   # merge it back together

Also, like they say in the comments, if your variable only has hostnames and you are fine with splitting on whitespace, you don't need the awk to split it. The shell will do it when assigning to array or starting the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

added 430 characters in body
Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (`*?[]'*?[]) will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

(I'll get back to implementing that) This would use | as a separator and split the string. You could use any character not needed in the values, maybe even some control character like \001 (Use $'\001' in Bash to create it).

#!/bin/bash
string='foo|bar'
IFS='|'; set -f
array=($string)        # split it

IFS='|'
string="${array[*]}"   # merge it back together

Also, like they say in the comments, you can just let the shell splitif your variable only has hostnames and you are fine with splitting on whitespace, or useyou don't need the awk to split it directly in. The shell will do it when assigning to array or starting the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (`*?[]') will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

(I'll get back to implementing that)

Also, like they say in the comments, you can just let the shell split your variable, or use it directly in the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (*?[]) will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

This would use | as a separator and split the string. You could use any character not needed in the values, maybe even some control character like \001 (Use $'\001' in Bash to create it).

#!/bin/bash
string='foo|bar'
IFS='|'; set -f
array=($string)        # split it

IFS='|'
string="${array[*]}"   # merge it back together

Also, like they say in the comments, if your variable only has hostnames and you are fine with splitting on whitespace, you don't need the awk to split it. The shell will do it when assigning to array or starting the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

edited body
Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (`*?[]') will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set +f-f.

(I'll get back to implementing that)

Also, like they say in the comments, you can just let the shell split your variable, or use it directly in the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (`*?[]') will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set +f.

(I'll get back to implementing that)

Also, like they say in the comments, you can just let the shell split your variable, or use it directly in the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

The problems you might face are the usual ones: splitting and globbing.

You can't have strings with whitespace, since they'll be split to separate array items, and anything resembling a glob character (`*?[]') will be expanded to matching filenames. Probably not a problem with host names though, but in general, it is.

This is a common issue, at repeatedly discussed, see e.g. Expansion of a shell variable and effect of glob and split on it and Security implications of forgetting to quote a variable in bash/POSIX shells


To work around those, you'd need to a) set IFS to something other than whitespace and separate your strings with that, and b) disable globbing with set -f.

(I'll get back to implementing that)

Also, like they say in the comments, you can just let the shell split your variable, or use it directly in the loop:

SRVR_ARRAY=( $HOST_NAMES )
for x in $HOST_NAMES ; do ...

(Again, you'll get trouble if HOST_NAMES contains glob characters.)

Actually, that's what is going to happen even with your example: awk prints something, the shell catches it, splits it on words, since the $() was not inside double-quotes, and then saves the words separately in the array.

Source Link
ilkkachu
  • 147.8k
  • 16
  • 268
  • 441
Loading