4 Replies - 1449 Views - Last Post: 24 May 2010 - 03:47 AM Rate Topic: -----

#1 EvanSchoffstall  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 17
  • Joined: 23-May 10

Shell- Too many arguments

Posted 23 May 2010 - 02:21 PM

I've looked at other topics on various other sites but my issue won't clear up.
Here's my terminal

[Infomation] >> You must put all commands in lowercase!
[Infomation] >> 2 Players will be used for now.

[Main Menu] >> Newgame - Starts a new game.
[Main Menu] >> Load - Loads a saved game.

[Main Menu] << newgame

[Console] >> New game started.

[Console] >> Status - Displays the status of your nation.
[Console] >> Buy -[name] - Buys and Builds targeted unit/building.
[Console] >> End - Ends your turn.

[P1] << buy -house
Domination.sh: line 58: [: too many arguments
Domination.sh: line 82: [: too many arguments
Domination.sh: line 96: [: too many arguments



Here's my code. Its a bit complex. Spacing and Quotes checked out OK after skimming through my code. There must be something I'm missing.
#!/bin/sh
# World Domination The Game

#### Main Menu ####
mainMenu() {
# clears recent terminal clutter
clear
# no point in 1 player and rare to have 3 players
echo "[Infomation] >> You must put all commands in lowercase!"
echo "[Infomation] >> 2 Players will be used for now."
echo ""
echo "[Main Menu] >> Newgame - Starts a new game."
echo "[Main Menu] >> Load - Loads a saved game."
echo ""
printf "[Main Menu] << "
read Main_Menu_Option
if [ $Main_Menu_Option = "newgame" ]
then
	newgame
fi

if [ $Main_Menu_Option = "load" ]
then
	loadGame
fi
}

## New Game ##

newgame() {
Turn="P1"
P1_money="1000"
P2_money="1000"
P1_income="0"
P2_income="0"
P1_house_value="0"
P2_house_value="0"
P1_barracks_value="0"
P2_barracks_value="0"
P1_police="0"
P2_police="0"
P1_population="0"
P2_population="0"
echo ""
echo "[Console] >> New game started."
echo ""
echo "[Console] >> Status - Displays the status of your nation."
echo "[Console] >> Buy -[name] - Buys and Builds targeted unit/building."
echo "[Console] >> End - Ends your turn."
echo ""
funcOption
}

funcOption() {
printf "["$Turn"] << "
read Option
# if User imput = status
if [ $Option = "status" ]
then
	if [ $Turn = "P1" ]
	then
		echo ""
		echo "[Console] >> Money is "$P1_money
		echo "[Console] >> Income is "$P1_income
		echo "[Console] >> There are "$P1_house_value" houses"
		echo "[Console] >> There are "$P1_barracks_value" barracks"
		echo "[Console] >> There are "$P1_police" police units"
		echo ""
		funcOption
	else
		echo "[Console] >> Money is "$P2_money
		echo "[Console] >> Income is "$P2_income
		echo "[Console] >> There are "$P2_house_value" houses"
		echo "[Console] >> There are "$P2_barracks_value" barracks"
		echo "[Console] >> There are "$P2_police" police units"
		echo ""
		funcOption
	fi
fi

# if User imput = end
if [ $Option = "end" ]
then
	if [ $Turn = "P1" ]
	then
		Turn="P2"
		funcOption
	else
		Turn="P1"
		funcOption
	fi
fi


# if User imput = buy
if [ $Option = "buy -house" ]
then
	if [ $Turn = "P1" ]
	then
		P1_house_value='expr $P1_house_value + 1'
		P1_money='expr $P1_money - 300'
		P1_population='expr $P1_population + 5'
	else
		P2_house_value='expr $P2_house_value + 1'
		P2_money='expr $P2_money - 300'
		P2_population='expr $P2_population + 5'
	fi
fi

}

## Load Game ##

loadGame() {
echo "[Console] >> Incomplete"
}

mainMenu


Is This A Good Question/Topic? 0
  • +

Replies To: Shell- Too many arguments

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Shell- Too many arguments

Posted 23 May 2010 - 03:41 PM

Stick your string comparisons in quotes - it's always good practice anyway :)
if [ "$Option" = "end" ]
then
#... etc.



Also, you need to run funcOption at the end of the "buy" conditional block.
Was This Post Helpful? 1
  • +
  • -

#3 EvanSchoffstall  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 17
  • Joined: 23-May 10

Re: Shell- Too many arguments

Posted 23 May 2010 - 05:29 PM

Thanks :) +rep
However, Shell still won't do the math for me. It puts the expression as a string with the expr function in double quotes ( " ) and does the same with the single quotations ( ' )

Here's my Terminal.
[Infomation] >> You must put all commands in lowercase!
[Infomation] >> 2 Players will be used for now.

[Main Menu] >> Newgame - Starts a new game.
[Main Menu] >> Load - Loads a saved game.

[Main Menu] << newgame

[Console] >> New game started.

[Console] >> Status - Displays the status of your nation.
[Console] >> Buy -[name] - Buys and Builds targeted unit/building.
[Console] >> End - Ends your turn.

[P1] << buy -house
[P1] << status

[Console] >> Money is expr 1000 - 300
[Console] >> Income is 0
[Console] >> There are expr 0 + 1 houses
[Console] >> There are 0 barracks
[Console] >> There are 0 police units

[P1] << 



Heres the new code
#!/bin/sh
# World Domination The Game

#### Main Menu ####
mainMenu() {
# clears recent terminal clutter
clear
# no point in 1 player and rare to have 3 players
echo "[Infomation] >> You must put all commands in lowercase!"
echo "[Infomation] >> 2 Players will be used for now."
echo ""
echo "[Main Menu] >> Newgame - Starts a new game."
echo "[Main Menu] >> Load - Loads a saved game."
echo ""
printf "[Main Menu] << "
read Main_Menu_Option
if [ "$Main_Menu_Option" = "newgame" ]
then
	newgame
fi

if [ "$Main_Menu_Option" = "load" ]
then
	loadGame
fi
}

## New Game ##

newgame() {
Turn="P1"
P1_money="1000"
P2_money="1000"
P1_income="0"
P2_income="0"
P1_house_value="0"
P2_house_value="0"
P1_barracks_value="0"
P2_barracks_value="0"
P1_police="0"
P2_police="0"
P1_population="0"
P2_population="0"
echo ""
echo "[Console] >> New game started."
echo ""
echo "[Console] >> Status - Displays the status of your nation."
echo "[Console] >> Buy -[name] - Buys and Builds targeted unit/building."
echo "[Console] >> End - Ends your turn."
echo ""
funcOption
}

funcOption() {
printf "["$Turn"] << "
read Option
# if User imput = status
if [ "$Option" = "status" ]
then
	if [ "$Turn" = "P1" ]
	then
		echo ""
		echo "[Console] >> Money is "$P1_money
		echo "[Console] >> Income is "$P1_income
		echo "[Console] >> There are "$P1_house_value" houses"
		echo "[Console] >> There are "$P1_barracks_value" barracks"
		echo "[Console] >> There are "$P1_police" police units"
		echo ""
		funcOption
	else
		echo "[Console] >> Money is "$P2_money
		echo "[Console] >> Income is "$P2_income
		echo "[Console] >> There are "$P2_house_value" houses"
		echo "[Console] >> There are "$P2_barracks_value" barracks"
		echo "[Console] >> There are "$P2_police" police units"
		echo ""
		funcOption
	fi
fi

# if User imput = end
if [ "$Option" = "end" ]
then
	if [ "$Turn" = "P1" ]
	then
		Turn="P2"
		funcOption
	else
		Turn="P1"
		funcOption
	fi
fi


# if User imput = buy
if [ "$Option" = "buy -house" ]
then
	if [ "$Turn" = "P1" ]
	then
		P1_house_value="expr $P1_house_value + 1"
		P1_money="expr $P1_money - 300"
		P1_population="expr $P1_population + 5"
		funcOption
	else
		P2_house_value="expr $P2_house_value + 1"
		P2_money="expr $P2_money - 300"
		P2_population="expr $P2_population + 5"
		funcOption
	fi
fi

}

## Load Game ##

loadGame() {
echo "[Console] >> Incomplete"
}

mainMenu

Was This Post Helpful? 0
  • +
  • -

#4 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Shell- Too many arguments

Posted 24 May 2010 - 12:17 AM

Where you have:
    P1_house_value="expr $P1_house_value + 1"



I've never used expr myself, actually. I always do it like this:
    P1_house_value=$((P1_house_value + 1))



but having fiddled, it looks like you need $() or backticks:
    P1_house_value=`expr $P1_house_value + 1`


Was This Post Helpful? 1
  • +
  • -

#5 EvanSchoffstall  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 17
  • Joined: 23-May 10

Re: Shell- Too many arguments

Posted 24 May 2010 - 03:47 AM

Ohhhh +rep. I looked online about the expr function and I thought it used ( ' ) and not ( ` ). I really couldn't tell the difference. Anyways, thanks a million! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1