我是 shell 腳本的新手,我正在嘗試讀取 API 以獲取值...
但我有一些疑問,以使其有效執行。
這是一個簡單的程式來顯示城市中的大學和域名
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=$1
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/ / /g' )
#user_input_location_of_the_university=$(echo "$user_input_location_of_the_university | sed 's/ / /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”以外的引號?
作為參考,我附上了上述鏈接中的 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"}]
uj5u.com熱心網友回復:
第一個問題:
- 如果地名不匹配,則 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 將是空的。
第二個問題:
- 正在添加域周圍的單引號,因為您
@sh在jq. 只需將其洗掉。
列出不帶引號的域:
echo "$response_contianing_university_data" | jq -r '.[].domains[]'
還:
- 你的變數名太長了。
$location例如更清楚。 - 您拼錯了“contianing”(應該是“包含”)。
- 在將變數
"回顯到jq.
uj5u.com熱心網友回復:
有趣的 API(JSON 檔案)。如果你用西德爾, 那么你就不需要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."
'
回傳:
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,因為這也是該 API 在后臺執行的操作。
$ 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'回傳Content-Type: text/plain;(而不是Content-Type: application/json),因此您必須將原始內容決議為 JSON。全域默認變數$json僅在輸入為 時設定Content-Type: application/json)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400485.html
