使用 shell 脚本读取 JSON 文件
Reading JSON file using shell scriptt
我是 shell 脚本的新手,我正在尝试阅读 API 来获取值...
但我对如何有效地执行它有一些疑问。
这是一个简单的程序,用于显示城市中的大学和域名
link 到 API 是:http://universities.hipolabs.com/search?name=middle
我的密码是
# A simple shell program to find the all the universities and domains present in the country depending on the user-provided.
echo "Description : List of universities based on the location provided \n"
user_input_location_of_the_university=
if [ -z "$user_input_location_of_the_university" ]
then
echo "location required. please provide a valid country name"
exit 1
fi
user_input_location_of_the_university_replacing_spaces=$( echo "$user_input_location_of_the_university" | sed 's/ /%20/g' )
#user_input_location_of_the_university=$(echo "$user_input_location_of_the_university | sed 's/ /%20/g')
response_contianing_university_data=$(curl -sS -X GET "http://universities.hipolabs.com/search?name=middle&country=$user_input_location_of_the_university_replacing_spaces")
if [ -z "$response_contianing_university_data" ]
then
echo "invalid request, provide with a valid location"
else
# echo "total response is: "
# echo $response_contianing_university_data | jq .
echo "Name of the university based the location $user_input_location_of_the_university is: \r"
echo $response_contianing_university_data | jq -r '.[] .name'
echo "\n"
echo "domain of the university based the location $user_input_location_of_the_university is: "
echo $response_contianing_university_data | jq -r '(.[] .domains[0:] | @sh)'
fi
如果我通过提供无效名称来执行程序,例如 sh file_anme.sh "tur" 而不是 sh file_anme.sh "turkey" 它应该被打印为:
invalid request, provide with a valid location
但是正在打印...
Name of the university based the location tur is:
domain of the university based the location tur is:
- 我必须在 if 条件中输入什么来测试所提供的 URL 是否有效?
和
另一个疑惑是,如何在终端显示输出的同时去掉引号?
在获取域的详细信息(第二部分)时,我在 jq 的帮助下使用了 -r 标志...
我在下面提供:
#while executing
sh myfiles.sh "United States"
#The output
Decription : List of universities based on the location provided
Name of the university based the location United States is:
Middlesex County College
Middlesex Community College
Middlebury College
Middle Tennessee State University
Middle Georgia State College
domain of the university based the location United States is:
'middlesexcc.edu'
'middlesex.mass.edu'
'middlebury.edu'
'mtsu.edu'
'mga.edu'
但我需要不带引号显示如下:
domain of the university based the location United States is:
middlesexcc.edu
middlesex.mass.edu
middlebury.edu
mtsu.edu
mga.edu
- 我应该怎么做才能删除“jq -r”以外的引号?
作为参考,我附上来自上述 link 的 JSON 数据...
[{"country": "United Kingdom", "domains": ["middlesbro.ac.uk", "mbro.ac.uk"], "name": "Middlesbrough College", "state-province": null, "web_pages": ["https://www.mbro.ac.uk/"], "alpha_two_code": "GB"}, {"country": "United States", "domains": ["middlesexcc.edu"], "name": "Middlesex County College", "state-province": null, "web_pages": ["http://www.middlesexcc.edu"], "alpha_two_code": "US"}, {"country": "United States", "domains": ["middlesex.mass.edu"], "name": "Middlesex Community College", "state-province": null, "web_pages": ["http://www.middlesex.mass.edu"], "alpha_two_code": "US"}, {"country": "United Kingdom", "domains": ["mdx.ac.uk"], "name": "Middlesex University", "state-province": null, "web_pages": ["http://www.mdx.ac.uk/"], "alpha_two_code": "GB"}, {"country": "Kuwait", "domains": ["aum.edu.kw"], "name": "American University of Middle East", "state-province": null, "web_pages": ["http://www.aum.edu.kw/"], "alpha_two_code": "KW"}, {"country": "United States", "domains": ["middlebury.edu"], "name": "Middlebury College", "state-province": null, "web_pages": ["http://www.middlebury.edu/"], "alpha_two_code": "US"}, {"country": "United States", "domains": ["mtsu.edu"], "name": "Middle Tennessee State University", "state-province": null, "web_pages": ["http://www.mtsu.edu/"], "alpha_two_code": "US"}, {"country": "United States", "domains": ["mga.edu"], "name": "Middle Georgia State College", "state-province": null, "web_pages": ["http://www.mga.edu/"], "alpha_two_code": "US"}, {"country": "Jordan", "domains": ["meu.edu.jo"], "name": "Middle East University", "state-province": null, "web_pages": ["http://www.meu.edu.jo/"], "alpha_two_code": "JO"}, {"country": "Turkey", "domains": ["metu.edu.tr"], "name": "Middle East Technical University", "state-province": null, "web_pages": ["http://www.metu.edu.tr/"], "alpha_two_code": "TR"}]
第一个问题:
- 如果地名不匹配,JSON 响应为空,您可以检查一下。 API 显然没有为此状态提供特定的错误代码或消息,除了响应为空。
检查空 JSON 消息:
if [ "$response_contianing_university_data" = '[]' ]; then
echo "invalid request, provide with a valid location"
else
# etc
- 如果需要,您也可以执行
if [ -z "$(echo "$response_contianing_university_data" | jq '.[]')" ]
或类似操作。处理后的 JSON 将完全为空。
第二个问题:
- 正在添加域周围的单引号,因为您在
jq
中使用 @sh
运算符。只需将其删除。
列出不带引号的域:
echo "$response_contianing_university_data" | jq -r '.[].domains[]'
还有:
- 你的变量名太长了。例如
$location
就更清楚了。
- 你拼错了“contianing”(应该是“containing”)。
- 当将它们回显到
jq
. 时,您必须用双引号 ("
) 引用变量
有趣 API(JSON-文件)。如果您使用 xidel,那么您就不需要 curl
、sed
或 Bash-脚本。
$ location='United States'; xidel -s "http://universities.hipolabs.com/search?name=middle&country=$location" -e '
if (exists($json())) then (
"Names of the universities based on the location '"$location"' is:",
$json()/name,
"",
"Domains of the universities based on the location '"$location"' is:",
$json()/(domains)()
)
else
"Invalid request. Provide with a valid location."
'
或
$ location='United States' xidel -s --variable location -e '
let $json:=json-doc("http://universities.hipolabs.com/search?name=middle&country="||$location) return
if (exists($json())) then (
x"Name of the university based the location {$location} is:",
$json()/name,
"",
x"Domain of the university based the location {$location} is:",
$json()/(domains)()
)
else
"Invalid request. Provide with a valid location."
'
哪个returns:
Name of the university based the location United States is:
Middlesex County College
Middlesex Community College
Middlebury College
Middle Tennessee State University
Middle Georgia State College
Domain of the university based the location United States is:
middlesexcc.edu
middlesex.mass.edu
middlebury.edu
mtsu.edu
mga.edu
或者,您也可以直接查询 JSON,as that's what this API is also doing in the background。
$ xidel -s "https://raw.githubusercontent.com/Hipo/university-domains-list/master/world_universities_and_domains.json" -e '
parse-json($raw)()[country="United States" and starts-with(name,"Middle")]/concat(name," - ",join((domains)(),", "))
'
Middlebury College - middlebury.edu
Middle Georgia State College - mga.edu
Middle Tennessee State University - mtsu.edu
Middlesex Community College - mxcc.commnet.edu
Middlesex Community College - middlesex.mass.edu
Middlesex County College - middlesexcc.edu
(对于github-url-e '$headers'
returnsContent-Type: text/plain;
(而不是Content-Type: application/json
),所以你必须解析原始内容为 JSON。全局默认变量 $json
仅在输入为 Content-Type: application/json
)
时设置
我是 shell 脚本的新手,我正在尝试阅读 API 来获取值...
但我对如何有效地执行它有一些疑问。
这是一个简单的程序,用于显示城市中的大学和域名
link 到 API 是:http://universities.hipolabs.com/search?name=middle
我的密码是
# A simple shell program to find the all the universities and domains present in the country depending on the user-provided.
echo "Description : List of universities based on the location provided \n"
user_input_location_of_the_university=
if [ -z "$user_input_location_of_the_university" ]
then
echo "location required. please provide a valid country name"
exit 1
fi
user_input_location_of_the_university_replacing_spaces=$( echo "$user_input_location_of_the_university" | sed 's/ /%20/g' )
#user_input_location_of_the_university=$(echo "$user_input_location_of_the_university | sed 's/ /%20/g')
response_contianing_university_data=$(curl -sS -X GET "http://universities.hipolabs.com/search?name=middle&country=$user_input_location_of_the_university_replacing_spaces")
if [ -z "$response_contianing_university_data" ]
then
echo "invalid request, provide with a valid location"
else
# echo "total response is: "
# echo $response_contianing_university_data | jq .
echo "Name of the university based the location $user_input_location_of_the_university is: \r"
echo $response_contianing_university_data | jq -r '.[] .name'
echo "\n"
echo "domain of the university based the location $user_input_location_of_the_university is: "
echo $response_contianing_university_data | jq -r '(.[] .domains[0:] | @sh)'
fi
如果我通过提供无效名称来执行程序,例如 sh file_anme.sh "tur" 而不是 sh file_anme.sh "turkey" 它应该被打印为:
invalid request, provide with a valid location
但是正在打印...
Name of the university based the location tur is:
domain of the university based the location tur is:
- 我必须在 if 条件中输入什么来测试所提供的 URL 是否有效?
和
另一个疑惑是,如何在终端显示输出的同时去掉引号? 在获取域的详细信息(第二部分)时,我在 jq 的帮助下使用了 -r 标志...
我在下面提供:
#while executing
sh myfiles.sh "United States"
#The output
Decription : List of universities based on the location provided
Name of the university based the location United States is:
Middlesex County College
Middlesex Community College
Middlebury College
Middle Tennessee State University
Middle Georgia State College
domain of the university based the location United States is:
'middlesexcc.edu'
'middlesex.mass.edu'
'middlebury.edu'
'mtsu.edu'
'mga.edu'
但我需要不带引号显示如下:
domain of the university based the location United States is:
middlesexcc.edu
middlesex.mass.edu
middlebury.edu
mtsu.edu
mga.edu
- 我应该怎么做才能删除“jq -r”以外的引号?
作为参考,我附上来自上述 link 的 JSON 数据...
[{"country": "United Kingdom", "domains": ["middlesbro.ac.uk", "mbro.ac.uk"], "name": "Middlesbrough College", "state-province": null, "web_pages": ["https://www.mbro.ac.uk/"], "alpha_two_code": "GB"}, {"country": "United States", "domains": ["middlesexcc.edu"], "name": "Middlesex County College", "state-province": null, "web_pages": ["http://www.middlesexcc.edu"], "alpha_two_code": "US"}, {"country": "United States", "domains": ["middlesex.mass.edu"], "name": "Middlesex Community College", "state-province": null, "web_pages": ["http://www.middlesex.mass.edu"], "alpha_two_code": "US"}, {"country": "United Kingdom", "domains": ["mdx.ac.uk"], "name": "Middlesex University", "state-province": null, "web_pages": ["http://www.mdx.ac.uk/"], "alpha_two_code": "GB"}, {"country": "Kuwait", "domains": ["aum.edu.kw"], "name": "American University of Middle East", "state-province": null, "web_pages": ["http://www.aum.edu.kw/"], "alpha_two_code": "KW"}, {"country": "United States", "domains": ["middlebury.edu"], "name": "Middlebury College", "state-province": null, "web_pages": ["http://www.middlebury.edu/"], "alpha_two_code": "US"}, {"country": "United States", "domains": ["mtsu.edu"], "name": "Middle Tennessee State University", "state-province": null, "web_pages": ["http://www.mtsu.edu/"], "alpha_two_code": "US"}, {"country": "United States", "domains": ["mga.edu"], "name": "Middle Georgia State College", "state-province": null, "web_pages": ["http://www.mga.edu/"], "alpha_two_code": "US"}, {"country": "Jordan", "domains": ["meu.edu.jo"], "name": "Middle East University", "state-province": null, "web_pages": ["http://www.meu.edu.jo/"], "alpha_two_code": "JO"}, {"country": "Turkey", "domains": ["metu.edu.tr"], "name": "Middle East Technical University", "state-province": null, "web_pages": ["http://www.metu.edu.tr/"], "alpha_two_code": "TR"}]
第一个问题:
- 如果地名不匹配,JSON 响应为空,您可以检查一下。 API 显然没有为此状态提供特定的错误代码或消息,除了响应为空。
检查空 JSON 消息:
if [ "$response_contianing_university_data" = '[]' ]; then
echo "invalid request, provide with a valid location"
else
# etc
- 如果需要,您也可以执行
if [ -z "$(echo "$response_contianing_university_data" | jq '.[]')" ]
或类似操作。处理后的 JSON 将完全为空。
第二个问题:
- 正在添加域周围的单引号,因为您在
jq
中使用@sh
运算符。只需将其删除。
列出不带引号的域:
echo "$response_contianing_university_data" | jq -r '.[].domains[]'
还有:
- 你的变量名太长了。例如
$location
就更清楚了。 - 你拼错了“contianing”(应该是“containing”)。
- 当将它们回显到
jq
. 时,您必须用双引号 (
"
) 引用变量
有趣 API(JSON-文件)。如果您使用 xidel,那么您就不需要 curl
、sed
或 Bash-脚本。
$ location='United States'; xidel -s "http://universities.hipolabs.com/search?name=middle&country=$location" -e '
if (exists($json())) then (
"Names of the universities based on the location '"$location"' is:",
$json()/name,
"",
"Domains of the universities based on the location '"$location"' is:",
$json()/(domains)()
)
else
"Invalid request. Provide with a valid location."
'
或
$ location='United States' xidel -s --variable location -e '
let $json:=json-doc("http://universities.hipolabs.com/search?name=middle&country="||$location) return
if (exists($json())) then (
x"Name of the university based the location {$location} is:",
$json()/name,
"",
x"Domain of the university based the location {$location} is:",
$json()/(domains)()
)
else
"Invalid request. Provide with a valid location."
'
哪个returns:
Name of the university based the location United States is:
Middlesex County College
Middlesex Community College
Middlebury College
Middle Tennessee State University
Middle Georgia State College
Domain of the university based the location United States is:
middlesexcc.edu
middlesex.mass.edu
middlebury.edu
mtsu.edu
mga.edu
或者,您也可以直接查询 JSON,as that's what this API is also doing in the background。
$ xidel -s "https://raw.githubusercontent.com/Hipo/university-domains-list/master/world_universities_and_domains.json" -e '
parse-json($raw)()[country="United States" and starts-with(name,"Middle")]/concat(name," - ",join((domains)(),", "))
'
Middlebury College - middlebury.edu
Middle Georgia State College - mga.edu
Middle Tennessee State University - mtsu.edu
Middlesex Community College - mxcc.commnet.edu
Middlesex Community College - middlesex.mass.edu
Middlesex County College - middlesexcc.edu
(对于github-url-e '$headers'
returnsContent-Type: text/plain;
(而不是Content-Type: application/json
),所以你必须解析原始内容为 JSON。全局默认变量 $json
仅在输入为 Content-Type: application/json
)