Echo not working with $

$cat FILE.txt

$PATH1/file1.txt
$PATH2/file2.txt

where

$PATH 1 = /root/FILE_DR/file1.txt
$PATH 2 = /root/FILE_DR/file2.txt
for I in `cat FILE.txt`

do
v=`echo $I`

echo $v

		
		if [ -e "$v" ]

			then 

    			rm $v 

			else 

			echo "file $v doesnot exist"

		fi
done

output -

$PATH1/file1.txt
$PATH2/file2.txt

expected -

/root/FILE_DR/file1.txt
/root/FILE_DR/file1.txt

There is not $PATH1 $PATH2 defined in the script.

Can you please paste the complete script or the proper input & output data?

Thanks

1 Like

$PATH1 and $PATH2 are visible for all users or environment variables set in the .proifile

Assumed that PATH1 and PATH2 WERE defined, it still wouldn't work as it would need a second expansion by the shell after $I or $v have been expanded. You need to use eval for that, but this can be dangerous and is generally disadvised.

2 Likes

Is there a way to find out put from double echo something like below

var=`echo `echo $I`` 

$I containes the $ and second echo should display the actual path , Any examples ?

You also shouldn't use for to read a file. If you have envsubst you can pipe it to a while read loop. See below how this looks:

mute@tiny:~$ cat file-list
$PATH1/file1.txt
$PATH2/file2.txt
mute@tiny:~$ export PATH1=/path1/here; export PATH2=/another/path
mute@tiny:~$ envsubst < file-list
/path1/here/file1.txt
/another/path/file2.txt
mute@tiny:~$

Use like so

mute@tiny:~$ ./script
file /path1/here/file1.txt doesn't exist
file /another/path/file2.txt doesn't exist
mute@tiny:~$ cat script
#!/bin/sh

envsubst '$PATH1 $PATH2' < FILE.txt |
while read file; do
        if [ -e "$file" ]; then
                echo rm "$file"
        else
                echo "file $file doesn't exist"
        fi
done
1 Like