pohe
pohe

Reputation: 119

bash script that reads each line of a file and stores to different variables (but got command not found message)

Thanks for your help in advance. I'm writing a simple bash script, which just read each line of a file, and then store each line into a different variable. For example, I'd like the script to perform the following commands:

d1=AER
d2=BHR
d3=CEF
...

Therefore, I have a text file that has 10 lines, and each line is the content of the variables I'd like to store (e.g., AER), and I have the following test.sh script:

#!/bin/bash
for i in {1..10..1}
do
    $(d$i=$(sed -n ${i}p $HOME/textfile.txt))
done

However,when executing the script, it gave me

./test.sh: line 4: d1=AER: command not found
./test.sh: line 4: d2=BHR: command not found
./test.sh: line 4: d3=CEF: command not found
...

,instead of storing the characters into corresponding variables. Could somebody please identify where I did it wrong? Thank you so much!

Upvotes: 1

Views: 7409

Answers (3)

Jake
Jake

Reputation: 2236

The problem with your script is that if you need to interpolate a variable on the first line, bash thinks its the name of a program and tries to execute it. You can eval it, to make the variable:

eval "d$i=$(sed ...)"

but its much simpler to use the read bash built-in, since it takes line input to a specified variable. Here's one way to do it:

for ((i=1;; i++)); do
    read "d$i" || break;
done < textfile.txt
echo "$d1"
echo "$d2"

Upvotes: 6

twalberg
twalberg

Reputation: 62439

As an alternative to the array solution, you can use eval.

((lnum=1))
exec 3< ${filetoread}
while read -u3 line
do
  eval `echo d${lnum}=\"${line}\"`
  ((lnum=1+${lnum}))
done
exec 3<&-

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799082

Use an array, but the right way.

read -a d -d '\n' < textfile.txt
echo "${d[1]}"

Upvotes: 4

Related Questions