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
HarryQ
  • 1,281
  • 2
  • 10
  • 25
  • @CharlesDuffy updated, my script actually did not contain space around `=`. – HarryQ Feb 20 '19 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 [mcve]. – Charles Duffy Feb 20 '19 at 20:52
  • Once again, please use the `-x` argument to `bash` to log the commands run during execution to stderr. BTW, if you use `PS4=':${LINENO}+' bash -x yourscript`, that log will also include line numbers (so long as it's not running as root with versions of bash that prevent PS4 from being inherited through the environment under that account for security reasons).. – Charles Duffy Feb 20 '19 at 20:55
  • 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. – Charles Duffy Feb 20 '19 at 20:59
  • the `bash -x ./myscript.bash` outputs with an appending `\r` – HarryQ Feb 20 '19 at 21:14
  • 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 https://stackoverflow.com/tags/bash/info – Charles Duffy Feb 20 '19 at 21:14
  • 1
    Try `echo "<<<$SESSION_TOKEN>>>"` to check if @HarryQ is right about an invisible `\r` char. That could be the problem. – Kjetil S. Feb 20 '19 at 21:33
  • `>>>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkdXIiOjI1OTIwMDAsImV4cCI6MTU1MzI3OTM1NSwic3ZyIjoibm8iLCJ1c3IiOiI0NTMzOTI1ZmVhIn0.yLhXquxYKCKWLrnoErKvZ1abxi_6NbEY_pFCGHyYuRs` this is the output – HarryQ Feb 20 '19 at 21:42

0 Answers0