I am trying to run multiple commands using SSH but I am not getting my syntax right. Could someone please give me some help?
ssh "$USER"@"$IP" " \
CURRENT_HOSTNAME=$(hostname); \
sed -c -i 's/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/' $DISTRO_FILE; \
sed -c -i 's/$CURRENT_HOSTNAME/$REPLACEMENT_VALUE/' $CONFIG_FILE; \
hostname $REPLACEMENT_VALUE"
Those commands work fine if they are run locally, but I am getting the following error with when trying to run them through SSH:
sed: -e expression #1, char 0: no previous regular expression
It seems the error comes from
sed -c -i 's/$CURRENT_HOSTNAME/$REPLACEMENT_VALUE/' $CONFIG_FILE; \
as it is trying to read $CURRENT_HOSTNAME which is set before in the SSH itself. I am not sure how to read that variable back. Any clue please?
EDITED the code:
ssh "$USER"@"$IP" "CURRENT_HOSTNAME=\$(hostname);
sed -c -i \"s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/\" $DISTRO_FILE;
sed -c -i \"s/\$CURRENT_HOSTNAME/$REPLACEMENT_VALUE/\" $CONFIG_FILE;
hostname $REPLACEMENT_VALUE"
This works! Thanks a lot!
Thanks.