-1

Please see the code below, the idea is that I need the third line as it is because I declare several input_${k} and fill them with a lot of values (so I iterate over k). I kept it simple for you by isolating 1 variable here and only filling it with '1'. My only problem is that the node.py line does not receive the content of input_1_128 ( here a user input is simulated using <<< ), but 'input_1_128: command not found'. Here I need it to send 1 to the python script. Can you guys help me please?

declare input_2_128='' 
k=1
declare input_${k}_128+='1'
python3 node.py <<< $(input_${k}_128)

Please note that if I write

echo $(input_1_128)

It will show the correct value (1) but I must use the first version (using $k). I type 'bash myscript.sh' to run the script if that is relevant here. I thank you all for your help. Of course do not hesitate to ask for more details

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
aveillon
  • 23
  • 4
  • Use a Bash [array](https://www.gnu.org/software/bash/manual/bash.html#Arrays) to work with arrays! (See also [Shell parameter expansion](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion).) – Jonathan Leffler Mar 15 '21 at 04:00
  • 1
    Most people don't have a command `input_1_128` on their system. The notation `$(input_${k}_128)` executes a command of that name and captures the standard output. Also, the output of `echo $(input_1_128)` is not likely to show the correct output — using `echo ${input_1_128}` probably would. Be very careful with spaces, quotes and brackets in shell scripts. – Jonathan Leffler Mar 15 '21 at 04:04
  • @JonathanLeffler in 4 years of CS studies, its the first time I hear about Bash arrays lol. thank you very much! However input_1_128 is not a command, its a variable. sorry if that was unclear or if Im missing your point – aveillon Mar 15 '21 at 04:04
  • 1
    Then it's time you read the [manual](https://www.gnu.org/software/bash/manual/bash.html) — top to bottom. Well, you could skip chapters from 7 onwards and the appendices, but you can't make full use of Bash without knowing much of what's in chapters 1 to 6. If nothing else, you need to know what's in the sub-section titles. I know you think `input_1_128` is a variable. But you're using the wrong brackets for the expression to be treated as a variable. You have `$(input_1_128)`; you need `${input_1_128}`. The difference is crucial! – Jonathan Leffler Mar 15 '21 at 04:11

1 Answers1

0

First a couple of answers of your original question (indirect and nameref) and what I suggest you do instead (array):

  1. Indirect access with the syntax ${!...}:
$ k=1
$ declare input_${k}_128=1

$ v=input_${k}_128
$ echo ${!v}
1

$ declare input_${k}_128=$((${!v} + 1))
$ echo ${!v}
2
  1. Nameref:
$ k=1
$ declare input_${k}_128=1

$ declare -n v=input_${k}_128
$ echo $v
1

$ v=$(($v + 1)); echo $v; echo $input_1_128
2
2
  1. Associate array:
$ declare -A a
$ a[input_${k}_128]=1
$ echo ${a[input_${k}_128]}
1

$ a[input_${k}_128]+=1
$ echo ${a[input_${k}_128]}
11

$ # if you mark the variable an integer you can use += 1
$ declare -i a[input_${k}_128]
$ a[input_${k}_128]+=1
$ echo ${a[input_${k}_128]}
12
Allan Wind
  • 23,068
  • 5
  • 28
  • 38