简体   繁体   中英

How to display single quotes inside double quotes in string in python

I know this is very basic question, but not able to frame it out.

I need to execute a customized python command based command with in a script.

ex:
below is the actual commands.
command-dist --host='server1' -- cmd -- 'command1;command2'

I need to use this command in my python script form my script , this command needs be to executed on remote server

I need this command format to use in my script

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2)

but the above is failing due to quotes, tried number of ways still no luck.

When I tried this way
cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2)

I don't under why back slashes are appearing before and after cmd1;cmd2?
This is what I am not getting.
cmd
'ssh -q %s "command-dist --host=%s -- cmd -- /'cmd1;cmd2'/""' %(host1,host2)

Please help how do I get the above value

This

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2)

is understood by Python as

cmd = <some string>command-dist --host=%s -- cmd -- 'cmd1;cmd2'<some string>

because you use the same quotes inside and outside. Try instead to escape the internal quotes with a backslash:

cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2)

Aside from that, you should use the subprocess module and supply your command as a list to subprocess.Popen .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM