39
echo "Select your option:"
echo "1. Change ip address"
echo "2. Add route"
echo "3. Reboot"
echo "4. Exit"
read A
case $A in
    1)
            echo "Add Ip address"
            read IP
            echo "Add Netmask"
            read Netid
            echo "Add name of interface"
            read Interface
            ifconfig ${Interface} ${IP}/${Netid}
            if [ $? -ne 0 ];then
                    echo "Ip address not configured"
            fi
            ;;
    2)
            echo "Add Destination"
            read dst
            echo "Add Netmask"
            read Netid
            echo "Add Gateway"
            read gw
            route add $dst mask $Netid gw $gw
            if [ $? -ne 0 ];then
                    echo "Route not added"
            fi
            ;;
    3)
            reboot
            ;;
    4)
            echo "Bye"
            exit 0
            ;;
    default)
            echo "Wrong selection"
            exit 1
esac

Error:

[b104@b104 Downloads]$ ./NetworkUtility.sh 
./NetworkUtility.sh: line 1: $'\r': command not found
Select your option:
1. Change ip address
2. Add route
3. Reboot
4. Exit
1
': not a valid identifier 7: read: `A
./NetworkUtility.sh: line 8: $'\r': command not found
./NetworkUtility.sh: line 9: syntax error near unexpected token `newline'
'/NetworkUtility.sh: line 9: `case $A in 
[b104@b104 Downloads]$ 
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
Manthan Patel
  • 429
  • 1
  • 4
  • 6
  • 5
    Your script file contains DOS/Windows style line endings (`\r\n`), this is what confuses your shell. Try to save it with unix line endings (`\n`). – Anders Lindahl Sep 04 '13 at 07:59
  • 9
    You can execute `tr -d "\r" < NetworkUtility.sh > cleaned.sh`, then check if `./cleaned.sh` works better. – Anders Lindahl Sep 04 '13 at 08:14
  • 2
    The most unfortunate thing about this question is how long it is -- there's absolutely no reason to have a more-than-20-line script when a two-line one (with a shebang and a single empty line) would produce the exact same problem. – Charles Duffy Sep 18 '17 at 21:32
  • 1
    If you developing using IntelliJ on Windows but executing scripts on Linux, you might have such issue due to default intelliJ line endings configuration. You can solve it by configuring Unix endings as described here: https://www.jetbrains.com/help/idea/configuring-line-endings-and-line-separators.html – Illidan Dec 10 '18 at 07:08

3 Answers3

46

It seems that you have Windows style line endings (\r\n) - you need to change them to unix style (\n). If you have dos2unix installed you could use it. You could also do it using sed or awk.

dstronczak
  • 2,406
  • 4
  • 28
  • 41
33

Its End of Line(EOL) conversion issue when script is written in windows using some editors like notepad, notepad++(tested).

Sed , tr may solve the issue in case of you just need to run script, however if you are developing shell script its become annoying each time first convert using, sed/tr command then run your script.

Notepad++ has an option to convert END OF LINE CONVERSION(EOL). How to do that: go to Edit > EOL Conversion > select Unix/OSX

Here you go, save script file and run it.

Here is screenshot of the same

pankajh
  • 331
  • 3
  • 3
20

The error happens, because shell doesn't understand DOS/Windows-like line endings and it expects LF instead of CRLF.

You'll need to first configure your editor to use Unix-like line endings or use dos2unix command to change it automatically, e.g.

dos2unix ./NetworkUtility.sh

Read more details at: '\r': command not found.

If you're using Vagrant, check: Windows CRLF to Unix LF Issues in Vagrant

kenorb
  • 155,785
  • 88
  • 678
  • 743