LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 08-02-2012, 04:10 AM   #1
theonislair
LQ Newbie
 
Registered: Oct 2011
Distribution: Fedora, CentOS, RHEL
Posts: 12

Rep: Reputation: Disabled
Bash scripting errors


I've been getting this weird syntax error no matter what I do, and I'm pretty stumped. I was wondering if you guys had any suggestions? I've tried a couple variant. I'll list their errors and code.

Here is the first variant:
Code:
#!/bin/bash

if /bin/grep -e $1 /etc/passwd > /dev/null
        HOMEDIR="$( /bin/grep -e ^$1 /etc/passwd | cut -d: -f6 )"
        CURSH="$( /bin/grep -e ^$1 /etc/passwd | cut -d: -f7 )"
        echo "$CURSH"
        echo "$HOMEDIR"
        exit;
else
        echo "No Such User Here."
        exit;
fi
exit;
And here is the error:
Code:
./script: line 9: syntax error near unexpected token `else'
./script: line 9: `else'
Here is the second variant:
Code:
if /bin/grep -e $1 /etc/passwd > /dev/null
        HOMEDIR="$( /bin/grep -e ^$1 /etc/passwd | cut -d: -f6 )"
        CURSH="$( /bin/grep -e ^$1 /etc/passwd | cut -d: -f7 )"
        echo "$CURSH"
        echo "$HOMEDIR"
        exit;
fi
And here is the error:
Code:
./script: line 8: syntax error near unexpected token `fi'
./script: line 8: `fi'
Here is the third variant:
Code:
#!/bin/bash

if /bin/grep -e $1 /etc/passwd > /dev/null
        HOMEDIR="$( /bin/grep -e ^$1 /etc/passwd | cut -d: -f6 )"
        CURSH="$( /bin/grep -e ^$1 /etc/passwd | cut -d: -f7 )"
        echo "$CURSH"
        echo "$HOMEDIR"
        exit;
else
        echo "No Such User Here."
        exit;
exit;
And here is the error:
Code:
./script: line 9: syntax error near unexpected token `else'
./script: line 9: `else'
Not sure what's going on here. I'm usually pretty good with bash (with a few minor gaps), but this has me stumped. Really stumped. I thought the first variant would work without a hitch. Is there something I'm missing that I'll probably catch tomorrow or when its mentioned? Probably something right under my nose, I don't know. Maybe someone can spot a flaw right off.
 
Old 08-02-2012, 04:16 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

you are forgetting the "then" after the if. Eg.
Code:
if condition
then
  statement
fi
In your last example you are also missing the "fi".

Evo2.
 
Old 08-02-2012, 04:19 AM   #3
roger_heslop
Member
 
Registered: Oct 2009
Location: Leander, TX
Distribution: Fedora 20
Posts: 97

Rep: Reputation: 35
This might do it:

Code:
#!/bin/bash
if /bin/grep -e $1 /etc/passwd > /dev/null; then 
 rest of script
fi
 
Old 08-02-2012, 04:21 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I couldn't find any then's in your program. What ever has happened to them?
 
Old 08-02-2012, 04:23 AM   #5
theonislair
LQ Newbie
 
Registered: Oct 2011
Distribution: Fedora, CentOS, RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
I told you guys, it was right under my nose and something that I'm missing due to lack of sleep. I do have another problem with a different script, but I'll make another post. Solved.
 
  


Reply



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
Command errors vs. bash errors a_d1234 Linux - Newbie 2 11-21-2011 03:53 PM
First steps into debian firewall scripting; a few errors still occur... Ypebe Linux - Security 10 05-29-2011 04:52 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash Reports Scripting Errors Where There Are None des_a Programming 3 03-28-2007 02:26 AM
bash scripting fnoyan Programming 1 01-18-2005 07:35 AM

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

All times are GMT -5. The time now is 08:49 AM.

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