Find: missing argument for -exec when run from file but not from cli

I have a file similar to this

#!/bin/bash

find . -type f -exec chmod 644 {} \;
find . -type f -exec chown vagrant:www-data {} \;
find . -type d -exec chmod 755 {} \;
find . -type d -exec chown vagrant:www-data {} \;

      

Let's say it's called foo.sh

I'm on an Ubuntu 14.04 machine and I have root privileges sudo su

before I executed it.

If I call sh foo.sh

, then on the command line:

# sh foo.sh 
: not foundh: 2: foo.sh: 
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
: not foundh: 7: foo.sh: 

      

but when i run 4 commands after each other directly from command line then it works. Here's the question: what happened? And why is he complaining about lines 2 and 7 (they are empty)

Thank (:

+3


source to share


2 answers


Thanks to @fejese's help , I managed to fix this.

The problem was that the files had Windows / DOS line endings. Not sure why, maybe I opened it once on my windows machine. More important is how it happened, as how can I fix it for me.

first figure out which file endings are in use. Therefore, we can use the command line:

file foo.sh

      

If it produces something like:

foo.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

      

Once you have the material CRLF line terminators

, you need to fix it with the program dos2unix

.



sudo apt-get install dos2unix
dos2unix foo.sh
file foo.sh

      

you only need to run apt-get stuff (first line) if you don't have dos2unix already. It should now look something like this:

foo.sh: Bourne-Again shell script, ASCII text executable

      

And now you can run it without any problem using

sh foo.sh

      

Further reading of the file, dos2unix and unix2dos can be found here: Viewing lines in a text file

+4


source


As an alternative to installation dos2unix

:

sed -i -e "s/\r//g" foo.sh

      



This command replaces all \r

characters in the file in place.

+1


source







All Articles