-1

My problem is that supposedly identical variables are seen as different by test, and a supposedly empty variable is seen as not empty. I have a file containing variables on two machines, and I'm trying to load the local variables, then load the remote variables, and check them for differences. It's part of a larger script, so i'm reducing the code down to a simpler form. The script is run from machine A. I think i'm misunderstanding something somewhere, but can't for the life of me work out what it is.

Machine A ~/file.conf

version='1.0'
path='/tmp/'

Machine B ~/file.conf

version='1.0'
path=''

Machine A script.sh

#!/bin/bash

. ~/file.conf

l_version=$version
l_path=$path

eval $(ssh -oBatchMode=yes -oConnectTimeout=5 -p 22 -tq user@machineb cat ~/file.conf) # load remote
r_version=$version
r_path=$path

if [ -z $r_path ]; then
       echo "r_path is empty!"
fi
if [ $l_version != $r_version ]; then
       echo "version is different!"
fi

echo $l_version
echo $l_path
echo $r_version
echo $r_path

Apologies if there are any syntax errors or anything, this is not the original code but should demonstrate my issue.

AlexH
  • 49
  • 1
  • 9
  • Sorry, that should be a comment, i'll edit it now. – AlexH Jan 24 '21 at 15:16
  • 1
    Are there DOS line feeds on either side? What does `bash -x` output? – tripleee Jan 24 '21 at 15:24
  • Also tangentially [quote your variables.](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jan 24 '21 at 15:24
  • You should also quote the whole subshell `$()` line passed to `eval` to avoid `~` expansion. – Amessihel Jan 24 '21 at 15:47
  • ( i quoted the variables but no difference, i'll also quote the whole eval subshell, thanks). Bash -x is great, i haven't used that before. It's showing the machine b variables have a '/r' appended which half answers my question! However both file.conf's are generated in the same way. – AlexH Jan 24 '21 at 16:01

1 Answers1

0

While I've not been able to work out how the '\r' carriage return has made its way into my variables via my ssh command, i've opted to strip them out like so.

eval "$(ssh -oBatchMode=yes -oConnectTimeout=5 -p 22 -tq user@machineb cat ~/file.conf)"
r_version="$version"
r_path="$path"
r_version="${r_version//$'\r'}"
r_path="${r_path//$'\r'}"

It seems to work, but i would like to learn why this is happening and if there is a cleaner way to deal with this issue that would be great. Thanks for all your help!

AlexH
  • 49
  • 1
  • 9
  • `file.conf` on the remote was saved with DOS line endings. Run dos2unix or similar on it, and you won't need this workaround – that other guy Jan 24 '21 at 17:27