• Arun Kumar

Shell Script to Convert Temperature

Linux shell script allows the user to enter the temperature and provides the option to convert in Fahrenheit or Celsius. To achieve this we will use for loop. A ‘for loop’ is a bash programming language statement that allows code to be repeatedly executed.


vi temperature_convert.sh
#!/bin/bash

echo "*** Converting between the different temperature scales ***"
echo "1. Convert Celsius temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperatures into Celsius"
echo -n "Select your choice (1-2) : "
read choice
 
if [ $choice -eq 1 ]
then
	echo -n "Enter temperature (C) : "
	read tc
	# formula Tf=(9/5)*Tc+32
	tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
	echo "$tc C = $tf F"
elif [ $choice -eq 2 ]
then
	echo -n "Enter temperature (F) : "
	read tf
	# formula Tc=(5/9)*(Tf-32) 
	tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
	echo "$tf = $tc"
else
	echo "Please select 1 or 2 only"
	exit 1
fi 

Here is the sample output of the above script


More ways to use for loop

  • You can use 'for' like the C programming language.

  • You can use the Range of numbers after the “in” keyword.

3 views

Recent Posts

See All

Basic Linux Commands

This article describes basic Linux commands that you must know as a system or database administrator. Below commands in Linux help you manage any Linux server fast and smoothly. Linux Command List Che

Linux Firewall with iptables and firewalld

In this article will be covering details regarding iptables and firewalld which helps in Linux firewall management. We will also be looking at how to enable specific ports (1521 for Oracle) inside ipt

Passwordless SSH in Linux

Every time you ssh from one Linux server to another, you need to enter target server user's password. We can also setup passwordless ssh in Linux between two Linux servers so that you don't have to en

  • Facebook
  • Twitter
  • YouTube
  • Instagram

© 2020 by DBA Genesis