I am new to shell scripting and I am trying to read an API to fetch the values...
But I have a couple of doubts to make it executable effectively.
It is a simple program to display the universities and the domains in the cities
The link to the API is : http://universities.hipolabs.com/search?name=middle
and my code is
# 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/ /%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
If I execute the program by providing with an invalid name like sh file_anme.sh "tur" instead of sh file_anme.sh "turkey" it should be printed like:
invalid request, provide with a valid location
but it is printing...
Name of the university based the location tur is:
domain of the university based the location tur is:
and
another doubt is, how to remove the quotes while displaying the output at the terminal? I have used the -r flag with the help of jq when fetching details of the domain(second part)...
I am providing it in the below:
#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'
But I need to display it as the following without the quotes:
domain of the university based the location United States is:
middlesexcc.edu
middlesex.mass.edu
middlebury.edu
mtsu.edu
mga.edu
for the reference, I am attaching the JSON data from the link mentioned above...
[{"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"}]
First question:
Check for an empty JSON message:
if [ "$response_contianing_university_data" = '[]' ]; then
echo "invalid request, provide with a valid location"
else
# etc
if [ -z "$(echo "$response_contianing_university_data" | jq '.[]')" ] or similar if you want. The processed JSON will be literally empty.Second question:
@sh operator in jq. Simply remove it.List the domains without quotes:
echo "$response_contianing_university_data" | jq -r '.[].domains[]'
Also:
$location is much clearer for example."), when echoing them in to jq.