-1

I want to copy multiple lines from a file to a shell script (i.e. not an "ordinary" file).

For example:

  • source file A.txt
      'ab cd'
      'ef gh'
      'ij kl'
    
  • target script file script.sh
    ....
    ....
    list=(
      HERE I NEED TO COPY AND PASTE THE CONTENT FROM a.TXT file. 
     )
    
  • The result should look like this:
    ....
    ....
    list=(
      'ab cd'
      'ef gh'
      'ij kl'
    )
    

The shell script should copy and paste automatically the content from another file to a script file. Kindly help me to resolve this

3
  • Do I understand correct that you want the running script.sh to modify the file script.sh? As a shell script is basically text, this task is not different from modifying other text files. Do you expect the change to have an effect on the execution of the script? The latter is normally not the case unless you somehow re-run the script. How exactly should the script determine where to put the contents of A.txt? Replace every line that contains HERE I NEED TO COPY AND PASTE THE CONTENT FROM a.TXT file. with the contents of the file?
    – Bodo
    Jun 23 at 16:30
  • 1
    Which shell specifically? In bash, you can fill an array from a file using the readarray builtin with input redirection Jun 23 at 16:43
  • 1
    Is there any particular reason you want to modify the script file (possibly introducing a code injection vulnerability if the data in A.txt contains anything but nicely quoted lines). It would be trivial to write the script in such a way that it reads the contents of A.txt into the array list using readarray -t list <A.txt, provided you were using bash.
    – Kusalananda
    2 hours ago

1 Answer 1

0

How about

sed -e '/list=(/ {N; s/\n.*$//; r A.txt' -e '}' script.sh
    ....
    ....
    list=(
    'ab cd'
    'ef gh'
    'ij kl'
     )

If list(= is encountered, the next line is appended and deleted, then the A.txt file appended to output.

2
  • after added those lines in a script, script should execute it but the lines are deleting before executing the scripts
    – Beginner
    Jun 24 at 17:26
  • @Beginner Simply write the result of the above command to a new file and execute that.
    – Kusalananda
    1 hour ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.