1

I'm attempting to read lines from a file and run some commands based on that file. However, only the first line of the file is read and nothing else is done. There are no error messages displayed, it seems as though the do while loop is ignoring the subsequent lines.

The contents of the file are file paths. I was able to get it to work in another fashion, but it gave me too much 'garbage' data and took longer to run as a result.

Example contents:

Users/username/rest/of/the/file/path/    
Users/username/rest/of/the/file/path/  
Users/username/rest/of/the/file/path/

Here is the problematic loop:

githubRepoList='githubcomparerepo.txt'
echo Start First Tag Check
while read l; do
    cd ~
    echo $l >> ~/testcsvtag1.txt
    cd $l
    pwd
    find $l -type f -name "*.csv" >> ~/testcsvtag1.txt
    printf "\n" >> ~/testcsvtag1.txt
done < $githubRepoList

The loop that works looks like this:

find / -name "l10n" >> l10n.txt
cat l10n.txt | grep github > l10n1.txt

filename='l10n1.txt'
echo Start
while read l; do
    cd ~
    echo $l >> ~/testcsvtag1.txt
    cd $l
    pwd
    find $l -type f -name "*.csv" >> ~/testcsvtag1.txt
    printf "\n" >> ~/testcsvtag1.txt
done < $filename

Some of the contents of the loop is just to monitor where it's at while I'm writing this script and for error checking, such as the cd ~ and pwd commands.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Thatsnotamuffin
  • 123
  • 1
  • 5
  • 16
  • What is the contents of `~/testcsvtag1.txt` after the loop is run? – that other guy Feb 19 '16 at 21:45
  • Perhaps `githubcomparerepo.txt` has \r (aka ^M) characters (DOS-file). Try removing them without a temp file using command substitution: .... `done < <( tr -d "\r" < ${filename})` – Walter A Feb 19 '16 at 21:47
  • @thatotherguy the csv files found in the first file path. It writes the first one correctly, but seems to ignore the second. – Thatsnotamuffin Feb 19 '16 at 21:58
  • @WalterA there were no issues running it but the same issue is persisting. There are no \r characters in the file path. At the moment it only contains two lines, I'm bypassing this issue at the moment by coding them in, but I would prefer to have a 'config' file to read and process data from. – Thatsnotamuffin Feb 19 '16 at 22:04
  • I thought you might have line endings with \r\n, not as a part of the path. Or maybe the second line does not end with a newline. Try `echo >> $filename` and start your script again. – Walter A Feb 19 '16 at 22:09
  • 2
    try using `read -r` in your loop, you can read about it here: http://wiki.bash-hackers.org/commands/builtin/read – jkdba Feb 19 '16 at 22:14
  • Or you can use `read -d \n` or whatever your line-ending is (\r etc.) to delimit your file. – jkdba Feb 19 '16 at 22:20
  • What is the content of `$l` *after* the loop. I suspect that githubcomparerepo.txt does not end with a newline, so that the second (and importantly, *last*) line causes `read l` to have a non-zero exit status, preventing the loop body from executing. Your second loop would work because `grep`, regardless of its input, will ensure that its output is a proper text file, with a newline terminating the last line. – chepner Feb 19 '16 at 22:20
  • 1
    read -r resolved the issue. I also tested the file by adding a new line at the end, that seemed to resolve it as well. Thank you @chepner and John K – Thatsnotamuffin Feb 19 '16 at 22:40
  • Possible duplicate of [Bash Scripting & Read File line by line](http://stackoverflow.com/questions/10929453/bash-scripting-read-file-line-by-line) – miken32 Feb 20 '16 at 05:09

0 Answers0