Variable that is varying itself processing [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
creating variable using variable value as part of new variable name
4 answers
i want to write a shell script
cd /dev
for hdd in sd*; do
[ -f "$hdd" ] || continue
status_$hdd=$(my_def_get_hddstaus "$hdd") #my_def_get_hddstaus returns OK or FAIL randomly just to test
done
i get error like
status_sda0=OK: command not found
status_sda1=FAIL: command not found
I want to record the value OK or Fail into these variable, I am using bash,
what I am doing wrong.
if I write status_sda0=OK
in shell it record OK into status_sda0
shell-script shell
marked as duplicate by don_crissti, Jeff Schaller, GAD3R, Stephen Rauch, Romeo Ninov Oct 29 '17 at 18:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
creating variable using variable value as part of new variable name
4 answers
i want to write a shell script
cd /dev
for hdd in sd*; do
[ -f "$hdd" ] || continue
status_$hdd=$(my_def_get_hddstaus "$hdd") #my_def_get_hddstaus returns OK or FAIL randomly just to test
done
i get error like
status_sda0=OK: command not found
status_sda1=FAIL: command not found
I want to record the value OK or Fail into these variable, I am using bash,
what I am doing wrong.
if I write status_sda0=OK
in shell it record OK into status_sda0
shell-script shell
marked as duplicate by don_crissti, Jeff Schaller, GAD3R, Stephen Rauch, Romeo Ninov Oct 29 '17 at 18:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
creating variable using variable value as part of new variable name
4 answers
i want to write a shell script
cd /dev
for hdd in sd*; do
[ -f "$hdd" ] || continue
status_$hdd=$(my_def_get_hddstaus "$hdd") #my_def_get_hddstaus returns OK or FAIL randomly just to test
done
i get error like
status_sda0=OK: command not found
status_sda1=FAIL: command not found
I want to record the value OK or Fail into these variable, I am using bash,
what I am doing wrong.
if I write status_sda0=OK
in shell it record OK into status_sda0
shell-script shell
This question already has an answer here:
creating variable using variable value as part of new variable name
4 answers
i want to write a shell script
cd /dev
for hdd in sd*; do
[ -f "$hdd" ] || continue
status_$hdd=$(my_def_get_hddstaus "$hdd") #my_def_get_hddstaus returns OK or FAIL randomly just to test
done
i get error like
status_sda0=OK: command not found
status_sda1=FAIL: command not found
I want to record the value OK or Fail into these variable, I am using bash,
what I am doing wrong.
if I write status_sda0=OK
in shell it record OK into status_sda0
This question already has an answer here:
creating variable using variable value as part of new variable name
4 answers
shell-script shell
edited Oct 29 '17 at 12:04
asked Oct 29 '17 at 11:57
user2642486
103
103
marked as duplicate by don_crissti, Jeff Schaller, GAD3R, Stephen Rauch, Romeo Ninov Oct 29 '17 at 18:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by don_crissti, Jeff Schaller, GAD3R, Stephen Rauch, Romeo Ninov Oct 29 '17 at 18:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
If the variable name is not a static string (or rather: If the part before the =
contains anything not allowed in a variable name) then the assignment is not recognized as such.
You need eval
:
tmp_var="$(my_def_get_hddstaus "$hdd")"
eval status_$hdd=""$tmp_var""
edit
You can echo
the value using eval
again or using indirection:
eval echo "$status_$hdd"
or
var_name="status_$hdd"
echo "$!var_name"
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
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
accepted
If the variable name is not a static string (or rather: If the part before the =
contains anything not allowed in a variable name) then the assignment is not recognized as such.
You need eval
:
tmp_var="$(my_def_get_hddstaus "$hdd")"
eval status_$hdd=""$tmp_var""
edit
You can echo
the value using eval
again or using indirection:
eval echo "$status_$hdd"
or
var_name="status_$hdd"
echo "$!var_name"
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
add a comment |Â
up vote
0
down vote
accepted
If the variable name is not a static string (or rather: If the part before the =
contains anything not allowed in a variable name) then the assignment is not recognized as such.
You need eval
:
tmp_var="$(my_def_get_hddstaus "$hdd")"
eval status_$hdd=""$tmp_var""
edit
You can echo
the value using eval
again or using indirection:
eval echo "$status_$hdd"
or
var_name="status_$hdd"
echo "$!var_name"
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If the variable name is not a static string (or rather: If the part before the =
contains anything not allowed in a variable name) then the assignment is not recognized as such.
You need eval
:
tmp_var="$(my_def_get_hddstaus "$hdd")"
eval status_$hdd=""$tmp_var""
edit
You can echo
the value using eval
again or using indirection:
eval echo "$status_$hdd"
or
var_name="status_$hdd"
echo "$!var_name"
If the variable name is not a static string (or rather: If the part before the =
contains anything not allowed in a variable name) then the assignment is not recognized as such.
You need eval
:
tmp_var="$(my_def_get_hddstaus "$hdd")"
eval status_$hdd=""$tmp_var""
edit
You can echo
the value using eval
again or using indirection:
eval echo "$status_$hdd"
or
var_name="status_$hdd"
echo "$!var_name"
edited Oct 29 '17 at 13:46
answered Oct 29 '17 at 12:14
Hauke Laging
53.6k1282130
53.6k1282130
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
add a comment |Â
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
but now how would echo the value of variable dynamically $(status_$hdd). This is not working, Thanks for above solution atleast.
â user2642486
Oct 29 '17 at 13:37
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
@user2642486 See my edit
â Hauke Laging
Oct 29 '17 at 13:46
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
unix.stackexchange.com/questions/98419/⦠I found in reference too eval "echo "$filemsg$word1"" Thanks again
â user2642486
Oct 29 '17 at 13:49
add a comment |Â