Simple multiplication problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple multiplication problem
# 1  
Old 07-05-2007
Simple multiplication problem

I'am doing a tutorial where a simple calculator was given, then i noticed that you can't actually multiply
this is how i have approached the problem so far. i just need if the user enters "*"
to change it to "/*" ,is it possible? i know that * means the name of the last file in the directory or something similar so are they conflicting?


Code:
function calculator()
{
firstN=$1
secondN=$3
result=0
operator=$2
 while [ $# -eq 3 ]
 do
  if [ "$operator" = * ]
   then
   $operator=\*
   fi

  result=$(( $firstN $operator $secondN ))
  echo "$result"
  return $result
 done
 echo "Usage: $0 number operator number"
 exit 1
 return
}

calculator $1 $2 $3


Last edited by greekozz; 07-05-2007 at 10:55 AM..
# 2  
Old 07-05-2007
Try this:
Code:
function calculator
{
typeset -i result
firstN=$1
operator=$2
secondN=$3
if [ $# -ne 3 ]; then
 echo "Usage: $0 number operator number"
 exit 1
fi
result=${firstN}${operator}${secondN}
echo "$result"
}
set -f
calculator $1 $2 $3

# 3  
Old 07-05-2007
Thanks shell_life few questions though..

1) is there a there a bigger version from your avatar so that i could use it as a background Smilie

2) It works if the given argument is "\*" but i wanted to make sure that even if the user forgets that and simply enters "*" to be able to make the multiply
in this case the result is:
./calculator2.sh 5 * 2
./calculator2.sh: line 13: 52calculator2: value too great for base (error token is "52calculator2")

So is it possible?

3) You've used some command and some syntax that i would like to know more about:

a) typeset -i result (no idea what it does)
b) result=${firstN}${operator}${secondN}
why the curly brackets? and there is no (( )) or `` syntax ,what makes the
difference here?
c) set -f (same as a )


Thank you so much!

Last edited by greekozz; 07-05-2007 at 11:59 AM..
# 4  
Old 07-05-2007
Quote:
1) is there a there a bigger version from your avatar so that i could use it as a background
That I don't know -- if you 'google' for 'beaches', you may find something. It should not be difficult.

Quote:
2) It works if the given argument is "/*" but i wanted to make sure that even if the user forgets that and simply enters "*" to be able to make the multiply
in this case the result is:
./calculator2.sh 5 * 2
./calculator2.sh: line 13: 52calculator2: value too great for base (error token is "52calculator2")
So is it possible?
When you are in the unix command line and you enter a metacharacter,
the shell will interpret it and look for file names that matches the resulting
regular expression.
If you do not want it to happen, you have two solutions:
1) Escape the metacharacter.
2) 'set -f'
Quote:
3) You've used some command and some syntax that i would like to know more about:
a) typeset -i result (no idea what it does)
'typeset' declares shell variables.
'-i' declares an integer.
Quote:
b) result=${firstN}${operator}${secondN}
why the curly brackets? and there is no (( )) or `` syntax ,what makes the
difference here?
You (we) should always use the curly brackets when using the value of shell variables. Here is one example of why it is necessary:
Code:
set -- A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
echo "Total = "$#
echo "All = "$@
echo "1  = "$1
echo "11 = "$11
echo "11 = "${11}

Quote:
c) set -f (same as a )
Tells the shell to ignore filename metacharacters.

Try using the man page -- it is your friend:
Code:
man typeset
man set

# 5  
Old 07-05-2007
Great post, you are the man!! (literally and metaphorically Smilie )

Ok now i understand a little better you used typeset inside the function in order to make the variable global. (no need for return $variable)

and the brackets example couldn't be better.

The set -f doesn't seem to work in my situation.
maybe it's meant for another reason for using it inside the script before a grep command or a find command i am not sure.

But what i actually want is to escape the character "*" after it is inserted by the user.
I cant make it work no matter what, the system cannot ignore the asterisk and behave as if it was just another with no meaning character.
# 6  
Old 07-06-2007
If your variable contains special characters, like "*", then you will need to "double-quote" the variable whenever you use reference it, e.g. at the ksh prompt...
Code:
$ function fn_evaluate { perl -e 'print eval shift' "$*"; }
$ read query
6 * 7
$ result=$(fn_evaluate "$query")
$ echo $result
42

# 7  
Old 07-06-2007
thanks ygor i've already tried that and doesn't work either.

Here is the final version of the calculator and the debug outcome.

Code:
#!/bin/bash

