How to store remote variable from remote cat file ??

I am trying to cat on a file located on remote server and assign it to remote variable.

I have both local and remote variables. Running below script from local. test.sh

J_NAME=XXX2
J_IP=XXX
ssh $J_IP  "ps auxw |grep java | grep -v grep |grep $J_NAME | awk '{print  \$2}'>/tmp/$J_NAME.pid;
chmod u+rwx /tmp/$J_NAME.pid;
pid= cat /tmp/$J_NAME.pid;
echo \$pid; kill -3 \$pid >>/tmp/$J_NAME.log"

I can print remote value by doing cat on remote /tmp/$J_NAME.pid but not able to store in remote variable and do some work on that variable.

I understand concept of `,",',\. Looks like I am missing something :frowning:

Hi, to put the output of a command into a variable, use command substitution

var=$(command)

A few points I think I should mention:-

  • You are needlessly issuing chmod as the file will be RW by you in any case and there is no need to execute it - it's just the output from ps after all.
    .
  • You ssh finishes and the file $J_NAME.pid is written to the local server, not the remote.
    You then try to kill the remote process on the local server which could be very bad if you have a process id locally that matches.
    Create your script on the remote server, then execute it as one process.

i hope that this helps,
Robin