此问题在此处已有答案:
Difference between single and double quotes in Bash(7个答案)
18小时前关闭
我有下面的脚本从Linux主机运行curl命令,它需要用户输入并运行,但它不转成功,而同一命令手动成功。
#!/bin/bash
# Constants
api_key="Thdshd760Jd"
api_url="https://execute-api.amazonaws.com/prod/v1/archive;done"
# User input
read -p "Enter projectName: " projectName
read -p "Enter siteName: " siteName
read -p "Enter emailAddress: " emailAddress
read -p "Enter the path to the file containing disk names: " disk_file
# Read disk names from the file and create a comma-separated string
disks=$(awk '{printf "\"%s\", ", $1}' "$disk_file" | sed 's/, $//')
# Create JSON payload in Bash
json_payload="'{\"projectName\": \"$projectName\", \"siteName\": \"$siteName\", \"disks\": [$disks], \"emailAddress\": \"$emailAddress\"}'"
# Make the API request using curl
curl -vv -X POST -H "x-api-key: $api_key" -H "Content-Type: application/json" -d "$json_payload" $api_url
echo 'curl -vv -X POST -H "x-api-key: '$api_key'" -H "Content-Type: application/json" -d '$json_payload' '$api_url''
字符串
下面我是如何执行的。
$ ./curl_api_call.sh
Enter projectName: my_disk001
Enter siteName: JLDC
Enter emailAddress: [email protected]
Enter the path to the file containing disk names: demo_file
Note: Unnecessary use of -X or --request, POST is already inferred.
{"message": "Invalid request body"}
curl -vvvvvv -X POST -H "x-api-key: Thdshd760Jd" -H "Content-Type: application/json" -d '{"projectName": "my_disk001", "siteName": "JLDC", "disks": ["my_disk001"], "emailAddress": "[email protected]"}' https://execute-api.amazonaws.com/prod/v1/archive
型
即使做喜欢给同样的错误:
for i in `cat` ; do curl -vvvvvv -X POST -H "x-api-key: Thdshd760Jd" -H "Content-Type: application/json" -d '{"projectName": "$i", "siteName": "JLDC", "disks": ["$i"], "emailAddress": "[email protected]"}' https://execute-api.amazonaws.com/prod/v1/archive;done
型
请指导我如何纠正这种情况。
1条答案
按热度按时间u91tlkcl1#
JSON赋值中的单引号是错误的。
字符串
类似地,在
for
循环尝试中,单引号防止变量被插值。型
试试这个:
型
并且类似地
型
(Also don't read lines with
for
.)