このスクリプトは、一連の検索用語を受け入れ、Google を検索するための書式設定された URL を返す必要があります。
$ ./google_search.sh albert einstein
https://www.google.com/search?q=albert+einstein
それはうまくいくので、特定のサイトを検索するオプションを追加するか、-s
または-S
フラグを使用してそのサイトを無視することにしました。
$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein+site%3Awikipedia.org
これは、スクリプトを初めて実行するときは機能しますが、その後のすべての試行で失敗します。
$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
新しいターミナル ウィンドウを開くか、ターミナルを再起動すると、この問題が解消され、失敗する前にもう一度試行できるようになります。
スクリプト:
#!/bin/bash
# original source of concatenate_args function by Tyilo:
# http://stackoverflow.com/questions/9354847/concatenate-inputs-in-bash-script
function concatenate_args
{
string=""
ignorenext=0
for a in "$@" # Loop over arguments
do
if [[ "${a:0:1}" != "-" && $ignorenext = 0 ]] # Ignore flags (first character is -)
then
if [[ "$string" != "" ]]
then
string+="+" # Delimeter
fi
string+="$a"
elif [[ $ignorenext = 1 ]]
then
ignorenext=0
else
ignorenext=1
fi
done
echo "$string"
}
qry="$(concatenate_args "$@")"
glink="https://www.google.com/search?q="
site=""
while getopts :s:S: opt; do
case $opt in
s) site="+site%3A$OPTARG" ;;
S) site="+-site%3A$OPTARG" ;;
esac
done
url=$glink$qry$site
echo $url
# open -a Firefox $url
このスクリプトの信頼性を高めるには、何を変更する必要がありますか?