LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2017, 11:50 AM   #1
max-shah
LQ Newbie
 
Registered: Feb 2017
Posts: 1

Rep: Reputation: Disabled
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.

how to do this.
please help me.

Last edited by max-shah; 02-20-2017 at 11:52 AM.
 
Old 02-20-2017, 01:08 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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?)
 
Old 02-20-2017, 01:20 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 02-20-2017, 01:31 PM   #4
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
Old 02-20-2017, 01:34 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.

Some additional reading that may help:
LQ Search
LQ Sitemap
LQ Site FAQ
How to ask technical questions to get quality answers
 
Old 02-20-2017, 01:41 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Well, I can give you the most naive implementation...

Code:
case "$1" in
"1")
echo "one"
;;
"2")
echo "two"
;;
# snip
"32")
echo "thirty two"
;;
# and so on
esac
Never confuse "most naive" with "worst". But for a more elegant implementation: can we assume that the input range is 1 to 99?

Last edited by dugan; 02-20-2017 at 07:58 PM.
 
Old 02-20-2017, 01:42 PM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
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.

Then, and only then, move on to three digits.
 
Old 02-20-2017, 01:48 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-20-2017, 02:47 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
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 View Post
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!

And welcome to LQ and the LQ Programming forum!
 
Old 02-20-2017, 03:07 PM   #10
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
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
 
Old 02-20-2017, 10:28 PM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.)

Last edited by NevemTeve; 02-20-2017 at 10:32 PM.
 
Old 02-21-2017, 06:14 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.

Here are the links:

Bash Guide for Beginners
Advanced Bash Scripting Guide
Bash Scripting for Dummies and Geniuses
 
Old 02-21-2017, 08:04 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 02-21-2017 at 08:09 AM.
 
Old 02-21-2017, 09:18 AM   #14
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
Quote:
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...
Quote:
echo "#!/bin/bash \n echo " $(pull_me) >> i_am_a_bash_script.sh

Last edited by dijetlo; 02-21-2017 at 10:32 AM. Reason: I think you'd probably have to echo it
 
Old 02-21-2017, 09:26 AM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
speechless
I am not even going to try and tackle that one just to see if I can do it.

The homework assignment is from a person that is a sadist.

If it is a basic course. If it is advance course then what is that question even doing in here?

Last edited by BW-userx; 02-21-2017 at 09:31 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
seeking USB connected audio input and output "box" SaintDanBert Linux - Hardware 4 08-08-2012 04:42 PM
Right-side-of-thirty "British" (I hate that word) Male GrubbySeismic LinuxQuestions.org Member Intro 4 01-15-2010 10:24 PM
"failed to execute child process" "Input/output error" fl.bratu Fedora 4 12-15-2008 04:03 AM
"/usr/bin/ls: reading directory .: Input/output error" DiZi Slackware 15 07-13-2008 10:03 AM
C program that takes input "FName|LName" and outputs to std out its_godzilla Programming 2 01-18-2005 10:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration