Alex Alex - 12 days ago 4
Linux Question

Checking if software is installed in SSH session

I am trying to check whether a certain package is installed on remote machine in bash script.

If I execute the following statement on the machine itself the result is 1 (installed) in file check.txt, which is correct:

dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed" > /home/someuser/check.txt


However, if I execute the same command in SSH session, the result is always 0.

Can somebody explain why and how to correct this?

Thank you.

#!/bin/bash
ADDRESS=SOMEUSER@$SOMESERVER

function run {
ssh $ADDRESS /bin/bash $@
}

run << SSHCONNECTION

dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed" > /home/someuser/check.txt

SSHCONNECTION

Answer

You need to escape the $ character:

dpkg-query -W -f='\${Status}' nano 2>/dev/null | grep -c "ok installed" > /home/someuser/check.txt
Comments