Skip to content

Instantly share code, notes, and snippets.

@naranyala
Last active August 8, 2023 03:09
Show Gist options
  • Save naranyala/f8983d223d1020ae73987ebeffecd4da to your computer and use it in GitHub Desktop.
Save naranyala/f8983d223d1020ae73987ebeffecd4da to your computer and use it in GitHub Desktop.
direct search from terminal (cli) into default browser | desktop and mobile (termux)
#!/bin/bash
# Function to handle errors and exit the script
function handle_error {
echo "Error: $1"
exit 1
}
# Associative array to store website URLs
declare -A websites
websites["Google Search"]="https://google.com/search?q=%s"
websites["Stack Overflow"]="https://stackoverflow.com/search?q=%s"
websites["Youtube"]="https://www.youtube.com/results?search_query=%s"
websites["Github"]="https://github.com/search?q=%s&ref=simplesearch&type=repositories"
websites["Twitter"]="https://twitter.com/search?q=%s"
websites["Hacker News"]="https://hn.algolia.com/?query=%s"
websites["Google Images"]="https://www.google.com/search?tbm=isch&q=%s"
# websites[""]=""
# Prompt the user to select a website
PS3="Select a website (type the number and press Enter): "
select website_name in "${!websites[@]}"; do
if [ -n "$website_name" ]; then
break
else
echo "Invalid selection. Please try again."
fi
done
# Get the URL for the selected website from the associative array
website_url="${websites[$website_name]}"
# Prompt the user to enter a search query
read -p "Enter the search query: " search_query
# Replace "%s" in the URL with the search query
full_url="${website_url//\%s/$search_query}"
# Open the URL in the default web browser
if command -v xdg-open >/dev/null; then
nohup xdg-open "$full_url" &
elif command -v start >/dev/null; then
nohup start "$full_url" &
else
handle_error "Cannot open the web browser."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment