194k views
5 votes
Write a Linux script that accepts a decimal number as input and converts it in its binary, octal and hexadecimal formats as well as displays them on the screen.

1 Answer

3 votes

Final answer:

To convert a decimal number to binary, octal and hexadecimal formats in a Linux script, you can use the 'bc' command.

Step-by-step explanation:

To write a Linux script that accepts a decimal number as input and converts it into its binary, octal, and hexadecimal formats, you can use the 'bc' command, which is a calculator language in Linux. Here is an example of a script that accomplishes this:

#!/bin/bash

read -p 'Enter a decimal number: ' decimal

binary=$(echo 'obase=2; ibase=10; '$decimal | bc)
octal=$(echo 'obase=8; ibase=10; '$decimal | bc)
hex=$(echo 'obase=16; ibase=10; '$decimal | bc)

echo 'Binary: ' $binary
echo 'Octal: ' $octal
echo 'Hexadecimal: ' $hex

User Howl
by
7.8k points