Issue in executing cat (remote ssh)

Hi,
I need to ssh remotely to a machine and cat a file assign the value to a variable

Script:

#!/bin/bash -x
value=`cat config.txt`
echo "$value"

ssh me@xxx.host.com "valu='cat /export/home/test.md5'; echo "$valu"" | tee

Execution:

$ ./x
++ cat config.txt
+ value='touch me'
+ echo 'touch me'
touch me
+ ssh me@xxx.host.com 'valu='\''cat /export/home/test.md5'\''; echo '
+ tee

It's not giving any result when it executes the command remotely.

remote server:

cat test.md5
ce65f87aecee3d99cd2e947de5e4d4ea

I tried alot but can't able to resolve. Could you please help me on this.

Thanks.

I am unsure what you are trying to achieve. A noticed a couple of things:

  • Use $(...) instead of `...` . It saves you quoting troubles..
  • You use value first and then valu later. That may lead to confusion
  • Perhaps it is something like this it wat you are looking for?
ssh me@xxx.host.com 'valu=$(cat /export/home/test.md5); echo "$valu"'
  • but why not just use
ssh me@xxx.host.com cat /export/home/test.md5
  • Or perhaps you mean to do this:
valu=$(ssh me@xxx.host.com cat /export/home/test.md5)
echo "$valu"
  • I do not understand why you use tee , since you are not redirecting its output.

Sorry for not being clear on my initial post. Below are the details:

jar=test.jar
md5=test.jar.md5

now i want to do the below:

#1. md5sum test.jar (exists in remote server x)
#2. cat test.jar.md5 (exists in remote server x)
#3. Now from my local machine i want to ssh to the remote server x and then want to check whether md5sum is matching

I tried the below:

==================

#!/bin/bash -x

ssh datac@x.host.com cat /home/data/test.jar.md5


ssh datac@x.host.com "if [ `cat test.jar.md5` == `md5sum test.jar` ]; then exit 0; else exit 1; fi"

========================
output:
-----------

$ ./x
ce65f87aecee3d99cd2e947de5e4d4eacat: /home/data/test.jar.md5: Permission denied
md5sum: /home/data/test.jar: Permission denied

-- when i cat the file its displaying the result without any issue
-- but when i do the same inside the if condition its throwing permission error

Note: On the remote server I have all the required permissions to read and execute these jar and md5 files.

I tried alot of time on this but couldn't able to find the issue. Could someone help me here.

Thanks

Hi, some more remarks:

  • Since you are using double quotes, your two command substitutions are being executed on your local computer. Try single quotes
  • == is not syntactically correct. a single = should be used
  • The if statement with the exit commands is reproducing the exact return codes that the test command would produce anyway, so you can just leave it out.
  • it is best to quote both sides of a string comparison in a test statement
  • Still consider using $(...) instead of `...` (unless you are using a classic Bourne shell). It saves you quoting troubles..

Combining all this into one, try something like:

ssh datac@x.host.com '[ "$(cat test.jar.md5)" = "$(md5sum test.jar)" ]'

You can add 2>/dev/null is you want to suppress messages..

Thanks alot, it works now.

Hi, good to hear,

In addition, if the files are in the same directory you do not need to use a test, you could also use:

ssh datac@x.host.com 'md5sum -c test.jar.md5'

or

ssh datac@x.host.com 'md5sum --status -c test.jar.md5'

(or alternatively use >/dev/null 2>&1 ) to quiesce the output and only use the return code..

1 Like