Solved

I Need Unix Shell Script to calculate Celsius to Fahrenhiet.

Posted on 2004-04-26
13
713 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
Comment Utility
homework question ?
0
 

Author Comment

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

Expert Comment

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

$cel is your temp in celcius
0
 
LVL 3

Assisted Solution

by:PerlKing
PerlKing earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rlburris
Comment Utility
SunnyCoder:

I receive a syntax error, '(' unexpected.

Also what does "scale=2" do?
0
 

Author Comment

by:rlburris
Comment Utility
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
Comment Utility
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
Comment Utility
avizit,
       Every time I run this I get a value of 32.00?
0
 

Author Comment

by:rlburris
Comment Utility
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
Comment Utility
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
Comment Utility
Avizit,
       I got it to work... Thanks for your help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
sameEnds challenge 3 105
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 81
Problem to open Excel file 15 37
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now