Site icon UnixArena

Why shell script is giving syntax errors without any issue? The same scripts work in another system

Shell Script - carriage return

Shell Script - carriage return

Why shell script is giving syntax errors despite no issue with the script? You might check the script multiple times but still couldn’t figure out what’s causing the problem? I personally come across a similar problem whenever I copy the script from a windows laptop to *NIX systems. The error message contains “syntax error near unexpected token” but very difficult to find where is the exact problem since vi editors will not show some of the control characters. There is a significant difference between Windows and *NIX systems on text handling.

Windows-style uses carriage returns (\r\n) but the Unix system uses line endings (\n).

How to find script has Windows-Style carriage returns?

1. View the script using the cat command first.

$ cat simple.sh
#!/bin/bash
echo "This is test script"
            if [[ $? -ne 0 ]]; then
                    echo "Unable to execute the command";
            else
                    echo "Command executed successfully"
            fi
#Sample script

2. View the file using “cat -evt” command. Here you can see the Control+M character at the end of every line.

#!/bin/bash^M$
echo "This is test script"^M$
            if [[ $? -ne 0 ]]; then^M$
                    echo "Unable to execute the command";^M$
            else ^M$
                    echo "Command executed successfully"^M$
            fi
#Sample script

Script Executiuon with Windows-Style Character

Let’s execute the script with windows style character and observe the errors.

$ bash -x simple.sh
' echo 'This is test script
This is test script
simple.sh: line 5: syntax error near unexpected token `else'
'imple.sh: line 5: `            else
$

How to remove windows style carriage return ?

1. Use sed command to remove those windows style carriage return.

$ sed -e "s/\r//g" simple.sh > simple_v1.sh

2. Check the newly created file simple_v1.sh. Now you can see that those control + M characters are removed at end of every line.

#!/bin/bash$
echo "This is test script"$
            if [[ $? -ne 0 ]]; then$
                    echo "Unable to execute the command";$
            else $
                    echo "Command executed successfully"$
            fi$
$

Note: Even “sdiff” utility can’t find the difference between Unix style file vs Windows-style file.

$ sdiff simple.sh simple_v1.sh
#!/bin/bash                                                   | #!/bin/bash
echo "This is test script"                                    | echo "This is test script"
            if [[ $? -ne 0 ]]; then                           |             if [[ $? -ne 0 ]]; then
                    echo "Unable to execute the command"      |                     echo "Unable to execute the command";
            else                                              |             else
                    echo "Command executed successfully"      |                     echo "Command executed successfully"
            fi                                                |             fi
                                                              |

3. Let’s execute the newly created script file and check it.

$ bash -x simple_v1.sh
+ echo 'This is test script'
This is test script
+ [[ 0 -ne 0 ]]
+ echo 'Command executed successfully'
Command executed successfully

Script has been successfully executed after removing the Windows-style characters. There are a few others commands that can help you to remove those special characters.

Using Perl ,

$ perl -p -e 's/\r//g' simple.sh > simple_v2.sh

Using vi binary mode,

Edit the file using “vi -b simple.sh”

in ESC mode type: :%s/^M$//

Note: To enter ^M, type CTRL-V + M. That is, hold down the CTRL key then press V and M in succession. Then save and close the file.

Exit mobile version