Linux 我的shell脚本中位于空格处的URL中断 #/垃圾箱/垃圾箱 scriptname=$0#$0是程序的名称 冗长=否 可能=不 用法(){ cat
您保护了几乎所有字符串不受空格的影响,但这里没有:Linux 我的shell脚本中位于空格处的URL中断 #/垃圾箱/垃圾箱 scriptname=$0#$0是程序的名称 冗长=否 可能=不 用法(){ cat,linux,bash,shell,unix,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 C
#!/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
应该是:
content=$(wget ${url} -q -O ~/movie_list)
顺便说一句,你宁愿使用
/tmp
作为你的临时文件(而不是你的主目录)有一些错误。在脚本级别,你需要引用${url}
:
content=$(wget "${url}" -q -O ~/movie_list)
这可以防止空格导致URL作为多个参数发送到wget
另一个可与上述内容结合使用的选项是正确形成URL。将这些空格替换为+
或%20
:
content=$(wget "${url}" -q -O ~/movie_list) #to save the page source in a local file called movie_list
url=“${url\u first\u part}$(perl-pe'chomp;s/+/+/g;s/([^A-Za-z0-9\+-])/sprintf(%%%02X),ord($1))/seg;”它之所以有效,是因为由于保护引号,wget
将被传递一个单独的参数,而不是几个,并用空格分隔。
url="${url_first_part}$(perl -pe 'chomp;s/ /+/g; s/([^A-Za-z0-9\+-])/sprintf("%%%02X", ord($1))/seg;' <<<"${OPTARG}" )${url_last_part}"