1

Hi I am trying to create script which will after run copy its content inside another file with different name (time dependent to debugging pruposes) my idea is something like that

#!/bin/bash
content="printf \"#!/bin/bash\\n%s\" \"$content\" >> \`date +%s+%M\`"
printf "#!/bin/bash\ncontent=%s\n%s" "$content" "$content" >> `date +%s+%M`

This script of course doesn't work because after run it didn't append last line correctly.

As a result I got something like this:

#!/bin/bash
content=printf \"#!/bin/bash\n%s" "" >> `date +%s+%M`
printf "#!/bin/bash\n%s" "" >> `date +%s+%M`

I had also tried with 'echo' rather than 'printf' but with echo it is even more complicated and because of my weak bash skills I am unable to figure out how to fix it. Also after hour of trying I am unable to discover any way how to fix this.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Have you tried `cat $0 > $filename`? – Barmar Mar 03 '16 at 00:28
  • https://en.wikipedia.org/wiki/Quine_(computing) – Barmar Mar 03 '16 at 00:30
  • Yes problem is that the process that consume scripts "after" execution remove them, so whit this I got error "file not exist" :/, but I don't know why during execution it didn't found them so I started with this approach –  Mar 03 '16 at 00:31
  • http://c2.com/cgi/wiki?QuineProgram – Barmar Mar 03 '16 at 00:32
  • Could it be removing the script while it's running, instead of after it's finished? – Barmar Mar 03 '16 at 00:33
  • Thanks for Quine-s, now I know that it is impossible in that way, I will try hack a little bit script but idea was to don't modify it to find other bug. –  Mar 03 '16 at 00:44
  • Have you tried using CAT >> file << EOT ? Maybe this can give you some hint: #!/bin/bash newFile=file_$(date +%s+%M) cat >> $newFile << EOF #!/bin/bash newFile=file_i\$(date +%s+%M) cat >> \$newFile << EOF $(cat $0) \EOF EOF – Cristian Ramon-Cortes Mar 11 '16 at 15:59

0 Answers0