Print | Rate this content

Red Hat Enterprise Linux - Shell Script Error "syntax error near unexpected token" or "bad interpreter"

Title: Red Hat Enterprise Linux - Shell Script Error "syntax error near unexpected token" or "bad interpreter"
Object Name: mmr_kc-0110573
Document Type: Support Information
Original owner: KCS - Linux
Disclosure level: Public
Version state: final
Environment
FACT:Red Hat Enterprise Linux 
FACT: bash shell
Questions/Symptoms
SYMPTOM:Syntax error near unexpected token
SYMPTOM:Bad interpreter
SYMPTOM:Attempting to run a bash shell script received from another source returns one of the following errors:

# ./test_script5.sh
-bash: ./test_script5.sh: /bin/bash^M: bad interpreter: No such file or directory

# sh ./test_script5.sh
'/test_script5.sh: line 9: syntax error near unexpected token `do
'/test_script5.sh: line 9: `  do
Cause
CAUSE:The script syntax is correct and runs on the original system.  Here is a short test script illustrating the problem:

# cat test_script5.sh
#!/bin/bash
IP=0
loop=0
while true
do
  loop=$(($loop+1))
  echo Loop number $loop
  for i in 0 1 2 3 4
  do
    scp bfile"$i" $IP:/root/
    if [ $? != 0 ]
    then
      echo scp to failed exiting
      exit 1
    fi
  done
done

Running the script as is doesn't provide many clues as to the source of the problem.  However, if one runs it using the shell options "-vx", this will show what commands are being run and what values are being stored in the script variables.  This sheds more light on the source of the "syntax error":

# sh -vx ./test_script5.sh
#!/bin/bash
IP=0
+ IP=$'0\r'
loop=0
+ loop=$'0\r'
while true
do
  loop=$(($loop+1))
  echo Loop number $loop
  for i in 0 1 2 3 4
  do
'/test_script5.sh: line 9: syntax error near unexpected token `do
'/test_script5.sh: line 9: `  do


The "-vx" option reveals several non-printable carriage return characters "\r" that were added to the end of each variable line.  This is the source of the problem where the shell is trying to interpret the unprintable character. In this case, the original source editor for the script introduced word wrap characters.  During the ftp transfer of the file to the Linux system, non-printable characters were introduced into the file that could not be seen simply by using cat to display the file or using vi to edit the file.
Answer/Solution
FIX:On the Linux system:

1) Use the "cat" command to display the contents of the script.

2) Then use copy and paste to copy what was displayed and paste it into a new file.  The new file will visually appear to be the same as the old, but it will no longer contain the non-printable characters.  For example:

# cat test_script6.sh
#!/bin/bash
IP=0
loop=0
while true
do
  loop=$(($loop+1))
  echo Loop number $loop
  for i in 0 1 2 3 4
  do
    scp bfile"$i" $IP:/root/
    if [ $? != 0 ]
    then
      echo scp to failed exiting
      exit 1
    fi
  done
done

3) Set the permissions on the newly created file so it can be executed:

# chmod 755 test_script6.sh

4) The script now runs as expected using either invocation method:

# ./test_script6.sh
Loop number 1
root@0's password:
scp to failed exiting

Or:

# sh ./test_script6.sh
Loop number 1
root@0's password:
scp to failed exiting

5) Run the new script using “sh -vx <script>” and notice that the non-printing characters "\r" are no longer there, allowing the script to run as expected:

# sh -vx ./test_script6.sh
#!/bin/bash
IP=0
+ IP=0
loop=0
+ loop=0
while true
do
  loop=$(($loop+1))
  echo Loop number $loop
  for i in 0 1 2 3 4
  do
    scp bfile"$i" $IP:/root/
    if [ $? != 0 ]
    then
      echo scp to failed exiting
      exit 1
    fi
  done
done
+ true
+ loop=1
+ echo Loop number 1
Loop number 1
+ for i in 0 1 2 3 4
+ scp bfile0 0:/root/
root@0's password:
+ '[' 1 '!=' 0 ']'
+ echo scp to failed exiting
scp to failed exiting
+ exit 1

© Copyright 2013 Hewlett-Packard Development Company, L.P.

Legal Disclaimer: Products sold prior to the November 1, 2015 separation of Hewlett-Packard Company into Hewlett Packard Enterprise Company and HP Inc. may have older product names and model numbers that differ from current models.

Provide feedback

Please rate the information on this page to help us improve our content. Thank you!
Document title: Red Hat Enterprise Linux - Shell Script Error "syntax error near unexpected token" or "bad interpreter"
Document ID: mmr_kc-0110573-3
How helpful was this document?
How can we improve this document?
Note: Only English language comments can be accepted at this time.
Please wait while we process your request.