How to iterate through a set of variables, then expand the variables in Bash
up vote
0
down vote
favorite
I'm trying to set up a "check if machines are online" script with Bash, but running into an issue of when and where to define the variables so they're expanded properly. Something like:
#!/bin/bash
rm01="c01 c02 c03"
rm02="d01 d02 d03"
rm10="e11 e22 e33"
for room in rm01 rm02 rm03; do
echo $room
for computer in $room; do
#run various nslookup/ping tests and report
done
done
exit 0
I'm running into issues because I can't find a way to expand $room
for its corresponding set of computers (in $rm01
, $rm02
, $rm10
) listed at the beginning.
What am I doing wrong?
bash variables iteration
add a comment |
up vote
0
down vote
favorite
I'm trying to set up a "check if machines are online" script with Bash, but running into an issue of when and where to define the variables so they're expanded properly. Something like:
#!/bin/bash
rm01="c01 c02 c03"
rm02="d01 d02 d03"
rm10="e11 e22 e33"
for room in rm01 rm02 rm03; do
echo $room
for computer in $room; do
#run various nslookup/ping tests and report
done
done
exit 0
I'm running into issues because I can't find a way to expand $room
for its corresponding set of computers (in $rm01
, $rm02
, $rm10
) listed at the beginning.
What am I doing wrong?
bash variables iteration
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to set up a "check if machines are online" script with Bash, but running into an issue of when and where to define the variables so they're expanded properly. Something like:
#!/bin/bash
rm01="c01 c02 c03"
rm02="d01 d02 d03"
rm10="e11 e22 e33"
for room in rm01 rm02 rm03; do
echo $room
for computer in $room; do
#run various nslookup/ping tests and report
done
done
exit 0
I'm running into issues because I can't find a way to expand $room
for its corresponding set of computers (in $rm01
, $rm02
, $rm10
) listed at the beginning.
What am I doing wrong?
bash variables iteration
I'm trying to set up a "check if machines are online" script with Bash, but running into an issue of when and where to define the variables so they're expanded properly. Something like:
#!/bin/bash
rm01="c01 c02 c03"
rm02="d01 d02 d03"
rm10="e11 e22 e33"
for room in rm01 rm02 rm03; do
echo $room
for computer in $room; do
#run various nslookup/ping tests and report
done
done
exit 0
I'm running into issues because I can't find a way to expand $room
for its corresponding set of computers (in $rm01
, $rm02
, $rm10
) listed at the beginning.
What am I doing wrong?
bash variables iteration
bash variables iteration
edited 3 hours ago
Benjamin W.
19.7k124554
19.7k124554
asked 4 hours ago
phonedog365
133
133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The quick fix is to use variable indirection:
for computer in ${!room}; do
Relying on word splitting is rarely the best idea, though. You could use arrays and namerefs instead (requires Bash 4.3 or newer):
#!/usr/bin/env bash
# Declare arrays
rm01=(c01 c02 c03)
rm02=(d01 d02 d03)
rm03=(e11 e22 e33)
# Declare room as nameref
declare -n room
# Using nameref as control variable sets room as reference to each variable in turn
for room in rm{01..03}; do
# Properly quoted array expansion
for computer in "${room[@]}"; do
echo "$computer" # or whatever needs to be done
done
done
exit 0
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The quick fix is to use variable indirection:
for computer in ${!room}; do
Relying on word splitting is rarely the best idea, though. You could use arrays and namerefs instead (requires Bash 4.3 or newer):
#!/usr/bin/env bash
# Declare arrays
rm01=(c01 c02 c03)
rm02=(d01 d02 d03)
rm03=(e11 e22 e33)
# Declare room as nameref
declare -n room
# Using nameref as control variable sets room as reference to each variable in turn
for room in rm{01..03}; do
# Properly quoted array expansion
for computer in "${room[@]}"; do
echo "$computer" # or whatever needs to be done
done
done
exit 0
add a comment |
up vote
1
down vote
accepted
The quick fix is to use variable indirection:
for computer in ${!room}; do
Relying on word splitting is rarely the best idea, though. You could use arrays and namerefs instead (requires Bash 4.3 or newer):
#!/usr/bin/env bash
# Declare arrays
rm01=(c01 c02 c03)
rm02=(d01 d02 d03)
rm03=(e11 e22 e33)
# Declare room as nameref
declare -n room
# Using nameref as control variable sets room as reference to each variable in turn
for room in rm{01..03}; do
# Properly quoted array expansion
for computer in "${room[@]}"; do
echo "$computer" # or whatever needs to be done
done
done
exit 0
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The quick fix is to use variable indirection:
for computer in ${!room}; do
Relying on word splitting is rarely the best idea, though. You could use arrays and namerefs instead (requires Bash 4.3 or newer):
#!/usr/bin/env bash
# Declare arrays
rm01=(c01 c02 c03)
rm02=(d01 d02 d03)
rm03=(e11 e22 e33)
# Declare room as nameref
declare -n room
# Using nameref as control variable sets room as reference to each variable in turn
for room in rm{01..03}; do
# Properly quoted array expansion
for computer in "${room[@]}"; do
echo "$computer" # or whatever needs to be done
done
done
exit 0
The quick fix is to use variable indirection:
for computer in ${!room}; do
Relying on word splitting is rarely the best idea, though. You could use arrays and namerefs instead (requires Bash 4.3 or newer):
#!/usr/bin/env bash
# Declare arrays
rm01=(c01 c02 c03)
rm02=(d01 d02 d03)
rm03=(e11 e22 e33)
# Declare room as nameref
declare -n room
# Using nameref as control variable sets room as reference to each variable in turn
for room in rm{01..03}; do
# Properly quoted array expansion
for computer in "${room[@]}"; do
echo "$computer" # or whatever needs to be done
done
done
exit 0
edited 3 hours ago
answered 3 hours ago
Benjamin W.
19.7k124554
19.7k124554
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415559%2fhow-to-iterate-through-a-set-of-variables-then-expand-the-variables-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown