Syntax error near unexpected token elif

./chkf: line 30: syntax error near unexpected token `elif' '/chkf: line 30: `elif [ -f "$object" ] ; then if [ -d "$object" ] ; then message="$message a directory" elif [ -f "$object" ] ; then...
./chkf: line 30: syntax error near unexpected token `elif'
'/chkf: line 30: `elif [ -f "$object" ] ; then


if [ -d "$object" ] ; then
    message="$message a directory"
elif [ -f "$object" ] ; then
    message="$message a regular file."
else
    message="$message not a known file type"
fi

Also this,

./chkf: line 38: syntax error near unexpected token `else'
'/chkf: line 38: `else 

if [ -w "$object" ] ; then
    write="writeable"
else 
    write="not writeable"
fi

What is wrong with this? It seems to be correct. I tried so many variations and cannot figure out what is wrong. Is there some kind of invisible character? If so, is there a command to strip it?

Edit: When I add #!/bin/bash at the top, I get the following error:

interpreter "/bin/bash" not found
file link resolves to "/usr/bin/bash"
-bash: ./chkf: /bin/bash^M: bad interpreter: No such file or directory

ajreal's user avatar

ajreal

46.4k10 gold badges86 silver badges119 bronze badges

asked Nov 15, 2010 at 7:03

Strawberry's user avatar

StrawberryStrawberry

64.7k56 gold badges144 silver badges195 bronze badges

4

It’s your line endings. Transferring it from Windows has left the CR/LF line endings on.

When I create a script then manually add the CR characters, I get exactly the same error:

qq.sh: line 3: syntax error near unexpected token `elif'
'q.sh: line 3: `elif [ 1 == 1 ] ; then

You can fix it by removing the CR character from CR/LF line endings.

cat script.sh | sed 's/r$//' >newscript.sh

answered Nov 15, 2010 at 7:15

paxdiablo's user avatar

paxdiablopaxdiablo

838k230 gold badges1561 silver badges1929 bronze badges

3

Two ways to resolve this

1) Using Sed:-

Syntax

sed -i 's/r//' filename.txt

2) Using dos2unix command

Syntax

dos2unix fileName.txt fileName.txt

answered Sep 25, 2017 at 21:27

Sireesh Yarlagadda's user avatar

it looks like you’ve got the «dos problem», embedded control-M’s in your file. fix it with sed:


sed -i 's/r//' chkf

answered Nov 15, 2010 at 7:13

jcomeau_ictx's user avatar

jcomeau_ictxjcomeau_ictx

37.3k6 gold badges92 silver badges105 bronze badges

Now that you’ve added the additional error message, I have a thought: the ^M is r, which is the Mac OS X line ending or part of the Windows line ending — Linux uses n only as EOL. If you edit in vim, you should be able to see the ^M if it’s not right through the file.

answered Nov 15, 2010 at 7:12

Chris Morgan's user avatar

Chris MorganChris Morgan

83.6k24 gold badges206 silver badges209 bronze badges

2

10 More Discussions You Might Find Interesting

1. Ubuntu

Syntax error near unexpected token `(‘

detect_mouse_mvt.sh
/home/andy/bin/detect_mouse_mvt.sh: line 4: syntax error near unexpected token `(‘
/home/andy/bin/detect_mouse_mvt.sh: line 4: `fh = file(‘/dev/input/mice’)’
#!/bin/bash
#
#
fh = file(‘/dev/input/mice’)
while True:
fh.read(3)
print ‘Mouse… (15 Replies)

Discussion started by: drew77

2. Shell Programming and Scripting

Syntax error near unexpected token `else’

Hello every one!!

I don’t know where I am going wrong but I am finding it difficult to clear this error of syntax error near unexpected token `else’

I am writing a simple shell script to find a file in a directory and if found execute that else return an error to the log file
(14 Replies)

Discussion started by: masubram

3. Shell Programming and Scripting

Syntax error near unexpected token `|’

Hi All;

I try to write a bash code and I am using command substitution. My code is like:

#!/bin/bash

IP=»10.0.0.1 10.0.0.2″
PORT=»22 80″
USERNAME=»admin»
SCRIPT_HOST=»adminHost»
HOME_DIR=/home/admin
SCRIPT_DIR=$HOME_DIR/scripts
script=»sudo /my_remote_script.sh»
SSH=»/usr/bin/ssh… (7 Replies)

Discussion started by: Meacham12

4. Shell Programming and Scripting

Syntax error near unexpected token `(‘

What do I do here?

#!/bin/bash
payload=-1 AND 1=IF(21,BENCHMARK(5000000,MD5(CHAR(115,113,108,109,97,112))),0)#
hash=`echo -n $payload md5sum tr -d ‘n’ sed ‘ss-sg’ md5sum tr -d ‘n’ sed ‘ss-sg’`
curl —data cs2=chronopay&cs1=$payload&cs3=$hash&transaction_type=rebill… (2 Replies)

Discussion started by: iiiiiiiiiii

5. Shell Programming and Scripting

Syntax error near unexpected token ‘elif’

Solaris 10
This is my script:

#!/bin/bash
#Script to print number of users and print list of them
NO=`awk < /etc/passwd -F: ‘{ print $1 }’ | wc -l`
echo There are $NO users on system.
echo «Do you want me to list them? (y or n):»
read YORN
if ]
awk < /etc/passwd -F: ‘{ print $1 }’… (5 Replies)

Discussion started by: kaustubh

6. Shell Programming and Scripting

Syntax error near unexpected token `}’ please help

I’m going mad not being able to get this to work. im assuming its only a simple mistake but its driving me bonkers trying to find it.

Please if you can help me it would save me pulling my hair out!!

Thanks

#!/bin/bash -xv
#
#Config
name=»TEST Server»
+ name=’TEST Server’… (6 Replies)

Discussion started by: Fisheh

7. Shell Programming and Scripting

syntax error near unexpected token `=’

Hi all,
This is a script which converts hex to bin. However am finding an error while executing

syntax error near unexpected token `=’

`($hexfile, $binfile) = @ARGV;’

I am running using ./fil.pl <hexfile> <binfile>

###################################################
#
# this script… (3 Replies)

Discussion started by: jaango123

8. Shell Programming and Scripting

Syntax error near unexpected token `done’

Hi all,
Here is a simple script that is working in one server and is giving a syntax error in other server. Can somebody help me ?

#!/bin/bash
# ftp files
done < $file

errors:

I tried..with no success:
if ; then (21 Replies)

Discussion started by: Lenora2009

9. UNIX for Advanced & Expert Users

Syntax error near unexpected token

Hi,
When I run the below shell script I’m getting the error » syntax error near unexpected token `»

Script:
REM :: File Name : Refresh_OTL.bat
REM :: Parameters : %1 — Region
REM :: : %2 — Cube Type
REM :: :
REM :: Notes :
REM ============================== set ENVIRONMENT… (2 Replies)

Discussion started by: tomailraj

10. UNIX for Dummies Questions & Answers

syntax error at line 33: `elif` unexpected

#!/bin/sh

echo «Choose option: e, d, l, t, p, or x.»
read option

if test $option = e

then

echo «Filename?»
read file

if test ! -f $file
then
echo «No such file»
else
echo «Yes its a file»
fi
(4 Replies)

Discussion started by: hazy

  • Home
  • Forum
  • The Ubuntu Forum Community
  • Ubuntu Specialised Support
  • Development & Programming
  • Programming Talk
  • [SOLVED] Syntax error near unexpected token ‘elif’

  1. Question Syntax error near unexpected token ‘elif’

    Hi, my friend and I are working on a private server running on 10.04 and when we run the linux_installer.sh we get the error:

    PHP Code:


    linux_installer.shline 77syntax error near unexpected token 'elif'
    linux_installer.shline 77'  elif [ "${option}" = "b" ]; then' 



    Here is the original code for the file:

    PHP Code:


        elif "${option}"b" ]; thenecho
            
    rm -rf "${bkpath}/logon_backup.sql"
            
    rm -rf "${bkpath}/character_backup.sql"
            
    echo " [Deleting Old Backups] Finished�"echo
            
    mysqldump -${server} --user=${user} --port=${port} --password=${pass} ${ldb} > "$bkpath}/logon_backup.sql"
            
    echo " [Backing Up Logon Database] Finished�"mysqldump -${server} --user=${user} --port=${port} --password=${pass} ${cdb} > "${bkpath}/character_backup.sql"
            
    echo " [Backing Up Char Database] Finished�"echo
            
    �cho " [Backing Up] Finished�" 



    I’ve tried searching for answers but all I could find was when people don’t have ‘then’ included in the code after the ‘elif’ command. Any help will be appreciated. Thanks


  2. Re: Syntax error near unexpected token ‘elif’

    If «elif» is unexpected the problem is likely before it, not after.


  3. Re: Syntax error near unexpected token ‘elif’

    Ok, i have posted the entire document on pastebin, it is 157 lines so no too big, thanks for any help

    http://pastebin.com/8U1kNg5J

    Last edited by Deadlyhugs; November 5th, 2011 at 08:07 PM.

    Reason: added link


  4. Re: Syntax error near unexpected token ‘elif’

    You have matching «done»s for all your «for»s? I think 61 might be the problem.


  5. Re: Syntax error near unexpected token ‘elif’

    Quote Originally Posted by gsmanners
    View Post

    you have matching «done»s for all your «for»s? I think 61 might be the problem.

    +1


  6. Re: Syntax error near unexpected token ‘elif’

    tried that and unfortunately no change…we are trying something different but thanks all


  7. Re: Syntax error near unexpected token ‘elif’

    Quote Originally Posted by Deadlyhugs
    View Post

    tried that and unfortunately no change…we are trying something different but thanks all

    If I insert «done» just above line 65 (the one that does echo » Adding Adding Stored Procedures Complete»‘)(<sic> for the «Adding» stutter) then the syntax error disappears.


  8. Re: Syntax error near unexpected token ‘elif’

    ofnuts: If I insert «done» just above line 65 (the one that does echo » Adding Adding Stored Procedures Complete»‘)(<sic> for the «Adding» stutter) then the syntax error disappears.

    Thanks for this, however it did not work here either. I got another error when doing that.

    Code:

    sh ./SkyFireDB/linux_installer.sh
    ./SkyFireDB/linux_installer.sh: line 67: syntax error near unexpected token `done'
    ./SkyFireDB/linux_installer.sh: line 67: `      done'

    before it was:

    Code:

    sh ./SkyFireDB/linux_installer.sh
    ./SkyFireDB/linux_installer.sh: line 44: logo: command not found
     i - Install Clean World Database
     u - Update World Database
     x - Exit Tool
    
     Enter option:  i
    
    
     [Cleaning World DB] Finished...
     Adding Stored Procedures
    ls: cannot access ./main_db/procs/*.sql: No such file or directory
     [1/0] import: *.sql
    ./SkyFireDB/linux_installer.sh: line 64: ./main_db/procs/*.sql: No such file or directory
     Adding Adding Stored Procedures Complete
     Importing world data
    ls: cannot access ./main_db/world/*.sql: No such file or directory
     [1/0] import: *.sql
    ./SkyFireDB/linux_installer.sh: line 74: ./main_db/world/*.sql: No such file or directory
    
     [Importing] Finished...
    
     Press any key to continue...

    Any other ideas? We are at our wits end with this and everyone else seems to have no problems. I know we are missing something, just trying to figure out what that something is.

    Thanks

    PS… Also the term done shows in Red Highlight as if it is linked to nothing at all. This is all being done in terminal window.

    Last edited by Deadlyhugs; November 6th, 2011 at 08:25 PM.

    Reason: Added info PS


  9. Re: Syntax error near unexpected token ‘elif’

    Quote Originally Posted by Deadlyhugs
    View Post

    Thanks for this, however it did not work here either. I got another error when doing that.

    Code:

    sh ./SkyFireDB/linux_installer.sh
    ./SkyFireDB/linux_installer.sh: line 67: syntax error near unexpected token `done'
    ./SkyFireDB/linux_installer.sh: line 67: `      done'

    before it was:

    Code:

    sh ./SkyFireDB/linux_installer.sh
    ./SkyFireDB/linux_installer.sh: line 44: logo: command not found
     i - Install Clean World Database
     u - Update World Database
     x - Exit Tool
    
     Enter option:  i
    
    
     [Cleaning World DB] Finished...
     Adding Stored Procedures
    ls: cannot access ./main_db/procs/*.sql: No such file or directory
     [1/0] import: *.sql
    ./SkyFireDB/linux_installer.sh: line 64: ./main_db/procs/*.sql: No such file or directory
     Adding Adding Stored Procedures Complete
     Importing world data
    ls: cannot access ./main_db/world/*.sql: No such file or directory
     [1/0] import: *.sql
    ./SkyFireDB/linux_installer.sh: line 74: ./main_db/world/*.sql: No such file or directory
    
     [Importing] Finished...
    
     Press any key to continue...

    Any other ideas? We are at our wits end with this and everyone else seems to have no problems. I know we are missing something, just trying to figure out what that something is.

    Thanks

    PS… Also the term done shows in Red Highlight as if it is linked to nothing at all. This is all being done in terminal window.

    Maybe you should show us the whole code as it looks now.


  10. Re: Syntax error near unexpected token ‘elif’

    Quote Originally Posted by Deadlyhugs
    View Post

    Thanks for this, however it did not work here either. I got another error when doing that.

    Code:

    sh ./SkyFireDB/linux_installer.sh
    ./SkyFireDB/linux_installer.sh: line 67: syntax error near unexpected token `done'
    ./SkyFireDB/linux_installer.sh: line 67: `      done'

    before it was:
    [CODE]sh ./SkyFireDB/linux_installer.sh

    1) with the correction I indicated, the added «done» ends up on line 65.
    2) if you call the shell with «sh ./SkyFireDB/linux_installer.sh» it is not executed by bash as intended but by dash. This may or may not explain the problem. Make iti executable and call it directly or at least call it with bash.


Bookmarks

Bookmarks


Posting Permissions

Модератор: Bizdelnick

AlexandrMakukhin

Сообщения: 67
ОС: Kubun9.10/10.04/SUSE11.2/WinXP
Контактная информация:

Решено: syntax error near unexpected token `elif’

Здравствуйте!

При попытки собрать мультизагрузочный диск при помощи MultiCD с двумя ISO-файлами
1. kubuntu-10.10-desktop-i386.iso — 695.7 Mb.
2. sgd_0.9588.iso — 3.4 Mb.
для записи на 700-мегабайтный диск, получаю такую ошибку:

alexander@ZXDSL831AII:/media/big-home/Видео/Linux/ISO-образы/multicd> chmod +x multicd*.sh
alexander@ZXDSL831AII:/media/big-home/Видео/Linux/ISO-образы/multicd> sudo ./multicd*.sh
root’s password:
Memtest86+

Continuing in 2 seconds — press Ctrl+C to cancel
Downloading SYSLINUX…
Unpacking and copying files…
Downloading memtest86+ 4.10 from memtest.org…
Writing isolinux.cfg…
./multicd-5.9.sh: line 2679: syntax error near unexpected token `elif’

Тут выложил список файловой иерархии /media/big-home/Видео/Linux/ISO-образы/multicd .

AlexandrMakukhin

Сообщения: 67
ОС: Kubun9.10/10.04/SUSE11.2/WinXP
Контактная информация:

Re: Решено: syntax error near unexpected token `elif’

Сообщение

AlexandrMakukhin » 21.10.2010 15:51

Код: Выделить всё

alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> chmod +x multicd*.sh
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> sudo ./multicd*.sh
root's password:
./multicd-6.0.sh: line 93: ./functions.sh: Нет такого файла или каталога
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> ls -a
.  ..  kubuntu-10.10-desktop-i386.iso  multicd-6.0.sh  sgd_0.9588.iso
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0>

Аватара пользователя

Rootlexx

Бывший модератор
Сообщения: 4439
Статус: GNU generation
ОС: Debian GNU/Linux

