Reputation: 317
I have written the following script:
#!/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
I have a file named foo.list which has a list of URIs I want to check using my for-loop; it looks like this:
/source/uri/here /dest/uri/here
/source2/uri/here /dest2/uri/here
/source3/uri/here /dest3/uri here
I'm trying to use awk to assign the source to $1 and the destination to $2, which works if I have 1 uri, but if I have multiple it appears to chomp parts of it.
The code looks a mess because I've got the random echos for debugging purposes.
At the end of script (which is commented out) I am trying to compare the destination of the source URL's redirect, with the suspected destination.
Where am I going wrong?
Upvotes: 3
Views: 589
Reputation: 317
Thanks lcd047!
Using what you put I've got the following:
#!/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
And it works flawlessly!
Upvotes: 2
Reputation: 5861
Not sure I got all the details right, but I'd try something like this:
while read -r src dst; do
redir="$( curl -q -s -S -o /dev/null -w '%{redirect_url}' qualified.domain.local"${src}" )"
if [ $? -eq 0 -a x"$redir" = qualified.domain.local"${dst}" ]; then echo Good; else echo Bad; fi
done <foo.list
Upvotes: 3