Bash shell script - variable magic needed (no idea how to approach this)
I'm trying to write a shell script that would determine the rhyme scheme for a poem (ABAB, AABB, ABCD, whatever).
I've already written a shell script that determines whether two words rhyme and returns a proper exit code for me to work with. I've already written a shell script that strips the last word out of each line of a given block of text and saves these last words as a new file, for easy working. What I'm having problems with is checking rhymes against previous words to see if a line rhymes with something before it. Let me explain. For example, a simple poem, rhyme scheme ABCB: Code:
On a couch Code:
couch Now the ideal thing would be to pass "couch" through the analysis script, and the script would assign the word "couch" as the variable "A" (for A rhyme). It would then activate a variable that makes it check all subsequent words against previous words, for rhymes. So in essence, this is what happens: 1. "couch" is the first word. It is therefore A. 2. "cat" is the second word. Check for rhymes with any previous lines. "cat" does not rhyme with "couch" so it is not A. It is therefore B. 3. "black" is the third word. Check for rhymes with any previous lines. "black" does not rhyme with "couch", so it is therefore not A. "black" does not rhyme with "cat", so it is therefore not B. It is therefore C. 4. "rat" is the fourth word. Check for rhymes with any previous lines. "rat" does not rhyme with "couch". It is therefore not A. "rat" DOES rhyme with "cat". It is therefore B. The script should be something like this: Code:
#!/bin/sh Code:
$./script poem |
I'm no genius, but my first thought was to create two arrays; one holding the word, and another holding the rhyme type (A, B, C...) so that the indices line up. That way, you have:
Code:
$words[0] = couch $rhyme_name[0] = 'A' Maybe I misunderstood your question and am way off, but I hope this helps. |
Quote:
|
Oh, I assumed you have some programming experience since normally people who are writing scripts have some sort of programming background; most of the things I mentioned would have been covered in a first year programming course. I can't really explain all of the things I mentioned in great detail because that would take a while. Instead, it might be a good idea to invest some time in reading a bash scripting tutorial. Particularly, you'd want to focus on sections involving arrays and for loops. A Google search should return plenty of good ones.
I'll try to be more clear, but you may still not understand until you read up on arrays and for loops. Here's the algorithm of my general idea. Code:
# i represents the current line starting at 0 |
Thank you for helping.
First year programming course? I'm only in high school. Took half a year of beginner ("Hello World"-equivalent) Visual Basic programming two years ago, and after that I've been finding out by myself, leaning heavily on Google. Never read a single Bash tutorial at all; again, things I've found out from trial and error, some logic, and a lot of Google. Quote:
Thanks again. |
That's cool that you're learning this early; I didn't start bash scripting until a few months ago. Anyway, word is the name of the array. This can be anything, whatever you declare it to be. i is just the index to access that element of the array. Check this out on bash script arrays. That and "for" loops is what you'll need to find out if a rhyme exists with a preceding word. Essentially, you count from the first line up to the current and use that count to access elements of the array and compare them with the current word.
I know this sounds really confusing, but it's too difficult for me to explain without going on forever explaining arrays and for loops. |
Here's a couple of good links to learn shell prog with:
http://rute.2038bug.com/index.html.gz http://www.tldp.org/LDP/abs/html/index.html Also prob worth buying the O'Reilly book if you are serious. |
Thanks for those links, but I've never in my life actually sat down and RTFM. I've always glanced at the first two pages, started trying stuff therein, running into problems and googling for the solutions. That's what I did with dubya's link; found the word "array" in the page, glanced at the code example, and made a simple shell script to discover how arrays worked. Already got my shell script working in a rudimentary form; trying to squash an odd bug, now. I'm not a manual person, that's just not how I do things. Give me a heavily commented example, a temp directory, and Google, and I'll figure it out.
Thanks, though. |
It's done! It works perfectly now. It just correctly transcribed Edgar Allan Poe's The Bells, the same as I did it by hand and about ten times as quickly!
I realise this code is probably very awkward, naïve and temeramental. It would be great if some of you told me how to clean it up a bit. I don't suppose there's any chance it could be made into some kind of cool web java\php\whatever application you could use on the internet? =D *nudge nudge* I do have one serious question, though. I had to use kdialog in one instance because it wouldn't let me use the read command. Why did it not let me use the read command? It just kept moving forward with the next line, instead of pausing, using that line as the "input" for the read command, which of course screws everything up. I realise not all of you have kdialog, so it is a problem. How can I get rid of it? Anyway, it consists of four scripts. They must all be in the same directory, executed as "./wrapper <poemfile>", where poemfile is a plain text poem and does not contain any lines you don't want scanned into the rhyme scheme except blank lines (i.e. no lines marking out stanza IV and such). The options -r and -c are allowed, used together as -rc, which cause identical words to be treated as NON-RHYMING, for -r, and which makes indexing of the poem continuous rather than by stanza, which is -c. --help or -h is also availab.e To be saved as "linestrip": strips the last word out of a line and removes punctuation. I had problems integrating this into the process script because it didn't like the ` character that's in the first sed statement when used as "stripped=`echo $line | sed ..........so on`" I would love it if someone told me how to integrate it. Code:
#!/bin/sh Code:
#!/bin/sh Code:
#!/bin/sh Code:
#!/bin/sh |
All times are GMT -5. The time now is 01:36 AM. |