function calculator()
{
firstN="$1"
secondN="$3"
result=0
operator="$2"

 while [ $# -eq 3 ]
 do
  if [ "$operator" = "*" ]
   then
   operator="\*"
   result=${$firstN} ${operator} ${secondN}
   return
   else
   result=$(( $firstN $operator $secondN ))
   return
  fi
 done

 echo "Usage: $0 number operator number"
 exit 1
 return
}

set -f

calculator "$1" "$2" "$3"
echo $result


sh -x calculator.sh 3 * 3
+ set -f
+ calculator 3 2 appointment
+ firstN=3
+ secondN=appointment
+ result=0
+ operator=2
+ '[' 3 -eq 3 ']'
+ '[' 2 = '*' ']'

As you see the arguments $2 and $3 are from the start wrong

I just want to pass the asterisk like any other character ,like the plus sign..

sh -x calculator.sh 3 + 3
+ set -f
+ calculator 3 + 3
+ firstN=3
+ secondN=3
+ result=0
+ operator=+
+ '[' 3 -eq 3 ']'
+ '[' + = '*' ']'
+ result=6
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

simple wc problem

Hi there, Im sure there is a simple explanation for this but I have a file like this with no balnk lines peter paul john I run the command # var=`grep paul file.txt` # echo $var paul # echo $var | wc -l 1 but when I grep for a value that isnt in the file, i still... (4 Replies)
Discussion started by: rethink
4 Replies

2. Shell Programming and Scripting

another simple awk problem

Hello; I need to print two previous lines after searching for a reg exp: awk '/haywood/' should produce the following =================== p9J46THe020804 89922 Tue Oct 18 21:06 MAILER-DAEMON (host map: lookup (haywood.com): deferred) ... (1 Reply)
Discussion started by: delphys
1 Replies

3. Shell Programming and Scripting

simple awk problem

Hello; I have the following log file: 10/11/11 10:42:02 LOCK Q Userid:284 Username=root UserPID:23158 Device:marlin batch 10/11/11 10:42:02 TableNr:226 TableName:iatkn RecId:116290398 Flags:X Q H 10/11/11 10:42:02 LOCK CONTENTION X 10/11/11 10:42:02 ... (3 Replies)
Discussion started by: delphys
3 Replies

4. Shell Programming and Scripting

Please help me with a simple problem

Hi, I have a very simple script like below: for n in 10 20 30 do for a in 30 40 50 60 70 80 do for r in 2 3 4 5 6 7 do m=$((r*a)) count=1 while do echo "a = " $a ", m = " $m ", n = " $n ... (2 Replies)
Discussion started by: Dark2Bright
2 Replies

5. Programming

Simple g++ compilation problem

I'm trying to port some code over to unix Solaris. I'm not really a unix programmer so I'm sure this is something straight-forwards but I'm getting the following: > g++ -c CTrade.cpp In file included from CTrade.cpp:1: stdafx.h:6: warning: `#pragma once' is obsolete CTrade.cpp:4: iostream: No... (1 Reply)
Discussion started by: achartley
1 Replies

6. Shell Programming and Scripting

simple jsp problem.

I have my jsp page located at /install/apache-tomcat-5.5.29/webapps/jsp-examples/light/login.jsp There fore i will have to use following url to access it. localhost:8080/jsp-examples/light/login.jsp How would i make this little shorter like. localhost:8080/login.jsp I m using... (13 Replies)
Discussion started by: pinga123
13 Replies

7. Shell Programming and Scripting

simple awk problem

pcn linus> ntpq -p remote refid st t when poll reach delay offset disp ============================================================================== +smpnn01 ntpsrv1 2 u 829 1024 377 1.46 0.793 0.85 *smpnn02 ntpsrv1 2 u ... (2 Replies)
Discussion started by: arch12
2 Replies

8. Shell Programming and Scripting

Simple regex problem?

Hi all, I am looking to create words from a sentence which adhere to a custom search pattern from my website: Example: ! +! / += ~ where the terms ! = not, +! = AND NOT, += - and equals and ~ = can be like.... Now here is the issue...i want to split a sentence like the one above on... (1 Reply)
Discussion started by: muay_tb
1 Replies

9. Shell Programming and Scripting

Simple Scripting Problem

Hi there, I was trying to add a line of text in the middle line of a file. I have counted the lines in the file, and then I divide it into 2, after that I am stuck on how am I suppose to append the line on that file? When I tried to use this command 'second line >> filename' it appends it at... (3 Replies)
Discussion started by: felixwhoals
3 Replies

10. UNIX for Advanced & Expert Users

A simple Samba problem, please help

Hi, I have successfully setup my Samba server on my Sun Solaris 8 Sparc machine. My Win2000 workstation is able to see the unix worstation and its shared directory from Network Neighborhood. However, the Unix workstation appears as Ip address instead of Hostname on my network neighborhood... (5 Replies)
Discussion started by: champion
5 Replies
Login or Register to Ask a Question