3

I have a file that looks like 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 assume it's called foo.sh I'm on an Ubuntu 14.04 machine and i have root rights sudo su before I execute it.

If I call sh foo.sh the command line tells me:

# 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 the 4 commands after each other directly from the command line, well then it works. Here's the question: What's wrong? And why is it complaining about line 2 and 7 (they are empty)

Thanks (:

wawa
  • 4,816
  • 3
  • 29
  • 52
  • Check for line endings. You did not happen to edit this shell script under windows, did you? – fejese Jan 05 '15 at 00:17
  • As I recall, I didn't but it would be possible. How can I check the line endings? I use NetBeans as editor while on a GUI and otherwise nano when using the CLI. Can I check that with them? – wawa Jan 05 '15 at 00:32
  • I'm not using either of those but this might help: http://stackoverflow.com/questions/986421/how-do-i-switch-between-windows-and-unix-newlines-in-netbeans – fejese Jan 05 '15 at 00:35
  • Thanks for your help (: I was able to fix it and put it as an solution below, if anyone runs in the same problem, as I did. – wawa Jan 05 '15 at 00:50

2 Answers2

4

Thanks to the help of @fejese I managed to fix it.

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

first find out what file endings are used. Therefore we can use the command line:

file foo.sh

If this outputs something like:

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

If you have the CRLF line terminators stuff, then you have to fix it with the dos2unix program.

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

you only have to run the apt-get stuff (first line) if you don't have dos2unix installed yet. Now it should 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 about file, dos2unix and unix2dos you can find here: View line-endings in a text file

Community
  • 1
  • 1
wawa
  • 4,816
  • 3
  • 29
  • 52
1

As an alternative to installing dos2unix:

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

This command replaces all \r characters in the file in-place.

fejese
  • 4,601
  • 4
  • 29
  • 36
  • Seams to work as well. I personally prefer the dos2unix solution, because it's easier to remember and if there would be a change in line endings (I know, wont happen :P), the dos2unix will probably get updated, so we don't have to change anything ;) – wawa Jan 05 '15 at 13:27
  • Me too, but sometimes it's not possible or preferable not to install packages for a simple job like this. – fejese Jan 05 '15 at 13:40
  • Yet again a down vote without any suggestion why it's not a good answer. – fejese Jan 07 '15 at 23:00