0
#!/bin/bash

while read p
do
  echo "$p"
done < numbers.txt

This script is only reading and allowing me to print .sh files. I have tried it with .txt files (like above but it does not print anything.)

  • Please add a sample of the contents of `numbers.txt`. The filename suffix means nothing on Unix systems. Both `.sh` and `.txt` files are just text files. – Kusalananda Feb 18 '19 at 19:26
  • I added a screen shot of both files – Cesa Salaam Feb 18 '19 at 19:29
  • Is `numbers.txt` properly line-terminated? What happens if you change `while read p` to `while read p || [ -n "$p" ]`? – steeldriver Feb 18 '19 at 19:30
  • @steeldriver that worked! What exactly was the problem? – Cesa Salaam Feb 18 '19 at 19:32
  • 2
    Please, [don't post images of text](https://unix.meta.stackexchange.com/questions/4086/psa-please-dont-post-images-of-text). – Kusalananda Feb 18 '19 at 19:32
  • 1
    Your file does not contain a single _terminated_ line. `read` can only read complete lines. – Kusalananda Feb 18 '19 at 19:33
  • 1
    I guess it would be more correct to say that `read` reads the line (and assigns it to variable `p`) but returns false, having encountered EOF while doing so: hence the body of the `while` loop is not executed. – steeldriver Feb 18 '19 at 19:40
  • @steeldriver That would be a more correct way of phrasing it, yes. In fact, what I said was not correct. – Kusalananda Feb 18 '19 at 19:41

1 Answers1

0

That's because you are printing test not variable p Try this:

#!/bin/bash

while read p
do
  echo "$p"
done < numbers.txt

Test:

$ cat numbers.txt
1
2
345
678
9
$ bash script.sh
1
2
345
678
9
$
Matej
  • 311
  • 1
  • 10
  • my apologies. I forgot to edit that ou, I was just testing something there. But even when changing to echo "$p", nothing prints – Cesa Salaam Feb 18 '19 at 19:24
  • @CesaSalaam It works for me, are you sure that numbers.txt file is not empty? – Matej Feb 18 '19 at 19:27