Shell not parsing $ sign from file

PFB my shell script which reads a csv file to process it

###############################################
fn_java(){
        echo JAVA_TOP is $JAVA_TOP
        echo "cp $filename $filepath/$filename"
        cp $filename $filepath/$filename
}

#########################################
JAVA_TOP=/dcypri/applmgr/common/java
if [ -f DATA.csv ]
then
        while read myline
        do
        echo $myline
        filename="$(echo $myline | cut -d, -f1)"
        filepath="$(echo $myline | cut -d, -f2)"
        echo $filename
        echo $filepath
        if [ -f $filename ]
        then
         fn_java $filename $filepath

        fi
        done < DATA.csv
fi

And the data file contains two rows.

XXEasyShipPage.java,/dcypri/applmgr/common/java
XXEasyShipPage.java,$JAVA_TOP

It works perfectly for the first row, but not for the second row.

$JAVA_TOP point to /dcypri/applmgr/common/java

It literally tries to

cp XXEasyShipPage.java $JAVA_TOP/XXEasyShipPage.java 

instead of

cp XXEasyShipPage.java /dcypri/applmgr/common/java/XXEasyShipPage.java

your JAVA_TOP variable must be set before the declaration of your fn_java(){ ... } function.
(if this is not sufficient, export it)

If you want to pass some argument to you fn_java function, you must refer to them as $1 $2 etc ... inside your fn_java function.

JAVA_TOP=/dcypri/applmgr/common/java
export JAVA_TOP
fn_java(){
        echo JAVA_TOP is $JAVA_TOP
        echo "cp $1 $2/$1"
        cp "$1" "$2/$1"
}

...

fn_java "$filename" "$filepath"

Hi ctsgnb,

I have tried changing to $1, $2 as well as the $JAVA_TOP variable has been set. But still it is not working. When I echo $JAVA_TOP, it gives me the desired o/p, but cp doesnt work as it throws below error

XXEasyShipPage.java,$JAVA_TOP
XXEasyShipPage.java
$JAVA_TOP
JAVA_TOP is /dcypri/applmgr/common/java
cp XXEasyShipPage.java $JAVA_TOP/XXEasyShipPage.java
cp: cannot create regular file `$JAVA_TOP/XXEasyShipPage.java': No such file or directory

How does your current code look like ?

please provide the output of

cat yourscript

Please also provide cat DATA.csv

one simple thing is before reading the file, replace the variable in the file.

sed "s:\$JAVA_TOP:$JAVA_TOP:" DATA.csv > /tmp/DATA.csv

read the value from "/tmp/DATA.csv" instead of "DATA.csv"

Kalpeer. Thanks for your reply. It worked, :slight_smile: however the folder is not $JAVA_TOP always and will vary each time.
When I sed my filepath variable with same filepath variable, again i am not able to get it.

Is there a way to get this without hard-coding as $JAVA_TOP?

Can somebody kindly help on this?

instead of

cp $filename $filepath/$filename

try this:

eval cp "\"$filename\"" "\"$filepath/$filename\""

WOW .. thanks a lot...

Hi kalpeer. This is just an example of how your code might be made to work. Your setup will only work with eval. Be advised though that the use of eval introduces a security risk, since anyone with access to your data file can have access to the rights of the person executing the script. Also in this case it relies on a environment variable and that can also possibly be manipulated. I would use a different setup that does not require the use of eval....