Search code examples
bashvariables

Calling another variable inside a variable assignment


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


Solution

  • @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.