In one word:
question and example could test locally:
sh -c "echo 'how to print single quote here'"
details:
I have a config like this:
upload_server = ('192.168.1.1', 10051)
now I need a shell script to replace server config. Locally I can do it with sed:
sed -E 's/( upload_server = ).*/\\1('\"'$a'\"', 10051)/g' config.py
but there's huge amount of machines have to change the config.
a proper plan is to use ssh
with for..in:
for i in `cat hosts`; do ssh $i "cmd"; done
but to change config also need a sudo
, it looks like this:
for i in `cat hosts`; do ssh $i "sudo -c \"cmd\""; done
and now within sed:
upload_server=123.123.123.123
upload_server_port=1000
cmd="sed -E 's/( upload_server = ).*/\\1('\"\"'\"'${upload_server}'\"'\"\"', ${upload_server_port})/g' config.py"
ssh $i "sudo -c \"${cmd}\""
the result of config:
upload_server = (123.123.123.123, 1000)
which should be like this:
upload_server = ('123.123.123.123', 1000)
Now I'm confused a lot in quotes usage. :(
sudo -c
? Why not justsudo cmd
?scp
it to the target before running it? That is much simpler, clearer, and adaptable.upload_server = (192.168.1.1, 10051)
. Anyway, for your purpose, I think that it will be easier for you to use some automation tool like ansible (github.com/ansible/ansible)