LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-15-2013, 02:12 PM   #1
mrduck
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Unix shell script code


I am having alot of trouble figuring out how to finish my code. The assignment is to:

Create a shell script that returns a full name associated with a userid specified in the command line argument.


Use the names found in /class/rolls/CSCE215-8xx. (example CSCE215-801)
Your shell script should be named findName.sh
Example:
$ ./findName.sh grabasan
BILLY T GRABASANDWICH
$

Your shell script must either:
-Return the full name associated with the userid or
-Return an error message if the command line arguments are not equal to 1. or
-Return the message "Sorry that person is not in CSCE215-8xx" You are only responsible for your section. No other output is acceptable.

My code so far is:

#!/bin/sh
# cmdarg.sh -
# for use by csce215 students.
# for use , in some project.....

if [ $# -eq 1 ]; then
# if there is exactly one command line arg used with the command
# do something(s).


echo < " $1" > test
if [ -s ./test ]; then
cat test
else
echo "You should not see this line as output....."
fi
else

# tell the user how to use the command and exit the script
echo "usage: `basename $0` [only_one_argument]"



# userID does not match any student in that section
echo "Sorry that student is not in CSCE2215-801"
exit1
fi
 
Old 02-15-2013, 02:26 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Put your code inside of the "CODE" tags please, it helps us read it.

What is the format of the file that you are checking the data from? Is it the same format as a standard /etc/passwd file?

Also what are you having trouble with specifically?


A suggestion would be to change your argument test, don't check to see if it equals one, check to see if it does not equal one and exit with a return code of 1 and print an error to stdout for the user to see. Would be a little bit cleaner.

Also, what is the point of:

Code:
else
echo "You should not see this line as output....."
fi
Why do you have the else statement?
 
1 members found this post helpful.
Old 02-15-2013, 02:38 PM   #3
mrduck
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Unix Shell script code

I apoligize for it not being clean, if a user types ./findName <username> I must take that username and compare it to a file in /class/rolls/CSCE215-801 which has a list of usernames and full names as such:

Name: username:
Bob T Hopes hopesbo
Jenny Stevens stevens
etc.

The shell script is findName.sh and the names are in /class/roll/CSCE215-801
I need my script to look into the file, im guessing a grep command, and print the persons full name.
 
Old 02-15-2013, 03:21 PM   #4
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Yea, this is easy enough. A good exercise for someone learning.

As is the culture here we aren't going to write it for you but will help get you there yourself. Even if someone does post behind me with a solution I would highly recommend doing it yourself so you know. Thats the point.


Are there any delimiters in the file or is it all spaces?

This should be a pretty simple grep statement then use awk or even a basic cut to print your output to the screen.


The biggest things to worry about here are to set yourself up with some coding standards that work for you. Almost every company has their own which are slightly different but come up with some frameworks so you can get in the hang of it.


Create a template that has a comment section at the top so you can put in your name, the name of the script, and a description. You should also have a separate comment block to label the variables used in your script if they are important. This will help you in the long run when you go to re-edit a script you wrote a year ago and can't remember these little things.

Back to the task at hand. You noted three bullet points at the top which notes that only one of the tasks will be accomplished by yourself. Are you trying to do all three or focus on one individually at this point?


We talked briefly about testing for the number of arguments, did you make any changes there?


When you post output or code snippets use the tags:

CODE
and
/CODE

inside of square brackets [] and it will keep the formatting.
 
Old 02-15-2013, 10:33 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
May I ask, does it have to be sh or can it be bash? Either is fine just the second has some more features that may be helpful.

I would add to the good advise above and also say that a script is just a way to put multiple tasks together. So are you able to accomplish the individual steps
on the command line?
 
Old 02-16-2013, 05:59 AM   #6
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian, Arch
Posts: 1,161
Blog Entries: 7

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
Quote:
Originally Posted by grail View Post
May I ask, does it have to be sh or can it be bash? Either is fine just the second has some more features that may be helpful.
On many modern systems, /bin/sh will be either a symlink to /bin/bash or /bin/dash. If you want to ensure the script is run with bash, provided it is available on the system, make this the FIRST line in your script:

Code:
#!/bin/bash
 
Old 02-16-2013, 10:54 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
I am aware of both the symlink and the option to change the interpreter, however my question to the OP is, is bash available and as it is a course, are they allowed to use it.
 
Old 02-16-2013, 12:02 PM   #8
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,977
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Looks like you're not using an elif (among other "bad habits" but one thing at at time )

Kinda hard to tell without CODE TAGS
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
unix shell script question redss Programming 18 09-07-2012 04:40 AM
[SOLVED] unix shell script for housekeeping netha Linux - Newbie 10 05-22-2012 08:53 PM
For writing a unix shell script sunildas49 Linux - Newbie 3 11-25-2011 04:44 AM
Shell Script for Unix Date ??? ajeetraina Linux - Newbie 11 02-08-2008 12:58 AM
unix shell script cxy0481 Programming 9 11-20-2005 08:15 AM


All times are GMT -5. The time now is 05:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration