Search code examples
shellsedsshkshremote-server

Why SED command works on server but not with remote server?


I've a sed command which works perfectly when I run it direct form server. But when I tried to run it from another server it seems that it doesn't want to get all my sed command.

Server RED HAT. I've tried directly from server my command which works perfectly but from another server via shh it doesn't.

Here my command with ssh :

ssh 'user@host' sudo -S -u webm sh -c "sed -i 's/^wrapper.java.additional.50=-agentpath:\"\/webmethods\/dynatrace/#wrapper.java.additional.50=-agentpath:\"\/webmethods\/dynatrace/g' /webmethods/eg_*/profiles/IS_default/configuration/custom_wrapper.conf"

I expect to change a file content but instead of, I've this message : sed: -e expression #1, char 54: unknown option to `s'

At the end, this will be in ksh script.

Thanks


Solution

  • Double quotes themselves interpret \/ as /, you need to double backslash to preserve the escaping in doublequotes.

    If possible, it might be easier to send the command to stdin of the shell:

    cat << EOF | ssh 'user@host' sudo -S -u webm sh
    sed -i 's/^wrapper.java.additional.50=-agentpath:"\/webmethods\/dynatrace/#wrapper.java.additional.50=-agentpath:"\/webmethods\/dynatrace/g' /webmethods/eg_*/profiles/IS_default/configuration/custom_wrapper.conf
    EOF
    

    Also, use different delimiters to avoid the need to escape. You can use any character not appearing in the strings:

    sed -i 's%^wrapper.java.additional.50=-agentpath:"/webmethods/dynatrace%#wrapper.java.additional.50=-agentpath:"/webmethods/dynatrace%g'
    

    Also note that . matches any character, but you probably don't want to match strings like

    wrapperAjavaBadditionalC50=-agentpath:"/webmethods/dynatrace
    

    Use \. or [.] to match a literal dot.