可变替代

时间:2019-02-27 23:07:58

标签: bash escaping

我正在尝试将一行REST API命令转换为脚本。我已经在该论坛上使用了各种技巧,但仍然没有成功。我的一行脚本看起来像这样

curl -F file=@data-file.csv https://rest-api-host-dev/v1/schools/351/roster  -H "Authorization: Bearer $( curl -d "@creds.json" -w "\n" -H "Content-Type: application/json" -X POST https://rest-api-host/v1/auth/login | grep -Po '"access_token":.*?[^\\]",' | awk -F\" '{print $4}')"

工作正常。我试图将其分解为可变的替代品以实现便携性。那是问题发生的时候

AWK=/bin/awk
CURL=/usr/bin/curl
GREP=/bin/grep
ROSTERFILE=data-file.csv

# Production= 355
# Staging= 351
INSTITUTIONID=351
URI="/v1/schools/${INSTITUTIONID}/roster"

# Production= " https://rest-api-host-prod"
# Staging= " https://rest-api-host-dev"
URL=" https://rest-api-host-dev"

GETTOKEN="${CURL} -d \"@creds.json\"  -w \"\n\" -H \"Content-Type: application/json\" -X POST ${URL}/v1/auth/login"
GREPTOKEN="${GREP} -Po '\"access_token\":.*?[^\\\\]\",'"
PRINTTOKEN="${AWK} -F\\\" '{print \$4}'"


# Token is obtained from curl based authentication
TOKEN=`${GETTOKEN} | ${GREPTOKEN} | ${PRINTTOKEN}`

UPLOADARGS=' -H "Authorization: Bearer ${TOKEN}"'

#
#*******************************************************
# Main Program
#*******************************************************
#

${CURL} -F file=@${ROSTERFILE} ${URL}${URI} ${UPLOADARGS}

我已经尝试过使用``和$()放在变量中来转义单引号和双引号。仍然出现错误

example test
+ AWK=/bin/awk
+ CURL=/usr/bin/curl
+ GREP=/bin/grep
+ ROSTERFILE=data-file.csv
+ INSTITUTIONID=351
+ URI=/v1/schools/351/roster
+ URL=' https://rest-api-host-dev'
+ GETTOKEN='/usr/bin/curl -d "@creds.json"  -w "\n" -H "Content-Type: application/json" -X POST https://rest-api-host-dev/v1/auth/login'
./restapi-test.sh: line 52: unexpected EOF while looking for matching `"'
./restapi-test.sh: line 62: syntax error: unexpected end of file

+ AWK=/bin/awk
+ CURL=/usr/bin/curl
+ GREP=/bin/grep
+ ROSTERFILE=data-file.csv
+ INSTITUTIONID=351
+ URI=/v1/schools/351/roster
+ URL=' https://rest-api-host-dev'
+ GETTOKEN='/usr/bin/curl -d "@creds.json"  -w "\n" -H "Content-Type: application/json" -X POST  https://rest-api-host-dev/v1/auth/login'
+ GREPTOKEN='/bin/grep -Po '\''"access_token":.*?[^\\]",'\'''
+ PRINTTOKEN='/bin/awk -F\" '\''{print $4}'\'''
++ /usr/bin/curl -d '"@creds.json"' -w '"\n"' -H '"Content-Type:' 'application/json"' -X POST https://rest-api-host-dev/v1/auth/login
++ /bin/grep -Po ''\''"access_token":.*?[^\\]",'\'''
++ /bin/awk '-F\"' ''\''{print' '$4}'\'''
awk: '{print
awk: ^ invalid char ''' in expression

curl: (6) Couldn't resolve host 'application'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0    13      0     72 --:--:-- --:--:-- --:--:--     0
+ TOKEN=

感谢您的帮助。

-克里斯

0 个答案:

没有答案