0

I want to create a number of variables equal to the lines in a file and assign to each of those variables a value from the file sequentially.

Say,
file1 contains device1 device2 device3 .....
file2 contains olddevice1 olddevice2 olddevice3 .....

I want values as when I do echo $A = device1 Similarly echo $B = device2 and echo $Z = device26

I tried a for loop, and even an array, but couldn't get through it.

I have tried something like below:

iin=0
var=({A..Z})
for jin in `cat file1`
do
   array[$iin]="$var=$jin";
   iin=$(($iin+1));
   var="$(echo $var | tr '[A-Y]Z' '[B-Z]A')"
   printf '%s\n' "${array[@]}"
done`
agc
  • 7,973
  • 2
  • 29
  • 50
Learner
  • 7
  • 2
  • I'm almost sure you cannot dynamically create variables. I do not have a source, but the compiler behaviour to assign a variable implies it is explicitly written, as it is then stored in a reference table. Otherwise (as your example shows) you would be assigning, not a variable (which will map to a memory cell), but a content (a letter, a string), and a data value cannot be a reference for a memory (heap, stack...) position. Correct me if I'me wrong – samthegolden Apr 01 '19 at 13:20
  • @samthegolden: The question is tagged `shell` and shows shell script code. Shell scripts are interpreted (at least in effect), and new variables can be created dynamically. – Eric Postpischil Apr 01 '19 at 15:00
  • Possible duplicate of [How can I generate new variable names on the fly in a shell script?](https://stackoverflow.com/questions/10820343/how-can-i-generate-new-variable-names-on-the-fly-in-a-shell-script) – samthegolden Apr 01 '19 at 17:04
  • Please describe what the `$var` variable is meant to accomplish. – agc Apr 01 '19 at 18:12

2 Answers2

0

I believe you're missing the point : variables have fix names in programming languages, like $A, $B, ..., $Z: while programming you need to specify those variables inside your program, you can't expect your program to invent it's own variables.

What you are looking for, are collections, like arrays, lists, ...:
You create a collection A and you can add values to it (A[n]=value_n, or A.SetAt(n, value_n), ..., depending on the kind of collection you're using.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • The question is tagged `shell` and shows shell script code. Shell scripts are interpreted (at least in effect), and new variables can be created dynamically. There may well be a better solution for OP’s actual problem that uses arrays or other features, but what they ask for is possible. – Eric Postpischil Apr 01 '19 at 15:02
0

With bash (v4 and later) something like this mapfile code should work:

mapfile -O 1 -t  array1   < file1  
mapfile -O 1 -t  array2   < file2 
# output line #2 from both files
echo "${array1[2]}" "${array2[2]}" 
# output the last line from both files
echo "${array1[-1]}" "${array2[-1]}" 

Notes: mapfile just loads an array, but with a few more options.

  • -O 1 sets the array subscript to start at 1 rather than the default 0; this isn't necessary, but it makes the code easier to read.
agc
  • 7,973
  • 2
  • 29
  • 50