LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2012, 11:30 PM   #1
akifennec
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Rep: Reputation: Disabled
Shell scripting help- unexpected fi


Im writing a script for a shell scripting class and I'm running into a unexpected fi problem and could use some help. heres the script

Code:
#!/bin/bash
set v = menu
clear
read -p "Please Enter your name " name
read -p "Enter your birth year.  Example: 1900 : "year
yearnow=$(date '+%Y')
agey=$(($yearnow-$year))
if [ $agey -lt 18 ] ; then
          echo "Sorry $name , you must be at least 18."
else

while : do
clear
        cat menu
        read -p "pick from above " choice
        if [ $choice -eq x ] ; then
                break
else
           set z = 1 # the counter

case "$choice" in
L)
clear
                set z = $z + 1
echo "Select field to cut form 'last' add comma to add multiple fields (x to quit)"
read value1
if [[ $value1 == x ]]; then exit;fi
echo "Select first row"
read value2
echo "Select last row "
read value3
last | tr -s ' ' ' ' | cut -d' '  -f"$value1" | tail -n +$value2 | head -n $value3
;;

U)
clear
set $z = $z + 1
who
read -p "Heres who's online line who do you want more info on? " name1
finger $name1
;;


esac
fi
fi
echo "$name"
I know there might be other things wrong but Id only like help with the if problem please

Last edited by akifennec; 11-12-2012 at 08:42 AM. Reason: solved
 
Old 11-11-2012, 11:49 PM   #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,

welcome to LQ. When posting questions, please post the *exact* error message. Also, it would be *much* easier to read your code it you indented it properly. In fact, I suspect that if your code was indented correctly, you might be able to see the problem yourself.

Evo2.
 
Old 11-12-2012, 12:02 AM   #3
akifennec
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
line 51: syntax error near unexpected token `fi'

the error I'm getting is line 51: syntax error near unexpected token `fi'

code with better formatting

Code:
else
#!/bin/bash
set v = menu
clear

read -p "Please Enter your name " name
read -p "Enter your birth year.  Example: 1900 : " year
yearnow=$(date '+%Y')
agey=$(($yearnow-$year))

if [ $agey -lt 18 ] ; then
        echo "Sorry $name , you must be at least 18."
else

while : do
        clear
        cat menu
        read -p "pick from above " choice
                if [ $choice -eq x ] ; then
                        break
                else
                        set z = 1 # the counter

                        case "$choice" in

                                L)
                                        clear
                                        set z = $z + 1
                                        echo "Select field to cut form 'last' add comma to add multiple fields (x to quit)"
                                        read value1
                                                if [[ $value1 == x ]]; then
                                                        exit;
                                                fi
                                        echo "Select first row"
                                        read value2
                                        echo "Select last row "
                                        read value3
                                        last | tr -s ' ' ' ' | cut -d' '  -f"$value1" | tail -n +$value2 | head -n $value3
                                        ;;

                                U)
                                        clear
                                        set $z = $z + 1
                                        who
                                        read -p "Heres who's online line who do you want more info on? " name1
                                        finger $name1
                                        ;;


                        esac
                fi
fi

echo "$name"
I don't see any unaccounted for if or fi
 
Old 11-12-2012, 12:24 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The while : do is not terminated (rigorous indentation would have identified that).

if [ $choice -eq x ] will not work because -eq is an arithmetic comparison operator and x is not an integer.

bash variables are not set by the likes of set v = menu
Code:
c@CW9:~$ set v = menu
c@CW9:~$ echo $v

c@CW9:~$ echo $1 $2 $3
v = menu
c@CW9:~$ v=menu
c@CW9:~$ echo $v
menu
Debugging is easier without the interactive prompts and with the clear commands commented out
Code:
#read -p "Please Enter your name " name
name=foo
#read -p "Enter your birth year.  Example: 1900 : " year
year=1990
...
    #clear
 
Old 11-12-2012, 01:40 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I also suspect this is not what you meant
Code:
else
#!/bin/bash
to help debug, try adding 'set -xv' thus
Code:
#!/bin/bash
set -xv
NB: the shebang line ( #!/bin/bash ) MUST be the first line in a shell script.

Handy links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 11-12-2012, 02:47 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Also 'while : do' should be 'while :; do'.

I also suggest variables to be closed from IFS parsing. e.g. command "$abc" ...

Last edited by konsolebox; 11-12-2012 at 02:48 AM.
 
Old 11-12-2012, 08:41 AM   #7
akifennec
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
don't know how the else got there its not in my code. but thank you i missed indenting the while statement and see that I was miss "done"

Thank you all for the help
 
  


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
LogCleanup.sh Shell scripting problem: unexpected "fi" Any Help Pls wireshark11 Linux - Newbie 6 11-10-2012 08:45 AM
Bash scripting error (unexpected end of line) Fraeco Linux - Newbie 5 01-06-2012 07:52 PM
[SOLVED] Shell Scripting "syntax error : unexpected end of file" roxie600 Programming 12 04-30-2010 12:18 AM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:11 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