面试题答案
一键面试#!/bin/bash
read -p "请输入城市名: " city_name
weather_info=$(curl -s "http://api.example.com/weather?city=$city_name")
current_temperature=$(echo "$weather_info" | jq -r '.current_temperature')
echo "当前温度: $current_temperature"
上述脚本中:
read -p "请输入城市名: " city_name
提示用户输入城市名,并将输入赋值给city_name
变量。curl -s "http://api.example.com/weather?city=$city_name"
调用天气API,-s
选项表示静默模式,不显示进度条等信息。echo "$weather_info" | jq -r '.current_temperature'
使用jq
工具从返回的JSON数据中提取current_temperature
字段的值,-r
选项表示以原始字符串形式输出。- 最后打印出当前温度。
请根据实际API返回的JSON结构调整 jq
命令中的字段名。