LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2012, 09:35 PM   #1
mamba24
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
Question 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}' \;
 
Old 04-08-2012, 09:40 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
read $NAME should be read NAME

$NAME - get the value of the variable NAME
NAME - set the value of the variable NAME
 
Old 04-08-2012, 10:20 PM   #3
mamba24
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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 '{}' \;
 
Old 04-08-2012, 10:36 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.
 
Old 04-08-2012, 10:50 PM   #5
mamba24
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up

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}

Last edited by mamba24; 04-08-2012 at 10:52 PM.
 
Old 04-08-2012, 10:53 PM   #6
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Oh wow, it just occurred to me: I hope I did not do your homework for you.
 
Old 04-08-2012, 11:02 PM   #7
mamba24
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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?
 
Old 04-09-2012, 07:03 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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

Last edited by David the H.; 04-09-2012 at 07:05 AM.
 
1 members found this post helpful.
Old 04-09-2012, 10:24 AM   #9
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Quote:
Originally Posted by mamba24 View Post
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'.
 
1 members found this post helpful.
  


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
Headache! djbriuk General 16 01-27-2012 03:15 AM
I've had nothing but a headache with 13.37 hedron Slackware 24 10-25-2011 08:06 AM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Help im so new i have a headache jjh221 Linux - Newbie 3 02-11-2004 08:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:43 AM.

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