Solved

I Need Unix Shell Script to calculate Celsius to Fahrenhiet.

Posted on 2004-04-26
13
717 Views
Last Modified: 2013-12-26
(in case you don't know)
Formula = Celsius degrees * 1.8 + 32 = Fahrenhiet

For this script, lets say Celsius degrees = 27.12

Let me know if this is easier done in Perl.

Thanks!
0
Comment
Question by:rlburris
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10925793
homework question ?
0
 

Author Comment

by:rlburris
ID: 10925868
Nope... I support a Unix system that I need to monitor the system temperature.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10925980
echo $(echo "scale=2; $cel*1.8+32" | bc)

$cel is your temp in celcius
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:PerlKing
PerlKing earned 200 total points
ID: 10926757
If you would like to have a perl script to do this, create a file cel2far.pl and put the following code in it:

#!/usr/local/bin/perl

while(<>) {
   printf "%.2f\n", (($_ * 1.8) + 32);
}

Now run it as
./cel2far.pl

You can keep entering the value in Farenhiet and it would keep printing the values in Celsius
0
 
LVL 11

Expert Comment

by:avizit
ID: 10926881
youcan chop off two lines of the perl script

#!/usr/local/bin/perl -n
printf "%.2f\n", (($_ * 1.8) + 32);

/abhiijt/

0
 
LVL 3

Expert Comment

by:PerlKing
ID: 10927002
If you want your script to take the Celsius value as a command line argument and print it.

#!/usr/local/bin/perl
printf "%.2f\n", (($ARGV[0] * 1.8) + 32);

You can now invoke you script like this:
./cel2far.pl 100
212.00 (this is the output)
0
 

Author Comment

by:rlburris
ID: 10929867
SunnyCoder:

I receive a syntax error, '(' unexpected.

Also what does "scale=2" do?
0
 

Author Comment

by:rlburris
ID: 10929894
PerlKing,

I need to have the Celsius value as a input file and the output needs to be stored in a output file. Can you show me what the Perl script would look like?

Thanks!
0
 
LVL 11

Accepted Solution

by:
avizit earned 300 total points
ID: 10929947
You can run

#!/usr/local/bin/perl -n
printf "%.2f\n", (($_ * 1.8) + 32);


as

./script.pl  infile > outfile


/abhijit/  
0
 

Author Comment

by:rlburris
ID: 10930252
avizit,
       Every time I run this I get a value of 32.00?
0
 

Author Comment

by:rlburris
ID: 10930322
Working solution:

#!/opt/bin/perl
while(<>) {
   printf "%.2f\n", (($_ * 1.8) + 32);
}

Now run it as
./cel2far.pl input > output

Thanks for your help!

I will split points!
0
 
LVL 11

Expert Comment

by:avizit
ID: 10930420
It works fine for me .. i tested just now


following is one transcript
> cat infile
10
11
12
-40
-14
-50
-40

> cat test.pl
#!/usr/local/bin/perl -n
printf "%.2f\n", (($_ * 1.8) + 32);

> ./test.pl infile
50.00
51.80
53.60
-40.00
6.80
-58.00
-40.00


oh btw make sure you have perl located as /usr/local/bin/perl
if not do a "which perl" and change the first line accordingly


/abhijit/
0
 

Author Comment

by:rlburris
ID: 10930451
Avizit,
       I got it to work... Thanks for your help!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question