1
while
    read -r line 
        do  
           readlink -f $line > a.txt 
done < $1

I have a file which contains 30 symbolic destination pathnames passed by $1. I want to read this file line by line and wants to save ls results of each path in a file a.txt. The problem coming is that its only taking last path name mentioned in $1. Its ignoring upper 29 lines. Why?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Pompy
  • 41
  • 9

1 Answers1

1

Change

readlink -f $line > a.txt

to

readlink -f "$line" >> a.txt

The >> appends to a file or creates the file if it doesn't exist.

The > overwrites the file if it exists or creates it if it doesn't exist.

https://serverfault.com/a/196735

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Thanks sir. it worked!!! Thank you so much. :) I would like to give you reputation but cant give. :( – Pompy Dec 07 '17 at 09:27
  • Or better yet put the redirect outside the loop `done >a.txt` – tripleee Dec 07 '17 at 09:31
  • ... and fix the quoting. https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Dec 07 '17 at 09:32
  • I can't delete anything but you should be able to delete it yourself. I marked it as a duplicate for the time being. – tripleee Dec 07 '17 at 10:07