takes an integer 32 as input and displays "thirty two" as output
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
takes an integer 32 as input and displays "thirty two" as output
create a bash script that takes an integer e.g. "32" as input and displays "thirty two" as output. the bash script should be simple, should not call any C program; but rather should make maximum use of built-in command line tools of linux e.g. sed, grep, awk etc. output should saved in a bash script file.
Sounds great! What have you done to solve this problem? (apart from ask someone here)
Personally, my first question to the person who set you this question would be what is the limit on the numbers to be converted? (ie. could you get 123451234512345 as a number?)
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
This seems like some real harsh homework but this just seems on a high level of being unreasonable. As Grail says, we'd need to know how large you need to let the number grow to... if you need to be able to handle thousands or even millions, then I'd use a recursive function myself... and use modulo and case statements to print the output. Not gunna give the answer, since as homework you still need to do it yourself but giving you a basis to start from as I can see why this would be confusing for simple homework.
Last edited by r3sistance; 02-20-2017 at 01:32 PM.
Whether or not this is homework or a work assignment or self study, the question is a poorly written one. Here is a copy of the LQ welcome information:
Hello and welcome to LQ. You were pointed here because a fellow LQ member feels the thread you started doesn't contain enough information to enable us to help you. We understand that Linux can be intimidating for new members, and we really do want to help. That said, please understand that LQ is not a help desk, customer service line for a product you purchased or willing to do your homework (although we are happy to assist you with specifics, if you show some effort of your own!). We're a 100% volunteer organization that wants to help you help yourself.
Here are a couple tips that will enable us to help you moving forward:
When asking a question, be sure to provide as many relevant details as possible. You should include the distribution and version you're using, along with hardware details, application version and exact error messages where applicable.
Using a descriptive title will vastly increase the number of members who see your thread, and therefore make a response significantly more likely.
If you're actively troubleshooting an issue you should also include any steps you've already taken.
You may want to include how what you are experiencing differs from what you expected to experience.
Before posting, have you used the search function to ensure your question hasn't been asked before?
You can edit your post by clicking the EDIT button in the bottom right corner of your message.
If you are unwilling or unable to ask questions in a manner that allows us to help you, it's unlikely our community will be able to provide you a solution. Unfortunately, serial offenders who show wanton disregard for this request after multiple pointers may be asked to seek help elsewhere. We truly hope that isn't necessary, and assure you Linux and Open Source are extremely rewarding and well worth the learning curve in the long run.
If it helps, I like to start simple and then make things more complicated as I go along.
So, imagine that the question was Create a bash script that takes an integer from 1 to 9 as input and displays "one" to "nine" as output.
Then move on to double digits (write down first what every digit in the "tens" represents e.g. 2 = "twenty" etc.).
Ignore at the start the more interesting numbers such as those with 1 in the "tens" and/or 0 in the "units". These are exceptions and can be added in once you've got the basic double digit version working.
Please read and consider rtmistler's posts #3 and #5 to become familiar with the proper use of the forum and guidance in asking well formed questions.
Quote:
Originally Posted by max-shah
create a bash script that takes an integer e.g. "32" as input and displays "thirty two" as output. the bash script should be simple, should not call any C program; but rather should make maximum use of built-in command line tools of linux e.g. sed, grep, awk etc. output should saved in a bash script file.
As others have said, you probably need to better define the task, particularly the range of numbers which must be handled.
For a small range something as simple as an array based lookup would suffice.
For larger ranges there are many ways to approach the problem, some already mentioned above.
The restrictions of simplicity and of using built-in command line tools is simulatneously vague and very broad. If you interpret built-ins as being shell built-ins, you have some very specific constraints and must specify which shell. If you interpret it as any tool installed (i.e. built-in) on some GNU/Linux distro, as seems to be the case, then you need to specify the distro, but almost anything goes under that definition!
As others have said, we are happy to help, but we need to see your own ideas first!
In bash, type assignment is fungible, the interpreter will attempt to interpret the string (since in bash, everything starts out as a string) and convert into an integer if you tell it to perform an arithmetic operation on it. If it can't, it fails. That has some interesting applications for what you're talking about. So your first level of taint checking could be performed by the shell, i.e. can the input be converted to an integer (no punctuation/no alphas). Once you've confirmed it can be cast as an integer, you could then do a "wc -m" to get the strings character count. The correlation between the word representing the digit and the digit itself is positional, i.e. 1 can mean one, teen (appended) or one-hundred based on where it's placed in the format of the string (last, second to last, third to last respectively).
EDIT: It would probably be easier to test the last two digits of the string to see if they can be converted to an integer > 10 && <20 since that ninr integer span doesn't follow the same language rules as the rest of the integers (it's it's eleven, not oneteen, twelve, not twoteen, thirteen, not threeteen) so that span will require special handling. Beyond fifteen, the language inverts the digits (it's not teensix, it's sixteen) and the pattern continues through to nineteen. From twenty to one hundred the language follows the form of the integers themselves i.e twenty one (21), sixty seven (67). This should considered in the context of what Dugan, Hydruga and Astrogeek have already suggested.
Hope that helps...just not too much
Last edited by dijetlo; 02-20-2017 at 03:45 PM.
Reason: Thought more about it
The specification explicitely mentions 'awk'. Now awk isn't just a filter, it is a programming language. So I suggest do this with an awk-program.
(PS don't tell your teacher that bash, grep, sed, awk etc are also C-programs, meaning they are written in C-language.)
There's definitely a process, and you can break down the problem and segment operations, such as validating that the input matches an actual number, dealing with minus sign, dealing with commas, and then dealing with the digits themselves, which many can suggest.
I try to bear in mind that max-shah created this question less than 24 hours ago and deserves a chance to update us with some feedback as to what their thought processes are.
Additional food for thought are my former links which I used to provide for BASH scripting as well as a pointer to a very old blog on the subject.
The two things occurring to me are that we advisers have not offered any helpful BASH links.
The other one is that last phrase in the problem description:
Quote:
output should (be) saved in a bash script file
WHY?!? A text file, a log file, sure. And sure, someone can save into a file with a .sh extension to make it a bash script file, but it won't be a "real" bash script file.
Code:
#!/bin/sh
# Sample output file
twenty four
That is not a valid BASH script file once you have the term "twenty four" with no variable assignment or other syntax. Really the only option here would be to make it a comment using '#', but why bother? Seems to be a poorly worded requirement.
yes you'd have to write an imminence "data base" to deal with digits or an elaborate condition statement to deal with every possibility trying to figure out how to match the numbers to know what the out put is to be.
striping down and separating every two digit number , three digit number, four digit number, on and on it goes then having to analyzing them to match the written names for each to be correct within the place holder format.
Putting restrictions on you to get this, I do not see any real word use for this script, done.
Quote:
the bash script should be simple, should not call any C program; but rather should make maximum use of built-in command line tools of linux e.g. sed, grep, awk etc. output should saved in a bash script file.
He or she or you limit your resources to get this done.
If this is a homework assignment that person is a sadist.
yes you'd have to write an imminence "data base" to deal with digits or an elaborate condition statement to deal with every possibility trying to figure out how to match the numbers to know what the out put is to be
Not really
Think of it like this. If we treat the string representing the user input as a string we can apply positional rules to it
example:
2 in position 1 counting right to left is "Two"
2 in position 2 counting right to left is "Twenty"
2 in position 3 counting right to left is "Two-hundred"
2 in position 4 counting right to left is "Two", it follows the same rules as position 1 however once positions 4-6 are aggregatteed, append "thousand"
2 in position 7 counting right to left is "Two", it follows the same rules as position 1 however once positions 7-9 are aggregated, append "million"
You aggregate them in groups of 3 characters, counting right to left, test for their position in the 3 character set and apply the appropriate "word" based on that position.
The, you append the suffix based on their 3 character sets position in the larger string and append the appropriate multiplyer (thousand, million, trillion... what comes after trillion....)
Special handling for the teens issue (ignore '0's)
That will get you to a trillion with 5 logical tests and 4 case statements
Finally, since $1 is a global variable, (you'd need one more as a "container" as you aggregated your response string) and shell functions can operate one globals, you could
probably reduce the entire code (sans functions) to something like...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.