簡體   English   中英

我的Shell腳本中的URL URL在空格處斷開

[英]URL breaking of at space in my shell script

#!/bin/sh

scriptname=$0   # $0 is the name of the program
verbose=no
probeonly=no

usage () {
   cat <<EOF
Usage: $scriptname [-o] [-a] [-d] [-c] [-r] [-p] [-h] [-w] movie name
   -a   Lead actor and Actress
   -c   Cast
   -d   Duration
   -h   Help
   -H   History of previous searches by the user
   -o   Overview(Actor,Actress,Ratings,Director and Plot)
   -p   Download the poster
   -r   Rating
   -w   Movies to be released this week

EOF
   exit 0
}
getUrl()
{
    url_first_part="http://www.imdb.com/find?ref_=nv_sr_fn&q="
    url_last_part="&s=all"
    url="${url_first_part}${OPTARG}${url_last_part}"
    echo "${url}"
    content=$(wget ${url} -q -O ~/movie_list) #to save the page source in a local file called movie_list
    count=$(grep -Po -m 1 "(?<=td class=\"primary_photo\"> <a href=\").*?(?=\s*ref_=fn_al_tt_1\")" ~/movie_list|head -1) 
    part_to_be_added="ref_=fn_al_tt_1"
    final_url="www.imdb.com$count$part_to_be_added"
    echo $final_url
    rm ~/movie_list
}
print()
{
    echo "$movie"
}

unset flag #this is to unset the value of flag if it had any value previously
while getopts ":a:c:d:hHo:p:r:w" opt
do
    case $opt in
        a)
          movie="${OPTARG}"
          #echo "-a was triggered, Parameter: $OPTARG" >&2
          getUrl
          flag='1' #we set flag to 1 so as to check if an option was pased or not.Since it skips the getopt part if no option was passed
          ;;
        c)
              getUrl
          #echo "-c was triggered, Parameter: $OPTARG" >&2
          flag='1'
          ;;
            d)
              getUrl
          #echo "-d was triggered, Parameter: $OPTARG" >&2
              flag='1'
          ;;
            h)usage
              flag='1'
          ;;
            H)
              getUrl
          #echo "-H was triggered, Parameter: $OPTARG" >&2
              flag='1'
          ;;
            o)
              getUrl
          #echo "-o was triggered, Parameter: $OPTARG" >&2
              flag='1'
          ;;
            p)
              getUrl
          #echo "-p was triggered, Parameter: $OPTARG" >&2
              flag='1'
          ;;
            r)
              getUrl
          #echo "-r was triggered, Parameter: $OPTARG" >&2
              flag='1'
          ;;
            w)
              getUrl
          echo "-w was triggered, Parameter: $OPTARG" >&2
              flag='1'
          ;;
        \?)
          echo "Invalid option: -$OPTARG" >&2
              flag='1'
          usage
          ;;
        :)
          echo "Option -$OPTARG requires an argument." >&2
              usage
          flag='1'
          ;;
      esac
done
if [ -z "$flag" ] #Here we check if an option was passed or not
then
    echo "No options were passed"
    usage
fi

由於某種原因,每當我輸入一個引號中包含多個單詞的選項時,我的URL就會拆分。如果我輸入了加勒比海盜信息,即使該URL打印為http://www.imdb.com/find? ref_ = nv_sr_fn&q = pirates of carribean&s = all。wget訪問的網站是http://www.imdb.com/find?ref_=nv_sr_fn&q=pirates 我是unix的新手,請幫忙解決這個問題。我無法調試。

您幾乎保護了每個字符串以防空格,但這里沒有:

content=$(wget ${url} -q -O ~/movie_list)

應該:

content=$(wget "${url}" -q -O ~/movie_list)

順便說一句,您寧願使用/tmp作為臨時文件(而不是主目錄)

有一些錯誤。 在腳本級別,您需要在${url}周圍加上引號:

content=$(wget "${url}" -q -O ~/movie_list) #to save the page source in a local file called movie_list

這樣可以防止空格導致URL作為多個參數發送給wget

可以與上述方法結合使用的另一種方法是正確地形成URL。 +%20替換這些空格:

url="${url_first_part}$(perl -pe 'chomp;s/ /+/g; s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg;' <<<"${OPTARG}" )${url_last_part}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM