2

I have these 2 line in my aaa.sh file:

#!/bin/bash
chmod +x  /home/tot/*.html

When I run it on AWS EC2 Linux:

$ sh aaa.sh

I got this message:

chmod: cannot access ‘\r’: No such file or directory
Keith Thompson
  • 242,098
  • 41
  • 402
  • 602
Tony
  • 553
  • 5
  • 8
  • 17
  • 8
    You have Windows line endings in your file (`\r\n`). It needs to have Unix-style line endings (`\n` only). – nobody Apr 03 '14 at 00:14
  • @AndrewMedico suggest that as an answer – that other guy Apr 03 '14 at 00:23
  • 2
    run the file through dos2unix or a similar utility – Duck Apr 03 '14 at 00:29
  • Thanks. How to heg rid of \r when I use vi editor on Linux? – Tony Apr 03 '14 at 00:31
  • `vi` tends to adapt to line endings; if you're editing a file with Windows-style endings, it might not let you remove them. Quit` vi and fix the file using `dos2unix`. (Be sure to read the man page; unlike most text filters, `dos2unix` replaces its input file rather than writing to `stdout`). – Keith Thompson Apr 03 '14 at 00:45
  • 3
    If you don't have `dos2unix`, you can probably install it; the package name is `dos2unix`. Or you can use `tr`, something like this: `tr -d '\r' < aaa.sh > aaa.sh.$$ && mv aaa.sh.$$ aaa.sh` – Keith Thompson Apr 03 '14 at 00:48
  • 2
    I faced the same issue on my windows machine, resolved it by using the above command. I was trying to resolve this issue for the last two days on windows, though I didn't get any issue on ubuntu. Thanks a lot @KeithThompson – Deepak Kumrawat Mar 19 '21 at 15:29

2 Answers2

4

You have Windows line endings in your file (\r\n). It needs to have Unix-style line endings (\n only).

You can do the conversion with the common dos2unix utility, or the set ff=unix command in vim.

nobody
  • 19,421
  • 17
  • 55
  • 76
0

chmod shouldn't care about the contents of your files. I bet you have a file with a control character in the filename itself. Might display as a question mark, but ls -b *.html should show you the culprit. Are your sure you want executable html files?

Jimko
  • 31
  • 4
  • Interesting suggestion, but it's more likely that the script itself has Windows-style line endings. Good point about executable HTML files. – Keith Thompson Apr 03 '14 at 04:50