Bash looking in different directory for file that isn't referenced in command

When I run the below bash I get the expected output, which is the sum of all matching targets less than 20 in $file1 . The filename in the directory is fixed (in bold).

for file1 in /home/cmccabe/Desktop/test/panel/reads/16-0000_EPIL70.txt ; do
        bname=`basename $file1`
        pref=${bname%%_*.txt}
     awk '{gsub(/[()]/,_)} \
count[$5]==""{ count[$5]=0 } 
            $7 < 20{ count[$5]++} 
END{
              for(k in count) 
                 printf "%s %d\n",  k, count[k]
}' $file1 > /home/cmccabe/Desktop/test/panel/20x/base/16-0000_allepil70.bed
done

The desired bash is the exact same except the unique numerical digits, 16-0000 are read into a variable {pref} . This is because there can be multiple files with the same extension, but the digits are always unique.

for file1 in /home/cmccabe/Desktop/test/panel/reads/${pref}_EPIL70.txt ; do
        bname=`basename $file1`
        pref=${bname%%_*.txt}
     awk '{gsub(/[()]/,_)} \
count[$5]==""{ count[$5]=0 } 
            $7 < 20{ count[$5]++} 
END{
              for(k in count) 
                 printf "%s %d\n",  k, count[k]
}' $file1 > /home/cmccabe/Desktop/test/panel/20x/base/${pref}_allepil70.bed
done

awk: fatal: cannot open file `/home/cmccabe/Desktop/test/panel/reads/16-0000_epil70lessthan20xregions.txt_EPIL70.txt' for reading

The file that it is looking for is not in that directory, but not sure why it is even looking for it as I do not reference it at all in the command or do I? Thank you :).

ls -l /home/cmccabe/Desktop/test/panel/reads
total 10952
-rw-rw-r-- 1 cmccabe cmccabe 11214550 Feb  7 07:27 16-0000_EPIL70.txt

I suggest putting set -x in your script so you can tell what its doing.

1 Like

Actually, I think I see the problem now. The starting value of $pref probably isn't what you think it is. The values of a 'for' loop aren't necessarily valid filenames.

In the future, please post more complete code. The code that sets variables you're using is important - garbage in, garbage out.

1 Like

Thank you very much for both tips set -x showed what you suspected. :slight_smile: