mario martinez
mario martinez

Reputation: 297

Echo printing variables in a completely wrong order

I am trying to create a string with a query that will be save / send to another location, this string contains different variables.

The issue that I am having is that the echo of the variables are completely upside down and mix.

See code below:

tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt")
serverName="asdasd"
attQuery="$tokenID $serverName"
agentRegQuery="$./opt/mule/bin/amc_setup -H $attQuery"

echo TOKEN ID $tokenID
echo SERVER NAME $serverName
echo $attQuery
echo $agentRegQuery

Find below the output I am receiving:

TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
SERVER NAME asdasd
 asdasdf-fa0e-4f08-87eb-418722872d80---46407
 asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407

Upvotes: 0

Views: 2456

Answers (2)

Gordon Davisson
Gordon Davisson

Reputation: 125758

There's a carriage return character at the end of the tokenID variable, probably because /tempdir/tokenfile.txt is in DOS/Windows format (lines end with carriage return+linefeed), not unix (lines end with just linefeed). When you print tokenID by itself, it looks ok, but if you print something else after that on the same line, it winds up overwriting the first part of the line. So when you print $attQuery, it prints this:

29a6966f-fa0e-4f08-87eb-418722872d80---46407[carriage return]
 asdasd

...but with the second line printed on top of the first, so it comes out as:

 asdasdf-fa0e-4f08-87eb-418722872d80---46407

The solution is to either convert the file to unix format (dos2unix will do this if you have it), or remove the carriage return in your script. You can do it like this:

tokenID=$(docker exec -ti $dockerContainerID /bin/sh -c "cat /tempdir/tokenfile.txt" | tr -d '\r')

Upvotes: 2

schorsch312
schorsch312

Reputation: 5698

I think everything works as it should

echo TOKEN ID $tokenID             -> TOKEN ID 29a6966f-fa0e-4f08-87eb-418722872d80---46407
echo SERVER NAME $serverName       -> SERVER NAME asdasd
echo $attQuery                     -> asdasdf-fa0e-4f08-87eb-418722872d80---46407
echo $agentRegQuery                -> asdasdmule/bin/amc_setup -H 29a6966f-fa0e-4f08-87eb-418722872d80---46407

Why do you think something is wron here? Best regards, Georg

Upvotes: 0

Related Questions