How to escape the single quote character in an ssh / remote bash command?

前端 未结 5 2138
春和景丽
春和景丽 2020-12-01 07:51

I\'m building a small set of scripts for remotely starting, stopping and checking the status of a process. The stop of these scripts should look for a process a

5条回答
  •  隐瞒了意图╮
    2020-12-01 08:40

    Use

    ssh deploy@hera 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '"'"'{print $2}'"'"' | head -n 1`'
    

    Explanation:

    ssh deploy@hera 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '"'"'{print $2}'"'"' | head -n 1`'
                    >                               1                                   <>2<>    3     <>4<>      5      <
    

    1) First string with beginning of command: 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '

    2) Second string with only a single ' char: "'"

    3) Third string with the print command: '{print $2}'

    4) Fourth string with another single quote: "'"

    5) Fifth string with rest of command: ' | head -n 1`'

提交回复
热议问题