从文本文件读取的Bash脚本变量不起作用

时间:2019-02-20 20:47:53

标签: bash

在脚本中,我尝试将会话令牌字符串分配给变量,该令牌字符串存储在文本文件中。

SESSION_TOKEN=$(<login_token.txt)

以前,我是按字面意思进行写的。

SESSION_TOKEN_ORIGINAL=eyJhbGciOiJI......CGHyYuRs 

由于每次启动会话时会话令牌都不同,所以我将获取的令牌存储在文本文件中,而不是每次都编辑bash脚本。

出乎意料的是,SESSION_TOKEN_ORIGINAL正常工作时,服务器未接受SESSION_TOKEN。

我用bash来调用脚本

bash ./myscript.bash

为了解原因,我创建了一个实验性比较声明

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

它不输出相等的语句。为什么?

为了更好地说明上下文,我发布了整个脚本,并屏蔽了服务器地址。

#!/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

0 个答案:

没有答案