Need help with a program

Want to just shoot the breeze? Forum 42 is the place!

Moderator: Moderators

Post Reply
Sparkfist
Forum Administrator
Posts: 6754
Joined: Tue Apr 20, 2004 7:12 am
Location: Michigan

Need help with a program

Post by Sparkfist »

I have to have this ready tomarrow, I know I waited til the last minute, smart me! Anyways I'd like to know if someone who knows bash shell scripting can help me. I keep gettting a syntax error on line 19.

Code: Select all

set -xv
#! /bin/bash

function Cave ()
{
        read -p "Yell something: " yell
        echo $yell
        echo $yell
        echo $yell
}

function Calculator ()
{
        read -p "Enter number: " var1
        read -p "Enter opreator: " var2
        read -p "Enter number: " var3
        case $var2 in
                +) echo "var1 + var3" | bc -l
                -) echo "var1 - var3" | bc -l
                *) echo "var1 * var3" | bc -l
                /) echo "var1 / var3" | bc -l
esac
}

function Quit ()
{
        exit
}

clear
echo "1)Enter Cave
2)Calculator
3)Quit"

        read -p "Enter number: " opt

        case $opt in
        1) function Cave
        ;;
        2) function Calculator
        ;;
        3) function Quit
        ;;
        *) echo "Invalide please enter 1, 2, or 3"
        ;;
esac

set +xv
For anyone who's going to run it I did leave the debugging "set -xv" command in there. And thanks for any help.
vskid wrote:Nerd = likes school, does all their homework, dies if they don't get 100% on every assignment
Geek = likes technology, dies if the power goes out and his UPS dies too

I am a geek.
xelion
Posts: 185
Joined: Sat Jan 21, 2006 11:04 am
Location: My chair
Contact:

Post by xelion »

love me :)

Code: Select all

#! /bin/bash

function Cave()
{
        read -p "Yell something: " yell
        echo $yell
        echo $yell
        echo $yell
}

function Calculator()
{
        read -p "Enter number: " var1
        read -p "Enter opreator: " var2
        read -p "Enter number: " var3
        case "$var2" in
                 +) echo "var1 + var3" | bc -l;;  #you forgot to end the statements here with the ;;
                 -) echo "var1 - var3" | bc -l;;
                [*]) echo "var1 * var3" | bc -l;; #needs [] else anything would work
                 /) echo "var1 / var3" | bc -l;;
	esac
}

function Quit()
{
        exit
}

clear
echo "1)Enter Cave
2)Calculator
3)Quit"

        read -p "Enter number: " opt

        case "$opt" in
        1) Cave;;        #don't need to say function before the name of the function
        2) Calculator;;
        3) Quit;;
        *) echo "Invalide please enter 1, 2, or 3";;

	esac
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Just about to say, you need to throw the brackets around the * symbol.
xelion
Posts: 185
Joined: Sat Jan 21, 2006 11:04 am
Location: My chair
Contact:

Post by xelion »

looking back at your code, it sucks, I'm gonna rewrite part of it. Please hold.
gannon
Moderator
Posts: 6974
Joined: Sun Apr 04, 2004 4:48 pm
Location: Near that one big lake
Contact:

Post by gannon »

yep, I was going to talk about escaping as well :P
Edit:
lol
mike # bash test.sh
1)Enter Cave
2)Calculator
3)Quit
Enter number: 2
Enter number: 2
Enter opreator: +
Enter number: 2
0

Code: Select all

#! /bin/bash

function Cave () {
    read -p "Yell something: " yell
    echo $yell
    echo $yell
    echo $yell
}

function Calculator () {
    read -p "Enter number: " var1
    read -p "Enter operator: " var2
    read -p "Enter number: " var3
    case $var2 in
        +) echo "$var1 + $var3" | bc -l;;
        -) echo "$var1 - $var3" | bc -l;;
        [*]) echo "$var1 * $var3" | bc -l;;
        /) echo "$var1 / $var3" | bc -l;;
esac
}

function Quit () {
    exit
}

clear
echo "1)Enter Cave
2)Calculator
3)Quit"

    read -p "Enter number: " opt

    case $opt in
    1) Cave;;
    2) Calculator;;
    3) Quit;;
    *) echo "Invalide please enter 1, 2, or 3" ;;
esac
xelion
Posts: 185
Joined: Sat Jan 21, 2006 11:04 am
Location: My chair
Contact:

Post by xelion »

Ok, revamped your code a bit

Code: Select all

#! /bin/bash

function Cave()
{
        read -p "Yell something: " yell
        echo $yell
        echo $yell
        echo $yell

	Askexit
}

function Calculator()
{
        read -p "Enter number: " var1
        read -p "Enter opreator: " var2
        read -p "Enter number: " var3
        case "$var2" in
                 +)echo $[$var1+$var3] | bc -l
		;;
                 -)echo $[$var1-$var3] | bc -l
		;;
                [*])echo $[$var1*$var3] | bc -l
		;;
                 /)echo $[$var1/$var3] | bc -l
		;;
	esac

	Askexit


}

function Quit()
{
        exit
}

function Askexit()
{
	read -p "Go back to menu? [y/n]" ans
	case "$ans" in
	"y" | "Y")main;;
	"n" | "N")Quit;;
	*)echo "Enter y/n"
	Askexit;;
	esac
}

function main()
{
clear
echo "1)Enter Cave
2)Calculator
3)Quit"

        read -p "Enter number: " opt

        case "$opt" in
        1) Cave;;
        2) Calculator;;
        3) Quit;;
        *) echo "Invalid please enter 1, 2, or 3"
	main
	;;

	esac
}

	main
As you can see I made your starting thing a function as well, as so to be able to go back to the menu after doing an option. Though I'm new at bash(been programming bash for about 30 minutes now :P) so I dunno how to get it to wait so you can see the answer to your calculator(and responce to the yell function) and then go back to main after a keypress or such. So yeah. There you go

EDIT: Nvm I own, solved it with another function, Askexit.
Sparkfist
Forum Administrator
Posts: 6754
Joined: Tue Apr 20, 2004 7:12 am
Location: Michigan

Post by Sparkfist »

Wow I'm impressed! It only had one typo that was the "n" at the begining right infront of the Cave funtion, but after that it's perfect. Thank you! You have no idea how much this means to me.
vskid wrote:Nerd = likes school, does all their homework, dies if they don't get 100% on every assignment
Geek = likes technology, dies if the power goes out and his UPS dies too

I am a geek.
Post Reply