1

I'm trying to read a txt file which has new lines, for example this:

#!/usr/bin/env sh
# Hi, this is a txt file that I will make a printf
# and I will execute later

VARIABLE_A=%s
VARIABLE_B=%s
VARIABLE_C=%s

so, this is my file txt_script.sh

and what I'm trying to do, is:

  1. Making my own bash script.
  2. Read txt_script.sh in a variable as text.
  3. Use printf to change those %s
  4. Write it in a new file.

My code is:

#!/bin/bash

create_script=`cat model_files/txt_script.sh`

create_script=`printf "$create_script" $1 $2 $3`

echo -e $create_script > model_files/create_imagenet_generated.sh

but my final txt file gets this format:

(If I execute ./my_bash_script.sh aAa bBb cCc)

#!/usr/bin/env sh # Hi, this is a txt file that I will make a printf # and I will execute later VARIABLE_A=aAa VARIABLE_B=bBb VARIABLE_C=cCc

How can I make it readable to be able to execute as bash script? like that, is everything a comment.

Thank you in advance.

Rafael.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • 2
    quote your `echo` to keep the format: `echo -e "$create_script"` – fedorqui Jul 09 '15 at 14:11
  • oh god.. thank you. I'm really new learning bash since I didn't study computer science... could you convert your comment in answer? I will validate it as Correct. Thank you! – Rafael Ruiz Muñoz Jul 09 '15 at 14:14
  • 2
    No problem : ) I just marked it as duplicate to a "canonical" answer for this situation (because it is quite a common mistake). Happy programming! – fedorqui Jul 09 '15 at 14:15
  • You can also use: `printf "$create_script\n" aAa bBb cCc > model_files/create_imagenet_generated.sh` and skip the `echo` part. – anubhava Jul 09 '15 at 14:15
  • 1
    I've gone across 8 or 9 posts and didn't find it... sorry! thank you very much. – Rafael Ruiz Muñoz Jul 09 '15 at 14:16

0 Answers0