LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scripting headache (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-headache-938814/)

mamba24 04-08-2012 09:35 PM

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}' \;

towheedm 04-08-2012 09:40 PM

read $NAME should be read NAME

$NAME - get the value of the variable NAME
NAME - set the value of the variable NAME

mamba24 04-08-2012 10:20 PM

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 '{}' \;

towheedm 04-08-2012 10:36 PM

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

filename="$(echo ${NAME} | tr ' ' '-' | tr 'A-Z' 'a-z')-quotes"
less /path/to/${filename}

Or if you must use find, then replace "*$NAME*$suffex*" with ${filename}. I not very familiar with find, so I'll assume you options are correct.

Of course you can also put the quotations in an indexed array and return the quotes according to the name entered.

mamba24 04-08-2012 10:50 PM

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}

towheedm 04-08-2012 10:53 PM

Oh wow, it just occurred to me: I hope I did not do your homework for you.

mamba24 04-08-2012 11:02 PM

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?

David the H. 04-09-2012 07:03 AM

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,,}"
filename="${filename// /-}-quotes"
less "./$filename"


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.

towheedm 04-09-2012 10:24 AM

Quote:

Originally Posted by mamba24 (Post 4648034)
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?

Well David is correct in all he said.

But to explain what was done:

Code:

filename="$(echo ${NAME} | tr ' ' '-' | tr 'A-Z' 'a-z')-quotes"
$(command) is command substitution. It assigns the value returned by the command to the variable filename.
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"
If NAME is Larry Wall:
$( : 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.