0

i need check status of some link listen in a file called paths.txt, i have write a simple for loop but not work

for LINK in $(cat paths.txt)
do
  CURL="$(curl -I $LINK 2>/dev/null | head -n 1 | cut -d$' ' -f2)"
  if [ "${CURL}" = "200" ]
  then
    echo ${LINK} >> true.txt
  else
    echo ${LINK} >> false.txt
  fi
done

If i launch the curl command from terminal i read the right status, instead if i make an echo of CURL variable i not have any output

3
  • instead of CURL="$(curl -I $LINK 2>/dev/null | head -n 1 | cut -d$' ' -f2)", do echo $LINK. Do you get the proper content of your file ? Commented Mar 9, 2017 at 13:58
  • yes, i have tried and work, i have insert echo -e "\n$LINK\n" and show correctly all link sepatared by line Commented Mar 9, 2017 at 13:59
  • are there any spaces or special characters (like * or ?) in the URLs? Commented Mar 9, 2017 at 14:01

2 Answers 2

2

Run the script like this: bash -x myscript and watch the executed commands.

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

2 Comments

i have found a \r at the end of any link, i have correct the file format and now works perfectly, thanks!
This isn't really an answer; it's a general comment that can direct one to the answer.
0

don't use cat in bash scripts, because is bad practice and can be problems with big files.

SCRIPTDIR=`dirname -- "$0"`
for siteUrl in $( < "$SCRIPTDIR/paths.txt")
do
    if [[ -z "$siteUrl" ]]; then break; fi
    if [[ "$(curl --write-out "%{http_code}\n" --silent --output /dev/null ${siteUrl} )" = "200" ]];  then
        echo ${siteUrl} >> true.txt
    else
        echo ${siteUrl} >> false.txt
    fi
done

Comments

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.