AlexandrMakukhin

Сообщения: 67
ОС: Kubun9.10/10.04/SUSE11.2/WinXP
Контактная информация:

Re: Решено: syntax error near unexpected token `elif’

Сообщение

AlexandrMakukhin » 21.10.2010 17:35

Код: Выделить всё

alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> sudo ./multicd*.sh
root's password:
./multicd-6.0.sh: line 93: ./functions.sh: Нет такого файла или каталога
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> sudo ./multicd*.sh
Memtest86+

Continuing in 2 seconds - press Ctrl+C to cancel
Downloading SYSLINUX...
Unpacking and copying files...
Downloading memtest86+ 4.10 from memtest.org...
Writing isolinux.cfg...
./multicd-6.0.sh: line 2586: syntax error near unexpected token `elif'
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> ls -a
.             kubuntu-10.10-desktop-i386.iso  multicd-working
..            memtest                         sgd_0.9588.iso
functions.sh  multicd-6.0.sh                  syslinux.tar.gz
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0> cd /media/big-home/MultiCD-6.0/multicd-working/
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working> ls -a.  ..  boot
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working> cd /media/big-home/MultiCD-6.0/multicd-working/boot/
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working/boot> ls -a.  ..  isolinux  memtest
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working/boot> cd /media/big-home/MultiCD-6.0/multicd-working/boot/isolinux/
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working/boot/isolinux> ls -a
.  ..  chain.c32  isolinux.bin  isolinux.cfg  memdisk  menu.c32  vesamenu.c32
alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working/boot/isolinux>

Аватара пользователя

Rootlexx

Бывший модератор
Сообщения: 4439
Статус: GNU generation
ОС: Debian GNU/Linux

AlexandrMakukhin

Сообщения: 67
ОС: Kubun9.10/10.04/SUSE11.2/WinXP
Контактная информация:

