|
|
Question : Bash scripting question - Creating a Loop and using a 'dynamic' variable
|
|
Bash Script Question I want to write a short clean script that will have just one main body doing all the work, but will parse through 70+ times using a loop. I want to set up variables (process1, proccess2, etc) that will be referenced by the main body of the script; cycling through another process each time. Currently my idea is setup a loop and find a way to tie the 'count' of the loop against a variable that will correctly populate the value of all those 'process' varriables. First am I approaching it the right way; second if not please provide sugestions and an example... . I will provide a bit more of a detailed example below: Here's what I was thinking:
#!/bin/bash COUNTER=0 while [ $COUNTER -lt 3 ]; do let COUNTER=COUNTER+1 PROCESS=PROCESS$COUNTER PROCESS0=00000 PROCESS1=11111 PROCESS2=22222 echo $PROCESS done
So no matter what order I state my variables; I never get $PROCESS to equal 00000, 11111, 22222 Ultimately; once I get that far, I will create the body of my script and reference a single variable or command that will parse a different 'PROCESS' value each iteration through the loop; thus saving me from having to have a script so large it becomes unmangeable.
|
Answer : Bash scripting question - Creating a Loop and using a 'dynamic' variable
|
|
#!/bin/bash PROCESS0=00000 PROCESS1=11111 PROCESS2=22222 COUNTER=0 while [ $COUNTER -lt 3 ]; do PROCESS=PROCESS$COUNTER echo ${!PROCESS} let COUNTER=COUNTER+1 done
|
|
|
|
|