How to use square values in Shell Scripting?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use square values in Shell Scripting?
# 1  
Old 11-28-2014
How to use square values in Shell Scripting?

Smilie

Hi

I am a newbie with Shell Scripting who stuck while creating a shell script for Pythagoras theorem.I need to know how to add the squares for the value in shell scripting(for eg: b2 =a2 +c2).

Thanks
VR

Last edited by VoraciousReader; 11-28-2014 at 03:11 AM..
# 2  
Old 11-28-2014
Code:
a=2
b=3
echo $(( (a*a) + (b*b) ))

# 3  
Old 11-28-2014
@balajesuri
thanks for the information but what would be the command for sh scripting ?
Regards
VR
# 4  
Old 11-28-2014
Bash doesn't do square roots though.You can pipe your formula to a more powerful math app -- bc
Code:
a=4
b=12
echo "scale=5; sqrt($a^2 + $b^2)" | bc  # print to screen
or
c=$(echo "scale=5; sqrt($a^2 + $b^2)" | bc)  # save in variable c
echo $c

scale is how many decimal places.

Last edited by ongoto; 11-28-2014 at 06:06 AM.. Reason: more options
# 5  
Old 11-28-2014
If you have a 1993 or later version of the Korn shell, save the following in a file named pythagorus:
Code:
#!/bin/ksh
b=$1
c=$2
printf 'b=%f\n' "$b"
printf 'b**2=%f\n' $((b**2))
printf 'c=%f\n' "$c"
printf 'c**2=%f\n' $((c**2))
printf 'b**2 + c**2=%f\n' $((b**2 + c**2))
printf '(b**2 + c**2)**.5=%f\n' $(((b**2 + c**2)**.5))
printf 'sqrt(b**2 + c**2)=%f\n' $((sqrt(b**2 + c**2)))

and make it executable with:
Code:
chmod +x pythagorus

and invoke it with two operands such as one of the following:
Code:
./pythagorus 2 3
./pythagorus .05 .12
./pythagorus 50 120

you'll get output like:
Code:
b=2.000000
b**2=4.000000
c=3.000000
c**2=9.000000
b**2 + c**2=13.000000
(b**2 + c**2)**.5=3.605551
sqrt(b**2 + c**2)=3.605551

Code:
b=0.050000
b**2=0.002500
c=0.120000
c**2=0.014400
b**2 + c**2=0.016900
(b**2 + c**2)**.5=0.130000
sqrt(b**2 + c**2)=0.130000

and
Code:
b=50.000000
b**2=2500.000000
c=120.000000
c**2=14400.000000
b**2 + c**2=16900.000000
(b**2 + c**2)**.5=130.000000
sqrt(b**2 + c**2)=130.000000

respectively.

If you don't have a recent ksh, you can use the following script using dc (which is the back end to the bc utility on many systems) with any shell that accepts basic Bourne shell syntax (such as bash, dash, ksh, or (on almost any system) /bin/sh):
Code:
#!/bin/ksh
b=$1
c=$2
printf '%s\n' "6k $b sb $c sclb[b=]np2^[b**2=]nplc[c=]np2^[c**2=]np+
[b**2 + c**2=]npv[sqrt(b**2 + c**2)=]npq" | dc

and with the same three commands, get the output:
Code:
b=2
b**2=4
c=3
c**2=9
b**2 + c**2=13
sqrt(b**2 + c**2)=3.605551

Code:
b=.05
b**2=.0025
c=.12
c**2=.0144
b**2 + c**2=.0169
sqrt(b**2 + c**2)=.130000

and
Code:
b=50
b**2=2500
c=120
c**2=14400
b**2 + c**2=16900
sqrt(b**2 + c**2)=130.000000


Last edited by Don Cragun; 11-28-2014 at 07:36 AM.. Reason: Remove extraneous printf call from my internal tests.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to calculate avg values of csv file using shell scripting .?

hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe . eg data are :... (2 Replies)
Discussion started by: Avinash shaw
2 Replies

2. Shell Programming and Scripting

Regex in Shell Scripting to pick values

Hi After lot of trial and error I am really bowled out with the requirement in hand and honestly you are my last hope Here is what I want to achieve Values *IF *VALUE MS_SQL_Statistics_Summary.Client_Count_Percent_Used *GT 70.00 *AND *VALUE... (20 Replies)
Discussion started by: radioactive9
20 Replies

3. UNIX for Dummies Questions & Answers

How to compare to values returned from sql in shell scripting?

hey i am using this code to connect to sql , store the value in variable and then compare it with another variable after some time by executing the same query but the desired result is not coming #!/bin/bash val=$(sqlplus -s rte/rted2@rel76d2 <<ENDOFSQL set heading off set feedback off... (11 Replies)
Discussion started by: ramsavi
11 Replies

4. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

5. Shell Programming and Scripting

set variable with square brackets in c shell!

Hi Guys, I want to set a variable which happend to have square brackets in them. I tried multiple ways that is by escaping it with quotes (both single and double ) but it didn't help. All I want is: set x = slicearray.slice\.post My failed attempts were: set x = "slicearray.slice\.post"... (3 Replies)
Discussion started by: dixits
3 Replies

6. Shell Programming and Scripting

mapping of values in shell scripting

sample content of file1: SSTY1 2145228348 652011011715140100000002419005432092074 008801726143662 VDZX01 MIO2 008801726143662 SSRTY 2145228349 ... (3 Replies)
Discussion started by: vsachan
3 Replies

7. Shell Programming and Scripting

Need help to change XML values with shell scripting for Network Simulation

Hello, I don't have experience in this scripting and I need some help to read a value from an XML file and change it with a random number to use in simulator for different network scenarios. </Description><sim_comm_rounds>35</sim_comm_rounds><num_clusters>1</num_clusters><Clocking> I want to... (5 Replies)
Discussion started by: erhanasd
5 Replies

8. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

9. UNIX for Dummies Questions & Answers

Concatenating arrays cell values in shell scripting

Hi All, I want to concatenate the array cell values and form a string.. Is it possible? for ex. I have an array word_array contains d u m b and after concatenating the string shld be 'dumb' thanks (2 Replies)
Discussion started by: mathur
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question