Re: Решено: syntax error near unexpected token `elif’

Сообщение

AlexandrMakukhin » 21.10.2010 18:15

Получилось!

Код: Выделить всё

alexander@ZXDSL831AII:/media/big-home/MultiCD-6.0/multicd-working/boot/isolinux> cd /media/big-home/multicd-6.0
alexander@ZXDSL831AII:/media/big-home/multicd-6.0> chmod +x multicd*.sh
alexander@ZXDSL831AII:/media/big-home/multicd-6.0> sudo ./multicd*.sh
root's password:
Memtest86+

Continuing in 2 seconds - press Ctrl+C to cancel
Downloading SYSLINUX...
Unpacking and copying files...
Downloading memtest86+ 4.10 from memtest.org...
Writing isolinux.cfg...
Building CD image...
У вас есть почта в /var/spool/mail/alexander
alexander@ZXDSL831AII:/media/big-home/multicd-6.0>

HI,

Im having problems with my bash script that I am using for automated zenwork imaging. I always get a elif error on line 23. Error message is Syntax error near unexpected token ‘elif’ bash scripting line 23. Can anyone please help.
Script is shown below.

mount dev/sda2 -t ext2 mnt/harddisk
Option=»»
while [ «$Option» != «4» ]
do
echo «»
echo «THH Data Migration — Zen Imaging»
echo «———————————«
echo «1            View ZEN Partition Info»
echo »                                «
echo «2             Take Backup Image of PC»
echo »                                «
echo «3             Restore WinXPBase      «
echo »                                «
echo «4        Back to Bash Prompt    «
echo »                                «
echo «5             Exit & Reboot          «
echo «———————————«
echo «Please Enter Your Choice:»
read Option;
if [ «$Option» = «1» ]; then
      img dump geo
      echo «View Partition Info»;
elif [ «$Option» = «2» ]; then
      ImageNAME=»»
      while [ «$ImageNAME» = «» ]
      do
            echo «Please Enter Asset Number For Backup of Image: «;
            read ImageNAME;
      done
      PartNo=»»
      while [ «$PartNo» = «» ]
      do
            echo «Please Enter Windows Partition No from Step 1: «;
            read PartNo;
      done
            if [ «$PartNo» = «1» ]; then
            img ml mnt/harddisk/PCBackups/$ImageNAME.zmg x2 x3 x4 x5 x6 x7 x8
            elif [ «$PartNo» = «4» ]; then
            img ml mnt/harddisk/PCBackups/$ImageNAME.zmg x1 x2 x3 x5 x6 x7 x8
            fi
elif [ «$Option» = «3» ]; then
      PartNo=»»
      while [ «$PartNo» = «» ]
      do
            echo «Please Enter Windows Partition No from Step 1: «;
            read PartNo;
      done
      if [ «$PartNo» = «1» ]; then
      img rl mnt/harddisk/newwinxpbase.zmg a1:p1
      if hwinfo —bios | grep -i «D510»; then img rl mnt/harddisk/Driversets/d510.zmg a1:p1
      elif hwinfo —bios | grep -i «D530»; then img rl mnt/harddisk/Driversets/d530.zmg a1:p1
      elif hwinfo —bios | grep -i «D5000»; then img rl mnt/harddisk/Driversets/d530.zmg a1:p1
      elif hwinfo —bios | grep -i «D5100»; then img rl mnt/harddisk/Driversets/d530.zmg a1:p1
      elif hwinfo —bios | grep -i «dc7100»; then img rl mnt/harddisk/Driversets/dc7100.zmg a1:p1
      elif hwinfo —bios | grep -i «dc7600»; then img rl mnt/harddisk/Driversets/dc7600.zmg a1:p1
      elif hwinfo —bios | grep -i «dc7700»; then img rl mnt/harddisk/Driversets/dc7700.zmg a1:p1
      elif hwinfo —bios | grep -i «dc7800 «; then img rl mnt/harddisk/Driversets/dc7800.zmg a1:p1
      elif hwinfo —bios | grep -i «Ultra»; then img rl mnt/harddisk/Driversets/dc7800usd.zmg a1:p1
      fi
      if hwinfo —cpu | grep -i «Units/processor: 2»; then img rl mnt/harddisk/Driversets/multiCPU.zmg a1:p1;
      fi
      elif [ «$PartNo» = «4» ]; then
      img rl mnt/harddisk/newwinxpbase.zmg a1:p4
      if hwinfo —bios | grep -i «D510»; then img rl mnt/harddisk/Driversets/d510.zmg a1:p4
      elif hwinfo —bios | grep -i «D530»; then img rl mnt/harddisk/Driversets/d530.zmg a1:p4
      elif hwinfo —bios | grep -i «D5000»; then img rl mnt/harddisk/Driversets/d530.zmg a1:p4
      elif hwinfo —bios | grep -i «D5100»; then img rl mnt/harddisk/Driversets/d530.zmg a1:p4
      elif hwinfo —bios | grep -i «dc7100»; then img rl mnt/harddisk/Driversets/dc7100.zmg a1:p4
      elif hwinfo —bios | grep -i «dc7600»; then img rl mnt/harddisk/Driversets/dc7600.zmg a1:p4
      elif hwinfo —bios | grep -i «dc7700»; then img rl mnt/harddisk/Driversets/dc7700.zmg a1:p4
      elif hwinfo —bios | grep -i «dc7800 «; then img rl mnt/harddisk/Driversets/dc7800.zmg a1:p4
      elif hwinfo —bios | grep -i «Ultra»; then img rl mnt/harddisk/Driversets/dc7800usd.zmg a1:p4
      fi
      if hwinfo —cpu | grep -i «Units/processor: 2»; then img rl mnt/harddisk/Driversets/multiCPU.zmg a1:p4;
      fi
      else
      echo «Invalid Partition Number, Please contact Harps or Bal: «;
      fi
elif [ «$Option» = «5» ]; then
      reboot -f
fi
done

Shell ScriptingScripting LanguagesLinux OS Dev

Понравилась статья? Поделить с друзьями:
  • Syntax error missing parentheses in call to print ошибка
  • Syntax error missing operator in query expression
  • Syntax error last token seen t garbled time
  • Syntax error json parse error
  • Syntax error invalid syntax перевод