Kev*_*vin 3 unix linux awk curl sed
我正在编写一个要在 Linux 设备上运行的 bash 脚本,因此我无权安装 jq,我需要能够使用本机 Linux 工具
curl --location --request POST "https://api-mp.meraki.com/api/v1/networks/12345/switch/stacks/12345/routing/interfaces" \
--header "X-Cisco-Meraki-API-Key: 123key" \
--header "Content-Type: application/json" \
--data '{
"name": "L3 int",
"subnet": "192.168.249.0/24",
"interfaceIp": "192.168.249.2",
"multicastRouting": "disabled",
"vlanId": 140,
"ospfSettings": {
"area": "0",
"cost": 1,
"isPassiveEnabled": true
}
}' | sed -n 's|.*"interfaceID":"\([^"]*\)".*|\1|p' > $newVar
Run Code Online (Sandbox Code Playgroud)
服务器的响应返回以下内容:
{
"interfaceId": "12345",
"name": "PA L3 DR Int",
"subnet": "192.168.249.0/24",
"interfaceIp": "192.168.249.2",
"multicastRouting": "disabled",
"vlanId": 140,
"ospfSettings": {
"area": "0",
"cost": 1,
"isPassiveEnabled": true
}
}
Run Code Online (Sandbox Code Playgroud)
然后我需要能够访问查询字符串参数中的interfaceID新变量
你只是把大小写弄错了(IDvs Idin interfaceId)并且忘记了后面的空格:,但你已经很接近了:
$ sed -n 's|.*"interfaceId": *"\([^"]*\)".*|\1|p' file
12345
Run Code Online (Sandbox Code Playgroud)
使用cat file代替curl命令来展示如何将其保存到变量,以防您不知道:
$ var=$(cat file | sed -n 's|.*"interfaceId": *"\([^"]*\)".*|\1|p')
$ echo "$var"
12345
Run Code Online (Sandbox Code Playgroud)