0

Here is the error am getting after running the script below. I have read through the other responses for the similar error but cannot find an answer. can someone see what could be going wrong with the script below?

Thanks in advance.

Error:

'bash: lowerit.sh: line 7: syntax error near unexpected token `do
'bash: lowerit.sh: line 7: `  do

Script:

for x in `ls`
  do
  if [ ! -f $x ]; then
    continue
  fi
  lc=`echo $x  | tr '[A-Z]' '[a-z]'`
done
that other guy
  • 116,971
  • 11
  • 170
  • 194
Sam
  • 13
  • 3
  • 2
    Due to the error messages starting with `'` instead of ending with them, this looks like a file encoding issue. Can you run `cat -v lowerit.sh` and see if each line ends with `^M`? – that other guy Jan 25 '17 at 21:53
  • 1
    Don't use `ls` like this in the first place. Use `for x in *`. – chepner Jan 25 '17 at 21:55
  • The error message is telling you that the unexpected token is `do\r`. – chepner Jan 25 '17 at 21:58
  • That code runs for me. I think I saw a first version which was different (not just the formatting, at least the `ls` was not inside backticks). As another commenter noted, use `for x in *`, and also quote your `"$x"` expansion in both places. – Fred Jan 25 '17 at 22:04
  • @Fred If you look at the edit history, the *only* change made to the post was to indent the code to create a code block; no additional line breaks were added. – chepner Jan 25 '17 at 22:08
  • I still see `ls` without backticks in the edits view, and I copy and pasted the code I am referring to to indent it on my side (before refreshing and seeing the indented version), so there has to be something wrong with my browser then. – Fred Jan 25 '17 at 22:11
  • @chepner Ok, I understand, the backticks were showing as formatting, hence when I copy/pasted them, only the text was copied, not the backticks. – Fred Jan 25 '17 at 22:12
  • @thatotherguy - Yes, each line ends with ^M – Sam Jan 25 '17 at 22:28
  • @chepner - I made the changes "for x in * and also quoted "$x" in both the places - still get the same error. – Sam Jan 25 '17 at 22:29
  • Yes, you would. See the linked duplicate. Using `ls` was a separate problem, unrelated to your immediate error. – chepner Jan 25 '17 at 22:55
  • Thanks everyone; the issue was with the carriage returns and after using the "dos2unix" command, the script worked just fine :) – Sam Jan 26 '17 at 02:23

1 Answers1

-2

you need ; after `ls` in for statement or just break line before "do"

Milan Jaric
  • 5,556
  • 2
  • 26
  • 34