0

Using bash I need to print some txt file which actually is another python script consisted of some tittles moved into the variable (t) within the main script

t="test"

printf "import pymol
cmd.load("${t}.pdb")" > ./script.py

the problem that the resulting script.py looks exactly like what I've defined in my main script

import pymol
cmd.load("${t}.pdb")"

so how I can to past correctly value of 't' within the resulted script.py?

import pymol
cmd.load("test.pdb")"

3 Answers 3

1

You need to use a placeholder in the format string:

printf 'import pymol\ncmd.load("%s.pdb")' "$t"

%s specifies that a string value will be inserted. It is not considered good practice to use variables within the format string; they should be passed as additional arguments to printf.

I have also fixed your quotes and used a newline \n rather than defining the string over two lines. It is always good practice to double quote your variable expansions, e.g. "$t".

An alternative in this case would be to use a heredoc, which you may prefer:

t='test'
cat <<EOF >script.py
import pymol
cmd.load("${t}.pdb")
EOF
Sign up to request clarification or add additional context in comments.

Comments

0
t="test"

printf "import pymol 
cmd.load('$t.pdb')" > script.py

You can do like you do when you a value echo $smth using just $. Mind the absence of ./ when writing to a file. We execute a file by adding a dot and a slash but we don't write to it like that.

Comments

0
t="test"

printf "import pymol 

cmd.load("$t.pdb")" > script.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.