U
U
UniverseElement2020-10-26 19:01:08
bash
UniverseElement, 2020-10-26 19:01:08

How to read a file line by line in bash?

I'm learning bash. I'm trying to create symlinks to files, but I'm getting the following error:
ln: unable to access '/usr/share/nginx/html/blabla.txt'$'\r': No such file or directory

Add ' to the end of the line $'\r'.
What is \r is understandable, but where does the dollar come from and how to get rid of them?

The code:

while read str
        do
                $(ln /usr/share/nginx/html/$str /usr/share/nginx/html/tst/$str)
        done < /home/user/list.txt

Answer the question

In order to leave comments, you need to log in

4 answer(s)
U
UniverseElement, 2020-10-27
@UniverseElement

Solved the problem like this:

while read str; do 
                str=${str/'\r'/} 
                ln "/usr/share/nginx/html/$str" "/usr/share/nginx/html/tst/$str" 
done < /home/user/list.txt

A
AVKor, 2020-10-26
@AVKor

It is not necessary to use screw EOL. In nix EOL = \n, and in Windows EOL = \r\n

X
xotkot, 2020-10-27
@xotkot

this is due to editing linux files from under windows / macos
for conversion there are special utilities in linux - these are dos2unix / mac2unix and unix2dos / unix2mac reverse them
p.s.
$() is superfluous here, and it is advisable to quote unknown paths, otherwise a line with a space will suddenly come across:

while read str; do
  ln "/usr/share/nginx/html/$str" "/usr/share/nginx/html/tst/$str"
done < /home/user/list.txt

S
Saboteur, 2022-02-17
@saboteur_kiev

The problem should be solved like this:

dos2unix /home/user/list.txt
while read; do
  ln -s "/usr/share/nginx/html/$REPLY" "/usr/share/nginx/html/tst/$REPLY"
done < /home/user/list.txt

PS By the way, in your example, it is simply indicated ln without ln -s, that is, you create hardlinks instead of symbolic links.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question