0

In a script, I tried to assign a session token string to a variable, that token string was stored in a text file.

SESSION_TOKEN=$(<login_token.txt)

Previously, I did the assignment by writing literally.

SESSION_TOKEN_ORIGINAL=eyJhbGciOiJI......CGHyYuRs 

As the session token is different every time I start a session, I store the obtained token in a text file instead of editing the bash script every time.

Unexpectedly, SESSION_TOKEN was not accepted by the server, while SESSION_TOKEN_ORIGINAL worked.

I used bash to invoke the script

bash ./myscript.bash

To understand the reason, I created an experimental comparison statement

if [ "$SESSION_TOKEN" == "$SESSION_TOKEN_ORIGINAL" ]
then
    echo "two tokens are the same"
fi

It does NOT output the equal statement. Why?

To better illustrate the context, I post the entire script with the server address masked.

#!/bin/bash
SESSION_TOKEN=$(<./login_token.tmp)
SESSION_TOKEN_ORIGINAL=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkdXIiOjI
USERID=fsag25fea
curl -s -X GET -H "x-tidepool-session-token: $SESSION_TOKEN" -H "Content-Type: application/json" "https://api.database.org/metadata/users/$USERID/users"  #this does not work, the server responds with 0 content
curl -s -X GET -H "x-tidepool-session-token: $SESSION_TOKEN_ORIGINAL" -H "Content-Type: application/json" "https://api.database.org/metadata/users/$USERID/users"  #this works, the server responds with the expected json
8
  • @CharlesDuffy updated, my script actually did not contain space around =. Commented Feb 20, 2019 at 20:51
  • BTW, using bash -x yourscript to invoke the script with trace logs on stderr is always helpful -- if nothing else to help you identify which commands are behaving contrary to expectation and allow development of a more focused minimal reproducible example. Commented Feb 20, 2019 at 20:52
  • 2
    ...if that log shows $'...filecontent...\r', when it should be '...filecontent...', that tells us the file has DOS newlines rather than being in native UNIX format. Commented Feb 20, 2019 at 20:59
  • 1
    That being the case, the question's duplicate list has been updated to be appropriate to the immediate cause. BTW, see also #1 in the "Before asking about problematic code" section in stackoverflow.com/tags/bash/info Commented Feb 20, 2019 at 21:14
  • 1
    Try echo "<<<$SESSION_TOKEN>>>" to check if @HarryQ is right about an invisible \r char. That could be the problem. Commented Feb 20, 2019 at 21:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.