我有一个用Bash编写的自动化脚本。我需要在其中执行一个perl脚本,并捕获/解析其输出的特定部分,并将其用作变量,以完成bash脚本的任务。
例如:
echo "Creating database for "$custname
perl /home/dib/testing/addCustomer.cgi name=$custname prefix=$customerno
perl脚本“ addCustomer.cgi”将返回以下内容的JSON输出:
Content-Type: application/json; charset=UTF-8
{
"recordcount" : 1,
"status" : "OK",
"statusText" : "Add Customer: 40:Testing",
"customer" : [
{
"address" : "",
"city" : "",
"country" : "US",
"description" : "",
"email" : "",
"endDate" : "0000-00-00 00:00:00",
"frontendurl" : "",
"phone" : "",
"prefix" : "testing",
"startDate" : "0000-00-00 00:00:00",
"state" : "",
"customerID" : "40",
"subscriberName" : "Testing",
"url" : "",
"zip" : ""
}
],
"timestamp" : 1559163419
}
我需要捕获的是customerID号,将其粘贴到变量中,然后使用它来完成bash脚本。这样的事情可能吗?我总是看到解析或从bash传递到perl,但反之则不然。
答案 0 :(得分:1)
将此附加到您的Perl命令:
| sed 1d | jq -r '.customer[].customerID'
输出:
40
我假设只有一个标题行。