Echo not working with $


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo not working with $
# 1  
Old 07-16-2015
Echo not working with $

Code:
$cat FILE.txt

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

where
Code:
$PATH 1 = /root/FILE_DR/file1.txt
$PATH 2 = /root/FILE_DR/file2.txt

Code:
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 -
Code:
$PATH1/file1.txt
$PATH2/file2.txt

expected -
Code:
/root/FILE_DR/file1.txt
/root/FILE_DR/file1.txt


Last edited by rbatte1; 07-21-2015 at 08:01 AM.. Reason: Added CODE tags for all relevant parts
# 2  
Old 07-16-2015
There is not $PATH1 $PATH2 defined in the script.

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

Thanks
This User Gave Thanks to PikK45 For This Post:
# 3  
Old 07-16-2015
$PATH1 and $PATH2 are visible for all users or environment variables set in the .proifile
# 4  
Old 07-16-2015
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.
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 07-16-2015
Is there a way to find out put from double echo something like below

Code:
var=`echo `echo $I``

$I containes the $ and second echo should display the actual path , Any examples ?
# 6  
Old 07-16-2015
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:

Code:
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

Code:
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

This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Echo command not working in the script

HI I have and echo command which works perfectly in the shell but when i execute in the script it gives me an error code query is as below QUERY=`echo "Select Severity,Dupl_count,Creation_Time,Last_Received,Node_Name,Node_Name,Object,Message_Group,Message_Text,Last_Annotation from " \ ... (2 Replies)
Discussion started by: Jcpratap
2 Replies

3. Shell Programming and Scripting

echo two variables like the paste command is not working

Dear all, I have two files like this file1 A B C D E F file2 1,2 3,4 5,6 I want this output output_expected A B 1,2 C D 3,4 E F 5,6 (3 Replies)
Discussion started by: valente
3 Replies

4. Shell Programming and Scripting

echo is not working as expected

for i in `cat /export/home/afahmed/Arrvial_time.txt` do echo $i echo $i | awk '$3 < $D { print $4 }' >> dynamic_DF.txt; done When i echo, its echo as Nov 15 02:24 /export/home/pp_adm/inbound//wwallet_20111115.txt where i expect it to be Nov 15 02:24... (7 Replies)
Discussion started by: afahmed
7 Replies

5. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

6. Shell Programming and Scripting

why the set rr='echo string|cut not working

I am new to the c shell script, can you let me know why the set rr= is not working. C shell script #! /bin/csh Set tt= 12345_UMR_BH452_3_2.txt set rr='echo $tt | cut –d”_” -f1' syntax error (4 Replies)
Discussion started by: jdsignature88
4 Replies

7. UNIX for Dummies Questions & Answers

Is echo $variable >> text.txt working in MacOSX?

Hi New at this, but want to learn more. I'm trying this as an Shell Command in MacOSX; newdate='<TIME>' echo $newdate >> /Users/ttadmin/Desktop/test.txt And it don't work. But if I just use; echo <TIME> >> /Users/ttadmin/Desktop/test.txt (<TIME> is an variable that one program... (6 Replies)
Discussion started by: jackt
6 Replies

8. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

9. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

10. Shell Programming and Scripting

Echo working funny

Hello, this is my first post and question. I have search before for this problem but didn't find anything similar. My case: I have a string inside the variable string1 like this: string1="lala lele lili lolo lulu" When I do echo of it, it appears like this: echo $string1 lala lele lili... (8 Replies)
Discussion started by: hemtar
8 Replies
Login or Register to Ask a Question