I'm new to python and fabric api. I'm trying to use the sudo functionality to run a sed command in bash terminal which insert some text after a particular line of text is found. Some of the text I'm trying to insert into the file I'm modifying contains backslashes which seem to either be ignored by fabric or cause syntax errors. I've tried "shell=true" and "shell=false" options but still no luck. How can I escape the backslash? It seems "shell=true" only escapes $ and ". My Code below.
sudo (' sed -i "/sometext/a textwith\backslash" /home/me/somefile.txt',shell=True)
Try prefixing your string with r
, which means that it will be interpreted as a raw string:
sudo (r' sed -i "/sometext/a textwith\backslash" /home/me/somefile.txt',shell=True)
See here for additional information on string literals and their usage in Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With