• Uncategorized

About shell : \r-command-not-found-duplicate

Question Detail

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:

[[email protected] 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 
[[email protected] Downloads]$ 

Question Answer

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.

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

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

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.