0

My line of code is :

while IFS= read -r line
do
    echo "$line"
    echo "**"
    CURL_REQ=$(curl -is "${line}"  -H 'Pragma: akamai-x-get-extracted-values' | grep 'AKA_PM_PROPERTY_NAME')
    echo "$line"
    echo "$CURL_REQ"

After doing some trial an error, I now understood that inside variable declaration of "CURL_REQ" ${line} is not being recognized. However the echo "$line" is working perfectly without any issues outside the variable declaration.

When I replace "${line}" with a static hostname, the curl works fine and I can see the value on echo "$CURL_REQ"

Reproducible example :

#!/bin/bash
INPUT_FILE="/domain.txt"
EXPECTED_HEADER="AKA_PM_PROPERTY_NAME"
declare -i COUNT=1

echo "*************************************************************************"

while IFS= read -r line
do
    CURL_REQ=$(curl -is ${line} -H 'Pragma: akamai-x-get-extracted-values' | grep 'AKA_PM_PROPERTY_NAME')
    echo "$line"
    echo $CURL_REQ
    PROP_VALUE=$(echo ${CURL_REQ} | cut -c 51-)
    echo $PROP_VALUE
    
done < "$INPUT_FILE"

Input file domain.txt should contain the lines below :

myntra.com ndtv.com

mhs
  • 3
  • 2
  • 2
    replace `echo "$line"` with `declare -p line` and see if it has a trailing `\r` – oguz ismail Jan 05 '23 at 10:48
  • 2
    Why do you think that `${line}` is not recognized? What happens if you replace the declaration with `CURL_REQ=$(echo "$line")`? My suspicion is that grep does not work as you intended. Can you provide a minimal reproducible example, i.e. in this case a short example of a file which should produce some expected output? – January Jan 05 '23 at 10:49
  • Here's a small reproducible example : INPUT_FILE="/domain.txt" EXPECTED_HEADER="AKA_PM_PROPERTY_NAME" declare -i COUNT=1 echo "*************************************************************************" while IFS= read -r line do CURL_REQ=$(curl -is ${line} -H 'Pragma: akamai-x-get-extracted-values' | grep 'AKA_PM_PROPERTY_NAME') echo "$line" echo $CURL_REQ PROP_VALUE=$(echo ${CURL_REQ} | cut -c 51-) echo $PROP_VALUE done < "$INPUT_FILE" – mhs Jan 05 '23 at 12:24
  • input text file containing these two lines : myntra.com ndtv.com – mhs Jan 05 '23 at 12:25
  • Almost certain your input file contains DOS line endings. See [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – tripleee Jan 05 '23 at 13:42
  • [Don't](https://stackoverflow.com/a/673940/8656552) use all [caps](https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html). – Paul Hodges Jan 05 '23 at 14:30
  • Please [edit] your question to add details. Don't use comments. Code is unreadable in comments, and comments are show in order of votes, not by time posted or author. See also [ask]. – Robert Jan 05 '23 at 15:49
  • Sorry. New to Stack overflow. I have added the code in the details now. – mhs Jan 05 '23 at 19:28
  • @tripleee Thanks I think what is the problem. I created a new input file and ran the script. It works. My original file was downloaded off internet. – mhs Jan 05 '23 at 19:38

1 Answers1

0

@mhs Based on your provided script, seems it is working for me - I have only changed the INPUT_FILE="/domain.txt" to INPUT_FILE="./domain.txt" to read the file from the same directory that I am running the script.

Here are the details, please compare them with yours and you'd find the issue on your side.

IMHO the domain.txt file's context or permissions (accessibility) should be the problem.

Note: The script.sh and the domain.txt files are both in the same directory (my user's home directory) and they have proper permissions relevant to user (my user) that I'm running the script

If you didn't find the issue, I'll suggest re-format your question and provide step by step description and details as I have in the following.

  • The script.sh file content I'm running:
#!/bin/bash

INPUT_FILE="./domain.txt"
EXPECTED_HEADER="AKA_PM_PROPERTY_NAME"
declare -i COUNT=1

echo "*************************************************************************"

while IFS= read -r line
do
    CURL_REQ=$(curl -is ${line} -H 'Pragma: akamai-x-get-extracted-values' | grep 'AKA_PM_PROPERTY_NAME')
    echo "$line"
    echo $CURL_REQ
    PROP_VALUE=$(echo ${CURL_REQ} | cut -c 51-)
    echo $PROP_VALUE

done < "$INPUT_FILE"

  • The domain.txt file content:
myntra.com
ndtv.com
  • Here are the files permissions:
-rw-r--r-- 1 vrej vrej   20 Jan  5 17:18 domain.txt
-rwxr-xr-x 1 vrej vrej  440 Jan  5 17:18 script.sh
  • The output that I get is:
*************************************************************************
myntra.com
X-Akamai-Session-Info: name=AKA_PM_PROPERTY_NAME; value=www.myntra.com_ssl
value=www.myntra.com_ssl
ndtv.com
X-Akamai-Session-Info: name=AKA_PM_PROPERTY_NAME; value=www.ndtv.com-EON-1501
value=www.ndtv.com-EON-1501
  • My BASH version is:
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Vab
  • 412
  • 1
  • 11
  • Thank you so much for such a detailed answers. You are right. The problem was on the content of the Input file. What I did is created a new input file and ran the same script. It works whereas the original input file which I got causes the problem. Another user @tripleee also mentioned the bash script is encoding sensitive. Thanks alot :) – mhs Jan 05 '23 at 19:34
  • Glad that you find the issue, good luck. – Vab Jan 08 '23 at 08:53