Shell script to read a number and write the number in words
Posted in Decision Making
Script logic is as follows:
a) First read number into a shell variable
b) Next get one digit at a time
c) Use simple case control structure and loop to find digit equivalent in words
Sample script to convert numbers into equivalent words
#!/bin/bash # Shell script to read a number and write the number in words. Use case # control structure. For example 12 should be print as one two # ------------------------------------------------------------------------- # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- echo -n "Enter number : " read n len=$(echo $n | wc -c) len=$(( $len - 1 )) echo "Your number $n in words : " for (( i=1; i<=$len; i++ )) do # get one digit at a time digit=$(echo $n | cut -c $i) # use case control structure to find digit equivalent in words case $digit in 0) echo -n "zero " ;; 1) echo -n "one " ;; 2) echo -n "two " ;; 3) echo -n "three " ;; 4) echo -n "four " ;; 5) echo -n "five " ;; 6) echo -n "six " ;; 7) echo -n "seven " ;; 8) echo -n "eight " ;; 9) echo -n "nine " ;; esac done # just print new line echo ""
Download - Email this to a friend - Printable version
Related Other Helpful Shell Scripts:
- Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'
- Shell script to find whether an input number is palindrome or not
- Shell Ccript Convert Fahrenheit to Celsius Temperature ( Celsius to Fahrenheit Temperature )
- Shell program to calculate the number of digits in a number read from the user
- Shell program to read a number and reverse the number
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: case ... esac command, case control, control structure, convert number into words, find word length, for loop, shell loops, use case ~ Last updated on: April 5, 2008

