LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-17-2020, 07:56 AM   #1
Jensomon
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Rep: Reputation: Disabled
shell Ohm calculator


Hi all,
my son(12years) needs to find a calculator that uses Ohm formula, you now U=I*R.He has to look online for it but we have come up blank, most of them are in java.
This has to be a verry simple one,just give in I and U for example and get R and so on. On this they wil start building other scripts.Dont worry about giving a working script because that is what he needs.It is also a lesson in finding things online but with some supervision.
The beginning has to look something like this.

#!/bin/bash
clear
naam="Jens"
echo $naam
if [ u = i ]
then echo u

there could be some faults in there already but this is what we got on an other dutch speaking forum.
Thanks in advance
 
Old 10-17-2020, 06:51 PM   #2
ferrari
Senior Member
 
Registered: Sep 2003
Location: Auckland, NZ
Distribution: openSUSE Leap
Posts: 4,824

Rep: Reputation: 904Reputation: 904Reputation: 904Reputation: 904Reputation: 904Reputation: 904Reputation: 904Reputation: 904
These references may be of help to you...
https://www.shell-tips.com/bash/math...c-calculation/
http://faculty.salina.k-state.edu/ti...bash/math.html
 
Old 10-17-2020, 07:21 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,855

Rep: Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438Reputation: 4438
Do you already know about "bc -l"?
 
1 members found this post helpful.
Old 10-18-2020, 01:18 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,480
Blog Entries: 11

Rep: Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447Reputation: 3447
I would also recommend that you both become familiar with bc.

It should be available on most GNU/Linux distributions, begin by reading the man page (type man bc in a terminal), or by exercising that online search skill.

Using bc he can simply assign values to two of the variables then solve for the third by typing in the proper rearrangement of Ohm's law. For example (interactive mode):

Code:
$ bc -lq
v=12
i=.25
r=v/i
r
48.00000000000000000000
quit
He can also define math functions for each arrangement then get a result by passing the necessary variables to them.

I leave further explorations to the two of you.

Good luck!
 
1 members found this post helpful.
Old 10-18-2020, 02:27 AM   #5
Jensomon
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the reply all,
but making this ourselves was not the assigment.
We are both complete and utter noobs when it comes to this.
 
Old 10-26-2020, 01:55 AM   #6
Fjor
LQ Newbie
 
Registered: Oct 2004
Location: Jalisco, Mexico
Distribution: Slackware
Posts: 22

Rep: Reputation: 4
Let me give a simple introduction to programming. The proper structure/plan for your shell Ohm calculator could be:

Code:
shell identifier
comment about info and author
check if the user forgot to put values
get current and voltage from parameters
check if current is zero
calculate resistance
show result
And the corresponding code will be (let's use the filename ohmcalc.sh for the script):

Code:
#!/bin/bash
# use: ./ohmcalc.sh current voltage
# by Jensomon's Son

#-- if not exist second argument, then we do not have two arguments, error (! = not)
if ! [ "$2" ] ; then
   echo "Use: $0 current voltage to calculate resistance"
   echo "example: $0 120 5  (sorry, only integers)"
   exit
fi

#-- get value of arguments
I=$1
V=$2

#-- cannot divide by zero
if [ $I -eq 0 ] ; then
   echo "Sorry. The current cannot be zero."
   exit
fi

#-- calculate resistance from V=IR
R=$(( $V / $I ))

#-- show result
echo "The resistance without decimals is $R ohm."

#-- end --#
Here $0 give the name of the script and $1, $2, etc., are its arguments. The program will be executed this way:

Code:
chmod u+x ohmcalc.sh

./ohmcalc.sh
./ohmcalc.sh 100 7
./ohmcalc.sh 220 15
If you do not put at least two values, the script will show the correct form and stop.

Unfortunately, common shells only work with integers and cannot calculate decimals. To do that, they use external programs, like bc (as shown by @astrogeek). That's why I put messages about that to the user of the script. The first line of the script is called a 'shebang' and indicates which program will process the script instructions.

To solve the decimals part, you could substitute the calculation line with

U=$( echo "scale=2 ; $V / $I" | bc )

where scale=2 tell bc to show 2 digits after the decimal point and $V / $I is the desired calculation. The program bc has to be installed in the system for this to work.

A more interactive program could be:

Code:
shell identifier
comment about use info and author
show script title and function
repeat cicle (using while command)
  ask for current
  if current is 0, show message and exit the script
  ask for voltage
  calculate resistance using bc
  show result
continue cicle
Is important to express the plan of actions before coding them, because is easier to solve any problem if we know in advance the steps involved. This should include testing for error conditions and informing the user about that.

For more info about shell scripting, you can go to https://www.tutorialspoint.com/unix/shell_scripting.htm You can found others in the net.

Last edited by Fjor; 10-26-2020 at 01:57 AM.
 
2 members found this post helpful.
Old Yesterday, 11:53 AM   #7
Jensomon
LQ Newbie
 
Registered: Oct 2020
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Fjor, i have taken screenshots because this was also part of the assigment that he shows where he found it.
Again, thank you !
 
Old Yesterday, 04:57 PM   #8
Fjor
LQ Newbie
 
Registered: Oct 2004
Location: Jalisco, Mexico
Distribution: Slackware
Posts: 22

Rep: Reputation: 4
Happy to be useful. (Please mark the thread as SOLVED, if you think it is.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Calculator N+ is an open source scientific calculator for your smartphone LXer Syndicated Linux News 0 11-27-2019 06:21 AM
Shell Script Calculator gazagelen Linux - Newbie 7 05-05-2010 06:45 AM
Simple Shell Script Calculator chellemybelle Linux - Newbie 12 11-05-2009 11:44 PM
LXer: bc - The shell maestro’s calculator LXer Syndicated Linux News 0 04-06-2007 08:47 PM
shell calculator DropSig Linux - Newbie 3 11-01-2004 09:18 PM

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

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