d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Linux Help > Please
Add Reply New Topic New Poll
Member
Posts: 2,957
Joined: Aug 29 2011
Gold: 0.00
Aug 20 2017 04:16pm
im just super stuck on part 2 of this assignment and I bet there's someone that can put me in the right direction very easily
I can give u my 27 fg if u help. sorry but its all I have lol.
Note: Part 1 just takes pulls 10 names from a list and sorts them
(ex. john.smith John Smith)

Objectives:

Practice input/output redirection and piping
Practice writing simple scripts
Practice archiving files using tar
Instructions:

Part I:
Build a user reference data file with the usernames and full names by extracting the necessary information from the /home/inst/glen.sasek/files/passwd file.

Sort the /home/inst/glen.sasek/files/passwd file and get line 100-110 of that file. Use the cut command to extract the username from /home/inst/glen.sasek/files/passwd Save the result file as ids.
Create another file called names which contain full names of line 100-110 of sorted /home/inst/glen/files/passwd in a similar way.
Use the paste command to combine the two files, ids and names, together side-by-side as shown below, redirect the output to a file called users.ref. For ease of processing, separate the two fields with a space. If there is any ‘,’ in the file, use an editor to get rid of them. The final version of users.ref should look like the following format:
sally.black Sally Black
jane.smith Jane Smith
lily.brown Lily Brown

Before you continue with Part II, it is a good idea to save a copy of users.ref. You will know why when you accidentally erase the contents of the data file.

Part II:


For the following steps, write shell programs that use command line arguments as input to the shell program. Do not prompt the user for input from the keyboard!
Note: Most of these shell programs need to be only 1 or 2 lines long.

Write a shell program called list to display all the usernames and full names in the users.ref file. The information should be displayed in alphabetical order by student's last name (not by username). Save the list shell program in your lab7 directory.
Write a shell program called lookup to locate an existing record in users.ref and display the student's username and full name on the screen. The search should not be case sensitive. This shell program should allow the user to specify either a student's username or their full name. Save the lookup shell program in the lab7 directory.
$ lookup "ryan.campbell3" or $ find 'Ryan Campbell'
Did you encounter any problems in the previous step? If so, what were they and how did you resolve them? Include a note describing the problems and solutions in a file called lab7.notes.
Write a shell program called remove which deletes a student's entry from your users.ref file. Similar to lookup, the user should be able to specify the student to be removed by their username or their full name. Save the remove shell program in lab7. (hint: grep has an option "-v". Go check it out!)
Write a shell program called add to add a new username and full student's name to your users.ref file. Your program should be used in the following manner (hint: echo hello >> file):
$ add woody.brown 'Woody Brown'

Part III:
Verify correct operation of your shell programs. Use the script command to record your test results. Type script at the command line. This will start a new shell that "captures" all input and output and records it in a file named typescript in your current directory.

Execute the pwd command.
Execute the ls command.
Execute your list command.
Execute your lookup command (or whatever you named it) twice. (Once to locate a student by their username and a second time to locate them by their full name.)
Use your remove shell program twice: once to remove a student using their username, another removing them by their full name.
Use your add shell program to add an entry to the file
Execute your list command a second time.
Type exit (or ctrl-d) to exit the script shell. The input and output were written to a file called typescript. Change the name of this file to script1 and save it in the lab7 directory.

Part IV:
Use the "tar" (tape archive) command to wrap all the files into a single file, and then send via mailx . The files to wrap with the tape archive are your final versions of:

list
lookup
remove
add
lab7.notes
script1
Copy all of the final versions of your shell programs into your lab7 directory. Then use the following tar command to create your archive:

tar –cvf lab7.tar lab7
Be sure to include the latest version of each of your files! Move lab7.tar to a temp directory. Try the following command to confirm you have included everything
tar –xvf lab7.tar
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Aug 20 2017 06:13pm
Any particular piece of part 2 that's giving you trouble?
Member
Posts: 27,177
Joined: Mar 27 2008
Gold: 445.00
Aug 20 2017 06:13pm
So what you are looking for is the getopt() function, short for get options. Pass the name to the script as an option then manipulate as needed.
Member
Posts: 2,957
Joined: Aug 29 2011
Gold: 0.00
Aug 20 2017 08:05pm
Quote (Mastersam93 @ Aug 20 2017 05:13pm)
Any particular piece of part 2 that's giving you trouble?


now ive got it all figured out but skipped 1 thing im having an issue with

How do you use grep to search for a first and last name?

This is my script and it gives me this error (grep: Santos: No such file or directory)

echo -e "Please enter the name you would like to search for."
read "name1" "name2"
grep $name1 $name2 ~/Labs/Lab7/users.ref
Member
Posts: 27,177
Joined: Mar 27 2008
Gold: 445.00
Aug 21 2017 05:31am
Pipe the output of the first name into the second.

cat ~/Labs/Lab7/users.ref | grep $name1 | grep $name2
Member
Posts: 2,957
Joined: Aug 29 2011
Gold: 0.00
Aug 21 2017 03:02pm
Quote (ROM @ Aug 21 2017 04:31am)
Pipe the output of the first name into the second.

cat ~/Labs/Lab7/users.ref | grep $name1 | grep $name2


Thanks for the reply. Maybe I entered something in wrong but it wasn't working for me.. I'll try playing around with it when I get off work and see if I can get it working
Member
Posts: 27,177
Joined: Mar 27 2008
Gold: 445.00
Aug 21 2017 04:35pm
Quote (KronikSmoker @ Aug 21 2017 05:02pm)
Thanks for the reply. Maybe I entered something in wrong but it wasn't working for me.. I'll try playing around with it when I get off work and see if I can get it working


Keep in mind that the shell script is in a different level then bash.
Try that outside of your script and it will work and should point in the right direction.

This post was edited by ROM on Aug 21 2017 04:36pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll