shell script to convert octal number to decimal numbers

Posted: March 24, 2015 in LINUX
Tags: , ,

Here in this article i am going to mentions a  small script to convert octal number to decimal numbers. We are going to test this script in linux.I have tested it on RedHat Linux.

 

In this article I we will use (()) and bc commands to do my task.

You may be excited to know that there are several tips and tricks to do that and here i am going to mention all those.

 

Trick1: Use (()) brace expatiation. 

Here I am going to create a file octa.sh using vim editor. and write the below code.

#!/bin/bash

read -p “enter OCT number: ” OCT1

echo “The decimal value of $HEX1 is $((8#$HEX1))”

 

 

Now Change permissions of script:

 

chmod +x  octa.sh

 

Now  run the shell script

#./octa.sh

 

 

Trick2: Using bc command

 

#!/bin/bash

read -p ” enter OCT number: ” OCT1

 

echo “ibase=8; $OCT1” | bc

 

 

Save above file as octatodec.sh

 

Change permissions to this script now and execute as follows

 

chmod +x octatodec.sh

Executing shell script

 

./oct2dec.sh

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s