scripting headache
The task is to create a script that will prompt the user for the name of a famous Computer Scientist (Larry Wall, Edsger Dijkstra, Dennis Ritchie), and will output the quotes from that person, using the files created in a previous exercise.
For instance, I need to echo "Enter name of CS" read $NAME #and then be able to have the file print depending on which name I entered #I think using grep or find should solve that but I don't know how to go about that This is what I have most recently. When I enter a name, nothing happens and it ends the script. #!/bin/bash echo "Enter name of a famous Computer Scientist" read $NAME find .. -maxdepth 1 -name "*$NAME*$suffex*" -exec less '{larry-wall-quotes dennis-ritchie-quotes edsger-dijkstra-quotes}' \; |
read $NAME should be read NAME
$NAME - get the value of the variable NAME NAME - set the value of the variable NAME |
Okay. I revised the script and it still does not give me the desired output.
#!/bin/bash suffex="quotes" echo "Enter name of a famous Computer Scientist" read NAME find . -maxdepth 1 -name "*$NAME*$suffex*" -exec less '{}' \; |
What exactly does it do now? Do you have spaces in your filenames?
I would do something like this: If the filename is larry-wall-quotes: Code:
read NAME Of course you can also put the quotations in an indexed array and return the quotes according to the name entered. |
It worked thank you so much. The solution (provided by towheedm) is below.
#!/bin/bash echo "Enter name of a famous Computer Scientist" read NAME filename="$(echo ${NAME} | tr ' ' '-' | tr 'A-Z' 'a-z')-quotes" less ./${filename} |
Oh wow, it just occurred to me: I hope I did not do your homework for you.
|
That was the last portion of a lengthy assignment. I have been working on that specific exercise for three days. I tried contacting people in the class and the prof with no response. This was my last resort and I appreciate your help. Could you explain what you did?
|
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Also, when you have problems, don't just say "it didn't work", explain the error in detail. Post the exact command you used and the output you got, if any. Next, there's no need to use the external tr command when bash has built-in parameter substitutions that can do the same thing. Code:
filename="${name,,}" Follow the link for more on shell string manipulations. And don't forget to ALWAYS QUOTE YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later. http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes Finally, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them. |
Quote:
But to explain what was done: Code:
filename="$(echo ${NAME} | tr ' ' '-' | tr 'A-Z' 'a-z')-quotes" Now the actual commands: The '|' is a pipe. Basically, it sends the output of the first command to the input of the second command. So the command: Code:
"$(echo ${NAME} | tr ' ' '-' | tr 'A-Z' 'a-z')-quotes" $( : Start command substitution echo ${NAME}: Get the value stored in var NAME | : send the output of echo ${NAME} to the next command. The next command gets Larry Wall as it's input tr ' ' '-' : translate all space to emdash (-). This produces Larry-Wall | : Send Larry-Wall to the input of the next command tr 'A-Z' 'a-z' : Changes all uppercase chars to lowercase. This produces larry-wall ) : end command substitution. The result in the buffer is now larry-wall -quotes : append '-quotes' to the result os the command substitution. filename is therefore assigned 'larry-wall-quotes'. |
All times are GMT -5. The time now is 04:12 AM. |