Link to home
Start Free TrialLog in
Avatar of rlburris
rlburris

asked on

I Need Unix Shell Script to calculate Celsius to Fahrenhiet.

(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!
Avatar of sunnycoder
sunnycoder
Flag of India image

homework question ?
Avatar of rlburris
rlburris

ASKER

Nope... I support a Unix system that I need to monitor the system temperature.
echo $(echo "scale=2; $cel*1.8+32" | bc)

$cel is your temp in celcius
SOLUTION
Avatar of PerlKing
PerlKing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
youcan chop off two lines of the perl script

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

/abhiijt/

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)
SunnyCoder:

I receive a syntax error, '(' unexpected.

Also what does "scale=2" do?
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!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
avizit,
       Every time I run this I get a value of 32.00?
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!
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/
Avizit,
       I got it to work... Thanks for your help!