使用bash脚本解析curl中的变量

时间:2017-08-16 13:55:57

标签: json bash curl environment-variables echo

嘿我正在使用管道卷曲方法从帖子创建任务。当我从具有硬编码值的终端运行时,它工作正常。但是当我尝试用变量执行它时会抛出一个错误:

脚本:

./test.sh "test003 ticket from api post" "for testing" "open" "high" "ahsan"

执行:

test003 ticket from api post
for testing
open
high
ahsan
{"error":"ERR-CONDUIT-CORE","errorMessage":"ERR-CONDUIT-CORE: Validation errors:\n  - User \"$5\" is not a valid user.\n  - Task priority \"$4\" is not a valid task priority. Use a priority keyword to choose a task priority: unbreak, very, high, kinda, triage, normal, low, wish.","response":null}

输出:

const colorThemes = [
  { foreground: "#0A1C6B", background: "#5DF0AD" },
  { foreground: "#C2F5FF", background: "#0A1C6B" },
  { foreground: "#583985", background: "#CCCCF0" },
  { foreground: "#FBBEA6", background: "#5839B5" },
  { foreground: "#8A350D", background: "#FFDB0D" }
]

const colors = colorThemes.reduce((acc, curr) => {
  acc.push(curr.foreground, curr.background);
  
  return acc;
}, []);

const randomnumber = Math.floor((Math.random() * colorThemes.length));

const selector = colorThemes[randomnumber]

if (colors.includes(selector.foreground)) {
  console.log('Success');
}

正如你所看到的那样,它的读数为4美元和5美元,而不是变量。我无法理解如何在这些参数中使用$变量作为输入。

1 个答案:

答案 0 :(得分:1)

您在最后EXTRACT( date_part FROM expression )周围使用单引号,以便您可以在JSON中使用双引号,但这会导致echo打印字符串而不展开任何内容。你需要为字符串使用双引号,所以你必须转义它内部的双引号。

将最后echo替换为:

echo

它会起作用。为避免此类问题,您可以查看http://wiki.bash-hackers.orghttp://mywiki.wooledge.org/BashGuide,了解echo "{ \"transactions\": [ { \"type\": \"title\", \"value\": \"$1\" }, { \"type\": \"description\", \"value\": \"$2\" }, { \"type\": \"status\", \"value\": \"$3\" }, { \"type\": \"priority\", \"value\": \"$4\" }, { \"type\": \"owner\", \"value\": \"$5\" } ] }" 新手的一些常规提示。此外,您可以将shellcheck与许多文本编辑器一起使用,这会自动发现这样的错误。