LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-26-2002, 06:12 AM   #1
sleepwalker1.0
Member
 
Registered: Oct 2002
Distribution: mandrake 8.2
Posts: 41

Rep: Reputation: 15
Talking one more easy one ? anybody??


This is as simple as it gets but i get an error when i try to get the option 4 (the exit option) to exit the script. It says "line 28 unexpected token , fi" now I know that i have to close the if statement and have tried it both ways to make sure. However, i get the same error both ways. Do i need the "fi" statement at all here?

<code>


echo "Your current IPaddress is:
"/sbin/ifconfig ppp0|grep inet|cut -d : -f 2|cut -d " " -f 1"
echo

uptime
echo

echo "Whar do you want to do today?"
echo "1.Burn a cd"
echo "2.Erease a CD"
echo "3.Use instant messenger"
echo "4.Exit"
read A

if [ $A -eq 1 ]
[ cdwrite.sh ]
then
fi

read A
if [ $A -eq 2 ]
then
[ cde.sh ]
exit
fi

if [ $A -eq 3 ]
then
[ amsn ]
exit
fi

if [ $A -eq 4 ]
then
exit
fi
</code>

The script runs fine and does what i want it to do (so far) just a little something to do im my spare time. just wont exit. thanks for looking .
 
Old 10-26-2002, 09:24 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
There is more than one error in your script, like nested quotes, an extra "read A", and the ' [ ' and ' ] ' around the names of shell-scripts you want to execute. While there's nothing wrong with the exit-option in your menu.
I guess you copied it incorrectly to the forum-entry-box (?).

Anyways, I fixed it:

Code:
#!/bin/bash

echo -n "Your current IPaddress is: "
/sbin/ifconfig eth0 | grep inet|cut -d : -f 2|cut -d " " -f 1
echo

uptime
echo

echo "What do you want to do today?"
echo "1.Burn a cd"
echo "2.Erease a CD"
echo "3.Use instant messenger"
echo "4.Exit"
read A

if [ $A -eq 1 ]
then
    cdwrite.sh
fi

if [ $A -eq 2 ]
then
    cde.sh
exit
fi

if [ $A -eq 3 ]
then
    amsn
exit
fi

if [ $A -eq 4 ]
then
    exit
fi
Or better maybe:

Code:
#!/bin/bash

clear
echo -n "Your current IPaddress is: "
/sbin/ifconfig eth0 | grep inet|cut -d : -f 2|cut -d " " -f 1
echo

uptime

while true ; do
    echo
    echo "What do you want to do today?"
    echo "1.Burn a cd"
    echo "2.Erease a CD"
    echo "3.Use instant messenger"
    echo "4.Exit"
    read A

    case "$A" in
	"1") cdwrite.sh;;
	"2") cde.sh;;
	"3") amsn;;
	"4") exit 0;;
	*)   echo -e "\n==> Sorry, menu option not recognized.";;
    esac
done

# We should never get here...
exit 1
 
Old 10-26-2002, 10:19 AM   #3
sleepwalker1.0
Member
 
Registered: Oct 2002
Distribution: mandrake 8.2
Posts: 41

Original Poster
Rep: Reputation: 15
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: SuSE & Debian
Posts: 52

( post #2)
<quote>
There is more than one error in your script, like nested quotes, an extra "read A", and the ' [ ' and ' ] ' around the names of shell-scripts you want to execute. While there's nothing wrong with the exit-option in your menu.
I guess you copied it incorrectly to the forum-entry-box (?).
</quote>

sorry about the errors in the script, the one i use in linux is fine. I have no internet in linux (yet) and had to do a copy to mnt/windows/mydocuments and note pad did not split the code up right so i had to do the best i could with no "working" script in front of me. The script i have runs fine but yours is better code for sure. I need to work on that.
I can see the "exit 0" is better than the "exit" command i have.
I see also the "shorter" or "tighter" code looks better when reading.
The "case" statement is also cleaner.
The exit statement i had will exit the script but with errors. I got a unidentified token startment about the "fi" statement. dont understand i thought it would work myself. Oh well , thanks again.
Guess i just leave the "if and 'fi" alone and use more "case" when i can. lol
 
Old 10-26-2002, 10:47 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Guess i just leave the "if and 'fi" alone and use more "case" when i can. lol
Note: There's absolutely nothing wrong with "if". That's not what I mean. (sorry if it sounded that way). Use "if -- fi" whenever you need it.
It's just that when scanning multiple values for a variable, "case" is often cleaner. Good Luck.
 
  


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
easy connect not easy in suse greg collins Linux - Newbie 11 03-30-2006 07:31 PM
Easy urpmi ,no easy send mail. akihandyman Linux - General 2 12-27-2004 02:15 PM
Linux4RegularGuys - EASY FTP setup using vsftpd - READ THIS FOR EASY HELP DropHit Linux - Networking 4 11-25-2004 02:44 PM
Easy for most ... Please help.... kaz4u2dig Linux - Newbie 1 10-18-2004 09:46 PM
Here's an easy one... jhotchkiss Linux - Software 5 09-23-2003 11:53 AM

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

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