LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-17-2018, 06:01 AM   #1
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 17.04
Posts: 315

Rep: Reputation: 25
Why does this elif not work?


From my Googling it looks like the syntax is valid but I consistently get:

Quote:
$ ./FunctionTest.sh func1
./FunctionTest.sh: line 6: syntax error near unexpected token `elif'
./FunctionTest.sh: line 6: `elif ["$1" == "func2"]'
Code:
#!/bin/bash

function func1()
{
	echo 'Function 1'
}

function func2()
{
	echo 'Function 2'
}

if ["$1" == "func1"]
then
func1()
elif ["$1" == "func2"]
then
func2()
else
echo 'dunno'
fi
 
Old 05-17-2018, 06:13 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,966

Rep: Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150Reputation: 2150
You can use https://www.shellcheck.net/ to find syntax errors as well as using set -xv for run time debugging.

The syntax error is due to there not being a space after [ and before ]
 
Old 05-17-2018, 06:25 AM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 3,069
Blog Entries: 3

Rep: Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327Reputation: 1327
Also the == should be a single =

See "man test"
 
Old 05-17-2018, 06:37 AM   #4
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.3 MATE
Posts: 4,706
Blog Entries: 2

Rep: Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538
I would suggest working through a Bash tutorial/guide such as:

http://tldp.org/LDP/Bash-Beginners-Guide/html/

You'll find it reaps great benefits.
 
Old 05-17-2018, 09:20 AM   #5
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 721

Rep: Reputation: 325Reputation: 325Reputation: 325Reputation: 325
You run a shell function like a command
func1
Not func1()
 
Old 05-17-2018, 12:45 PM   #6
bgstack15
Member
 
Registered: Jul 2017
Distribution: korora
Posts: 32

Rep: Reputation: Disabled
Also, I've observed that the shell really likes [ and ] characters to be separate from other non-white-space and non-separator characters. That is, put spaces around the square brackets.
 
Old 05-17-2018, 12:53 PM   #7
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.3 MATE
Posts: 4,706
Blog Entries: 2

Rep: Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538Reputation: 1538
@Mark_667: just an aside to say that if your profile information is correct and you are running Ubuntu 17.04, then you should think seriously about upgrading to 18.04 as 17.04 went end-of-life on 13 January 2018.
 
Old 05-17-2018, 01:21 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 6,620
Blog Entries: 12

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
An immediate problem I see here is that the complaint about line #6 is not representative of the script shown.

Use "set -xv" to validate the syntax and see debug output from the script.

Comments about the () after function names is valid, you don't call them using the parentheses.
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Elif Trouble azurepenguin Linux - General 4 11-17-2012 02:12 AM
if elif issue shellscript_03 Programming 7 11-14-2012 08:54 AM
scipt help elif bilbonix Linux - Newbie 6 11-03-2012 03:47 PM
[SOLVED] Elif Statement help UnixNewbie91 Linux - Newbie 6 04-28-2012 10:35 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration