1

When I run the below command:

ssh -q -o BatchMode=yes -T <server> <<'EOF'
export INTIAL_COUNT=$(ps -ef|grep -v grep|grep "/bin/ksh /test/bin/worker.sh" |wc -l)
EOF

echo ${INTIAL_COUNT}

output I get is null

expected output is some value

echo ${INTIAL_COUNT}
10
5
  • Are you executing echo ${INITIAL_COUNT} on the local system or remote system?
    – jesse_b
    Feb 27 '18 at 15:07
  • local server after the EOF part is complete.
    – Amit Baid
    Feb 27 '18 at 15:12
  • Why would a variable you exported on a remote machine be available locally? It also wont be available remotely outside of our ssh session though. In order to make an environmental variable permanent you need to add it to your profile on the machine you want that variable on.
    – jesse_b
    Feb 27 '18 at 15:14
  • 1
    The value is assigned to INITIAL_COUNT in the shell started by ssh and will not be carried over to the local shell. To set the variable locally, use INITIAL_COUNT=$( ssh ... ). Also consider using pgrep.
    – Kusalananda
    Feb 27 '18 at 15:15
  • Possible duplicate of How to export a function in bash over ssh? Feb 28 '18 at 11:18
4

try

INTIAL_COUNT=$(ssh -q -o BatchMode=yes -T <server> <<'EOF'
ps -ef|grep -c "/bin/ksh /test/bin/[w]orker.sh" 
EOF
)

where

  • grep -v grep is replaced by grep [w] whitch not match itself
  • grep ... | wc -l is replaced by grep -c
  • $( .. ) construct can span line

or

INTIAL_COUNT=$(ssh -q -o BatchMode=yes -T <server> 'ps -ef|grep -c "/bin/ksh /test/bin/[w]orker.sh"')

even shorter.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.