LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2021, 10:48 AM   #1
sick12301
LQ Newbie
 
Registered: Apr 2021
Posts: 2

Rep: Reputation: Disabled
Bash else syntax error?


Hello,
I am trying to make a password script in bash.
However else throws a syntax error??

Code:
#!/bin/bash
read -p "Password for user 'sick12301': " -n 1 -r
if [[ $REPLY =~ ^[meisthebest]$ ]]
else logout
then
        echo "\nwelcome"
fi
 
Old 04-13-2021, 10:53 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled
Wrong order: then must come first, else after that.

Last edited by shruggy; 04-13-2021 at 10:57 AM.
 
Old 04-13-2021, 11:54 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,616
Blog Entries: 11

Rep: Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655
Welcome to LQ and the Programming forum!

As said already, the else clause must follow the then clause, including its list of operations to carry out. From man bash:

Code:
if list; then list; [ elif list; then list; ] ... [ else list; ] fi
              The if list is executed.  If its exit status is zero, the then  list  is  executed.   Otherwise,
              each  elif list is executed in turn, and if its exit status is zero, the corresponding then list
              is executed and the command completes.  Otherwise, the else list is executed, if  present.   The
              exit  status  is  the  exit  status of the last command executed, or zero if no condition tested
              true.
 
Old 04-13-2021, 04:08 PM   #4
sick12301
LQ Newbie
 
Registered: Apr 2021
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Wrong order: then must come first, else after that.
Still not working


Code:
#!/bin/bash
read -p "Password for user 'sick12301': " -n 1 -r
if [[ $REPLY =~ ^[meisthebest]$ ]]
then
	echo "\nwelcome"
fi
else logout
EDIT: Nevermind I fixed I had to put else inside the if command.

Last edited by sick12301; 04-13-2021 at 04:20 PM.
 
Old 04-13-2021, 04:10 PM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,063

Rep: Reputation: Disabled
Still wrong. You have a fi before the else.
 
Old 04-13-2021, 09:08 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,818

Rep: Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083
Also, while a matter of preference, having logout on the same line as else is cluttered and confusing. It would be cleaner to have logout on its own line
 
Old 04-14-2021, 01:17 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
I can only suggest you to use shellcheck to analyze your script.
 
Old 05-03-2021, 08:39 AM   #8
tvc457
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Also, while a matter of preference, having logout on the same line as else is cluttered and confusing. It would be cleaner to have logout on its own line
True, I agree
But there's a difference between a syntax error, and being hard to read

I see a "then" statement which is also on its own line - finally found somebody who actually does that, instead of using "; then"
 
Old 05-03-2021, 10:01 AM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,923

Rep: Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899
I always use ; then, but in C I put my opening braces on a new line, BSD style. Which is odd now that I think about it.
 
Old 05-03-2021, 02:35 PM   #10
tvc457
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by GazL View Post
I always use ; then, but in C I put my opening braces on a new line, BSD style. Which is odd now that I think about it.
Hmm yes - The usage of the semicolon should be a hint: it's a new command. New command, new line.
 
Old 05-03-2021, 02:59 PM   #11
GazL
LQ Veteran
 
Registered: May 2008
Posts: 5,923

Rep: Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899Reputation: 3899
I suspect it stems from the fact that shell-scripts were commonly written on 24 line terminals and saving a line for each "if" helps with greater visibility of the code. It also mirrors K&R style C indentation, which is probably the one most folk use: though as I said, personally I prefer BSD style, or more specifically 'Allman'.

Last edited by GazL; 05-03-2021 at 03:01 PM.
 
  


Reply


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
LXer: Bash If Else Syntax With Examples LXer Syndicated Linux News 0 02-27-2020 09:12 PM
httpd: Syntax error on line 210 of /etc/httpd/conf/httpd.conf: Syntax error on line 6 iswarya Linux - Newbie 1 01-25-2012 01:28 PM
[SOLVED] bash: syntax error near unexpected token `else' lucmove Programming 9 05-10-2010 09:03 PM
[SOLVED] "Error: syntax before '@' token and Error: syntax at 'OTHER' token" bullrider Programming 2 07-27-2009 08:00 AM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:28 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
Open Source Consulting | Domain Registration