Looping through files bash script Looping through files bash script unix unix

Looping through files bash script


Try:

for f in `ls /bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*`; do echo "hello world"; done

Thanks!

Brandon


Collating other folks' answers into a single one.

You've two problems with this script:

  • The script still has Windows line endings (that's what the \r refers to; it's the character that Windows has in its line endings, but Unix doesn't). bcarlso pointed that one out. Run dos2unix over the script to sort it out.

  • When assigning variables in a bash script, you cannot have spaces around the = sign. scibuff caught that one.

    The below gets interpreted as trying to run the command FILES (which doesn't exist) with the arguments = "/bowtie...".

    FILES = "/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*"

    Whereas the below is interpreted as assigning "/bowtie..." to the variable FILES:

    FILES="/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*"


try

FILES=/bow.../*for f in $FILESdo   echo "hello world"done

i.e. no spaces around ' = '