【问题标题】:Bash - Misuse of quotes I believe - what am I doing wrong?Bash - 我认为滥用引号 - 我做错了什么?
【发布时间】:2021-01-25 09:03:24
【问题描述】:

说实话,我很困惑——我以为我现在已经了解了 bash 的基础知识,但事实并非如此。

问题:我想使用如下所示的 curl 包装器向 api 发出 POST 请求,但是当我使用包装器时,没有任何反应

包装器在其他情况下也能正常工作(即 ${_CURL} -s -o /dev/null -w %{http_code} ${URL})

IDEA1:所以我认为这可能与 json 对象周围的引号有关,我过去遇到过问题,所以我最终插入了“'${JSON}'”,哪个没用

IDEA2:在 curl 包装器中,我将所有参数 ("-b ${COOKIES} $@") 放在引号中,但什么也没有

卷曲包装

#!/bin/bash
# sets headers to use cookies
# ${Cookies} is set and translates to an absolute path of ./cookie-file.txt

if [ -f "${COOKIES}" ]; then
  curl -b "${COOKIES}" $@
else
  curl $@
fi

Json 对象,我认为它很重要

JSON=$( jq -n -r \
           --arg login $LOGIN \
           --arg pass ${PASS_INPUTS[@]} \
           '{username: $login, password: $pass}')

文件id用在中

#JSON is located right above the following line
#_CURL being the path to the wrapper i exported 
# Does not even send a request to the URL at $V2/auth/login
  RESPONSE=$($_CURL -s \
       --header "Content-Type: application/json" \
       --request POST \
       --data "$JSON" \
       $ARGS \
       "${V2}/auth/login")
   echo $RESPONSE

#---------------------------------------------------------------------------------------------
# This is what i had previously but Id rather not
# works just fine
[ ! -f "cookie-file.txt" ] && ARGS="-c cookie-file.txt" || ARGS="-b cookie-file.txt"
  
  RESPONSE=$(curl -s \
       --header "Content-Type: application/json" \
       --request POST \
       --data "$JSON" \
       $ARGS \
       "${V2}/auth/login")
   echo $RESPONSE

【问题讨论】:

  • ShellCheck自动检测this等常见问题
  • 之前听说过,但我们在这里 - 在低生活下不是最聪明的@thatotherguy

标签: bash curl cookies


【解决方案1】:

主要问题在$@附近,试试看:

#!/bin/bash
# sets headers to use cookies
# ${Cookies} is set and translates to an absolute path of ./cookie-file.txt

if [ -f "${COOKIES}" ]; then
  curl -b "${COOKIES}" "$@"
else
  curl "$@"
fi

如果没有双引号,带空格的参数将无法正确处理。

【讨论】:

  • THX MAN 天哪,祝福这个人对 bash 的非正统知识 @Philippe
  • 很高兴我在标题中指出,引用错误或没有引用...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2018-01-16
  • 2011-04-15
相关资源
最近更新 更多