-1

This question already has an answer here:

I have a script from the book Data Science at Command Line, Jeroen Janssens like this

curl -s http://www.gutenberg.org/cache/epub/76/pg76.txt |
 tr '[:upper:]' '[:lower:]' | grep -oE '\w+' | sort |
 uniq -c | sort -nr | head -n 10

and saved it as top-world-1.sh

From terminal of Ubuntu 15.10 64bit, I put a command ./top-words-1.sh and I got output likes

./top-words-1.sh: line 1: $'\r': command not found
./top-words-1.sh: line 2: $'\r': command not found

At once, I guessed that was caused by multiple line and I remove '\n' places to make a sentence like this

curl -s http://www.gutenberg.org/cache/epub/76/pg76.txt |tr '[:upper:]' '[:lower:]' | grep -oE '\w+' | sort |uniq -c | sort -nr | head -n 10

But I got

: invalid number of lines

I'm totally new in programming bash script. Could you please give me any advices? Thanks in advance

Following comments below, I have:

  • checked permission

enter image description here

  • added #!/bin/bash at the top of file and got output bash: ./top-words-1.sh: /bin/bash^M: bad interpreter: No such file or directory
  • run with sudo

marked as duplicate by David Foerster, TheWanderer, edwinksl, waltinator, Anwar Sep 30 '16 at 6:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    What kind of editor are you using to create your top-words-1.sh file? The errors suggest it is saving with DOS-style line endings: make sure you are using an editor that saves with Unix-style endings. You should also add a #!/bin/bash shebang at the top of the file to ensure it is run as a bash script. – steeldriver Nov 21 '15 at 19:16
  • Hi, I use nano editor. I have add `#!/bin/bash' at top of file and when I ran it, I showed "bash: ./top-words-1.sh: /bin/bash^M: bad interpreter: No such file or directory". I have check different script, they work in stead of this one. I also check execute permission, permission is correct – Bryan Nov 22 '15 at 10:17
  • Create an entirely new script, don't copy-paste text and see what happens. Also, try running dos2unix on the script that has the error. – muru Nov 22 '15 at 10:23
  • Don't post screenshots of text. Just copy the text, please. – muru Nov 22 '15 at 10:23
1

It does work for me. Try:

cat > script.sh <<EOF
#! /bin/bash
 curl -s http://www.gutenberg.org/cache/epub/76/pg76.txt |tr '[:upper:]' '[:lower:]' | grep -oE '\w+' | sort |uniq -c | sort -nr | head -n 10
EOF
 chmod +x script.sh
 ./script.sh

And it gives me the right answer. Make sure you are not adding any extra (hidden) characters to the file. You can also copy/paste the command into the shell to make sure it works for you.

0

either run your script with the command: bash ./scriptname , or chmod +x script then: ./scriptname

command not found probably means its not executable.

  • Sadly, I doesn't work. – Bryan Nov 22 '15 at 10:19
0

I used dos2unix command to solve my problem. Thank @muru for your comment. The reason is explained by @steeldriver

Not the answer you're looking for? Browse other questions tagged or ask your own question.