Looping bash script files

I am trying to run a simple loop through all the script files, but this gives me the following error. The script is called test.sh and I use Cygwin on Windows 7.

My script:

 #!/bin/bash FILES = "/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*" for f in $FILES do echo "hello world" done 

Error:

 ./test.sh: line 2: FILES: command not found ./test.sh: line 4: syntax error near unexpected token ``$'do\r'' ./test.sh: line 4: ``do 

Before running the script, I converted all the files in the folder to unix format using the dos2unix .

+4
source share
4 answers

Try:

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

Thanks!

Brandon

+4
source

Combining other people's responses into one.

You have two problems with this script:

  • The script still has Windows line endings (which means \r , this is the character that Windows has at the end of the line, but Unix does not support). bcarlso indicated that one of them . Run dos2unix by script to sort it.

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

    The following is interpreted as an attempt to execute a FILES command (which does not exist) with arguments = "/bowtie..." .

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

    While the following is interpreted as assigning "/bowtie..." to the FILES variable:

     FILES="/bowtie-0.12.7-win32/bowtie-0.12.7/output_635_25bp/*" 
+2
source

try

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

i.e. no spaces around '='

+1
source

Try using the find command

 for i in `find /bow.../ -type f` do echo "hello world" done 

because ls will also return directories.

http://infofreund.de/bash-loop-through-files/

0
source

Source: https://habr.com/ru/post/1401088/


All Articles