linux - 我的 shell 脚本中的 URL 中断

标签 linux bash shell unix url

#!/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

出于某种原因,每当我输入引号中包含多个单词的选项时,我的网址就会 split 。如果我输入加勒比海盗,即使网址打印为 http://www.imdb.com/find?ref_=nv_sr_fn&q=pirates 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 作为你的临时文件(而不是你的主目录)

关于linux - 我的 shell 脚本中的 URL 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40192292/

相关文章:

linux - 在 Bash 中使用变量的值

linux - 无法从 bash 脚本中获取带有空格的路径?

linux - Linux 控制台中的 system() 和 type 命令之间的主要区别是什么?

shell - unix 中 shell 命令的 "Event not found"错误

linux - 如何获取shell脚本中使用的命令列表?

linux - 即使在物理断开连接并重新连接后,ssh 连接仍然存在

ruby-on-rails - libmysqlclient-dev 安装失败

php - 如何永久 chmod 777 PHP

c - 在 C 中读取 CAN 总线显示 29 位 CAN ID 的 CAN ID 不正确

Bash 传递变量名作为参数