shell 将$ variable传递给bash脚本中的curl命令

0ejtzxu1  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(189)

我尝试创建一个循环脚本,从CSV文件中提取电话号码,并在每个循环中设置$from和$to变量。
但是,我收到了一个错误阅读“The supplied JSON is invalid”。由于在脚本本身中设置了变量,因此$body被正确传递,并且当我在脚本中硬编码$from和$to变量时,没有收到任何错误。
但是,当我尝试使用CSV中的数据设置变量时,我在执行时收到JSON错误。
我尝试了数据中引号的各种可能的变化,但我没有运气。
我已经附上了一个我试图传递给脚本的示例数据的屏幕截图。
有什么想法吗?[![在此处输入图像描述][1]][1]

OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }

while read class from to
do
   if [[ $class = "send" ]];
   then
       echo "class: $class"
       echo "from: $from"
       echo "to: $to"

       body="Test"

echo $body
       
        curl -X POST \
        --header "Content-Type: application/json" \
        --header "Accept: application/json" \
        --header "Authorization: Bearer KEY018435A- SAMPLE" \
        --data '{
            "from": "'"$from"'",
            "to": "'"$to"'",
            "text": "'"$body"'",
            "media_urls" : [
            "https://files-service-prod.s3.amazonaws.com/direct-uploads%2F6e1a1600"
            ]
        }' \
        https://api.sortor.io/v2/messages

    fi    

sleep 2
done < $INPUT
IFS=$OLDIFS ```

  [1]: https://i.stack.imgur.com/9lkp4.png
cgvd09ve

cgvd09ve1#

如下所示,使用shell heredoc

curl -X POST \
    --header "Content-Type: application/json" \
    --header "Accept: application/json" \
    --header "Authorization: Bearer KEY018435A- SAMPLE" \
    --data "@/dev/stdin" https://api.sortor.io/v2/messages <<EOF
    {
        "from": "$from",
        "to": "$to",
        "text": "$body",
        "media_urls" : [
            "https://files-service-prod.s3.amazonaws.com/direct-uploads%2F6e1a1600"
        ]
    }
EOF

勾选

man bash | less +/here-doc

相关问题