Variable that is varying itself processing [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash 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







share|improve this 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.


















    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







    share|improve this 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.
















      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







      share|improve this question















      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









      share|improve this question













      share|improve this question




      share|improve this question








      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.






















          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"





          share|improve this answer






















          • 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

















          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"





          share|improve this answer






















          • 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














          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"





          share|improve this answer






















          • 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












          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"





          share|improve this answer














          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"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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


          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?