我写了下面的脚本:
#!/bin/bash host="www.myhost.com" IFS=$" " ; for x in $(cat foo.list) ; do srcURI=$(echo $x | awk '{print $1}') ; destURI=$(echo $x | awk '{print $2}') ; srcCURL=$(curl -s -H "Host: ${host}" -I "qualified.domain.local${srcURI}" |grep "Location:" | tr -d [[:cntrl:]]) ; destCURL=$(curl -s -H "Host: ${host}" -I "qualified.domain.local${srcURI}" |grep "301" | tr -d [[:cntrl:]]) ; echo " " echo " " echo -e "srcURI = ${srcURI}" echo -e "destURI = ${destURI}" echo -e "srcCURL = ${srcCURL}" echo -e "destCURL = ${destCURL}" echo -e "Host:DestURI = Location: http://${host}${destURI}" echo -e "Host:srcCURL = ${srcCURL}" echo " " echo " " # if [[ ${srcCURL} == "Location: http://${host}${destURI}" ]] ; then # echo -e "Good"; # else # echo -e "Bad"; # fi done
我有一个名为foo.list的文件,它有一个我想用for循环检查的URI列表; 它看起来像这样:
/ source / uri / here / dest / uri / here
/ source2 / uri / here / dest2 / uri / here
/ source3 / uri / here / dest3 / uri在这里
我试图使用awk来分配源$ 1和目的地$ 2,这工作,如果我有1 uri,但如果我有多个它似乎chomp的一部分。
代码看起来很乱,因为我已经有了用于debugging的随机回声。
在脚本结尾处(已注释掉),我正在比较源URLredirect的目标与可疑目标。
我哪里错了?
不知道我得到了所有的细节,但我会尝试这样的事情:
while read -r src dst; do redir="$( curl -q -s -S -o /dev/null -w '%{redirect_url}' qualified.domain.local"${src}" )" if [ $? -eq 0 -ax"$redir" = qualified.domain.local"${dst}" ]; then echo Good; else echo Bad; fi done <foo.list
谢谢lcd047!
使用你的东西,我有以下几点:
#!/bin/bash read -p "Enter the host: " host while read -r src dst; do srcCURL=$(curl -s -H "Host: ${host}" -I "some.domain.local${src}" |grep "Location:" | tr -d [[:cntrl:]]) ; dstURL="Location: http://${host}${dst}" #echo ${srcCURL} #echo ${dstURL} if [ $? -eq 0 -a "${srcCURL}" == "${dstURL}" ]; then echo -e "[\033[0;32mOK\033[0m]: ${src} \033[0;1mredirects to\033[0m ${dst}"; else echo -e "[\033[0;31mERROR\033[0m]: ${src} does not redirect to ${dst}"; fi done <foo.list
它完美地工作!