0

How to read line with variable from file and assign variable through a loop? My file has line xyz_$loop, now my script has to read line from file xyz_$loop and then while looping print xyz_1, xyz_2, xyz_3, etc. I am using shell script my file URL1 contain loop_$i my script -->

while read p; do
  max=10
  for (( i=2; i <= $max; ++i )) do
    echo $p
  done
done <URL1

print following output

loop_$i
loop_$i
loop_$i

but I need output

loop_2
loop_3
loop_4
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Show some code or we have no idea what you are talking about. –  Oct 10 '17 at 17:47
  • my file URL1 contain loop_$i my script while read p; do max=10 for (( i=2; i <= $max; ++i )) do echo $p done done – Vikash Singh Oct 10 '17 at 18:21
  • This should be in the body of the question. [Edit the question](https://stackoverflow.com/posts/46672795/edit) and show what you have done. –  Oct 10 '17 at 19:02
  • It's not at all clear how you're getting the output you claim from the code you provided. That said, *in general*, we already have a lot of questions that cover the ground of indirect variable assignment and expansion in depth; it's not at all clear that what you're asking presents something new. – Charles Duffy Oct 10 '17 at 19:55
  • ...if you *do* have a new question that isn't covered in the dupes, please edit your code to comply with the [mcve] definition, so folks can reproduce the behavior themselves (via copy-and-paste). – Charles Duffy Oct 10 '17 at 19:57

1 Answers1

-1

You will probably want to replace '$loop' with the index of your loop

// read your file    

$i = 0;
foreach($fileLines as $fileLine)
{
    $fileLine = str_replace('$loop', $i, $fileLine);

    $i++;
}

Edit: I did make the assumption that you are using php.

Adam Rodriguez
  • 1,850
  • 1
  • 12
  • 15
  • I am using shell script my file URL1 contain loop_$i my script while read p; do max=10 for (( i=2; i <= $max; ++i )) do echo $p done done – Vikash Singh Oct 10 '17 at 18:24
  • 1
    The question is tagged "shell" and this looks like some variant of the Bourne shell. –  Oct 10 '17 at 19:03