0

What I want to do: I want to find all the products(URLs) which are not redirected.

To get the final URL after redirection I'm using curl command as follows:

curl -Ls -o /dev/null -w %{url_effective} "$URL"

This is working fine. Now I want to iterate over URLs to get which are the URLs that are not redirected and display them as output of program. I've the following code:

result=""
productIds=$1
for productId in $(echo $productIds | sed "s/,/ /g")
do
    echo "Checking product: $productId";
    URL="http://localhost/?go=$productId";
    updatedURL=`curl -Ls -o /dev/null -w %{url_effective} "$URL"`

    echo "URL : $URL, UpdatedUrl: $updatedURL";
    if [ "$URL" == "$updatedURL" ]
    then
            result="$result$productId,";
    fi
done

The curl command works only for the first product. But from 2nd to last product, all the URL and updatedURL are same. I can't understand the reason why? The productId is changing in every iteration, so I think it cannot be something related to caching.

Also, I've tried the following variant of curl also:

updatedURL=$(curl -Ls -o /dev/null -w %{url_effective} "$URL")

updatedURL="$(curl -Ls -o /dev/null -w %{url_effective} "$URL")"


Edit: After trying with debug mode and lot of different ways. I noticed a pattern i.e. If I manually hit the following on terminal:

curl -Ls -o /dev/null -w %{url_effective} "http://localhost/?go=32123"

Then in shell script these urls will work fine. But if I don't hit manually then curl will also not work for those products via shell script.

4
  • How are you invoking the script, i.e. what is $1/$productIds? Commented May 20, 2016 at 11:30
  • sh test.sh 123,456,789,221 is what I'm using to invoke the shell script. $1 is what I'm using to get user input inside script. Commented May 20, 2016 at 11:41
  • 1
    put #!/bin/bash -vx in the top line and make test.sh executable and then call test.sh without sh - should give lots of debug output so you can see if the URL assignment works Commented May 20, 2016 at 13:16
  • 1
    can you detail "curl will also not work via shell script", what exactly happens? can you try curl -L "$URL" echo $? updatedURL=`curl -Ls -o /dev/null -w %{url_effective} "$URL"` inside the loop to see what is so strange about these calls? Commented May 20, 2016 at 14:07

2 Answers 2

1

Just add #!/bin/bash to be the first line of shell. It does produce required output. Now invocation should be like this bash file.sh 123,456,789,221 shell and its output

Invocation via Bourne shell sh file.sh 123,456,789,221 does requires code changes.Do let me know if you require that too :)

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest changing your loop to something like this:

IFS=, read -ra productIds <<<"$1"
for productId in "${productIds[@]}"; do
    url=http://localhost/?go=$productId
    num_redirects=$(curl -Ls -o /dev/null -w %{num_redirects} "$url")
    if [ "$num_redirects" -eq 0 ]; then
        printf 'productId %s has no redirects\n' "$productId"
    fi
done

This splits the first argument passed to the script into an array, using a comma as the delimiter. The number of redirects is stored to a variable. When the number is zero, the message is printed.

I have to admit that I can't see anything inherently broken with your original approach so it's possible that there is something extra going on that we're not aware of. If you could provide a reproducible test case then we would be able to help you more effectively.

2 Comments

This loop doesn't helped. Same problem. It is doing the same thing as the previous one. Also, you might want to change product_ids in your read line to productIds.
Good point about the variable name, I forgot to change them both back after testing (I normally use underscores in shell scripts). I would still suggest this approach as it saves you using an external tool. Unfortunately I can't really debug without knowing what your variables are. I'd suggest using set -x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.