LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2007, 09:10 PM   #1
ccwq
LQ Newbie
 
Registered: Aug 2007
Posts: 3

Rep: Reputation: 0
Post Shell programming: Question about source files and read data from the files


This is shell programming assignment.
It needs to create a file called .std_dbrc contains
STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB
(which includes all my simple database files)

and I am gonna use this .std_dbrc in my script file (read the data from the database files)
like this: . ${HOME}/.std_dbrc (means source a file or folder)
to import all database to my script and use them.

My question is what do I need to do after use . ${HOME}/.std_dbrc in my script? I am not sure how to use it.
Please help.
 
Old 08-04-2007, 09:11 PM   #2
ccwq
LQ Newbie
 
Registered: Aug 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Here is something about my assignment

1. Student Database
In this assignment you will design a simple file based database. All the files are in
the simplest form of CSV (Comma Separated Values) where every pair of adjacent fields
are sperated by a comma and the fields do not contain any commas, not even escaped.
Every line contains a single record and every record contains several fields. A set of files
will be provided and your code will be tested against these files as well as a few more that
you will not know in advance. You can use either ash or bash. You can use any stan-
dard utility we discussed in class but you cannot write a program in any other program-
ming environment. Your programs do not create any file (other than a possible temporary
file in the /tmp/ directory) anywhere and print everything in the standard output.
2. Files in the Database
The database will have three types of files. The first, of which there is only one
instance, is the Accounts file. It contains records that have three fields. The first field is
an account number, the second is the last name of the student and the third is the first
name. Please note that some students have names with non letter characters like space,
dot or hyphen.
The second type of file is the enrollemnt file, one for each course and they are
named CSExxxx where the x’s are digits, very much like our own courses. They contain
a series of records, each one with a single field, an account number. If a student has regis-
tered for a course, his account number appears in the corresponding file.
The third type of file is the marks file, the one submitted by course instructors one
for each course, are named MARKSxxxx and are associated with the corresponding
CSExxxx course. The files contain several records, one for each student in the course
that received a mark. Every record contains two fields: the account number and the stu-
dent mark from 0 to 100. If a student is in the CSExxxx but has no record in the corre-
sponding MARKSxxxx gets a zero. If the file CSExxxx exists but the file MARKSxxxx
does not, all students get IP (that is In Progress).
There is one more file in the database and this resides in the home directory of the
user, is named .std_dbrc and contains exactly one statement
STD_DBROOT=<blahblah>
and this is used by your scripts to find where is the directory of the database.
1
3. Scripts
You have to write four scripts. All scripts follow the specification regarding the
startup file .std_dbrc and provide reasonable error reporting. All missing files should
be reported. The output should be exactly as in the specification without anything extra.
They will be tested both by hand and by automarking.
3.1. Find the Name
Write a script std_db_acc2name that given the account number of a student as a
command line argument returns the name as comma separated values.
3.2. Find the Courses
Write a script std_db_acc2courses that given the account number of a student
as a command line argument returns the comma separated list of courses the student is
registered.
3.3. Find the Marks
Write a script std_db_acc2marks that given the account number of a student as
a command line argument returns the comma separated list of courses and marks the stu-
dent has received.
3.4. Create the Table
Write a script std_db_acc2table that returns a CSV file with the following
structure. All records in the file contain the same number of fields. The first record, is the
header record and its first field is just the string “Std. Name”. The rest of the fields are the
4 digit codes for the all the courses. All the subsequent records have as their first field the
student name (first name first, then last name and no comma) and the rest of the fields are
the corresponding marks, blank if the student is not registered in the course and the regu-
lar mark or IP if the student is registered. Students that are registered in no course do not
have a record in this table.
 
Old 08-04-2007, 09:11 PM   #3
ccwq
LQ Newbie
 
Registered: Aug 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Faq

Frequently Asked Questions for Assgn 3.
What does the .std_dbrc file contain?
Something like:

STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB

You do not submit this file, I will be using mine that will point to my std_db. But it should work with such a file, otherwise your program will not pass the tests.
Do we need the trailing slash (/) in the STD_DBROOT definition?
Your program should work even if there is no trailing slash. The idea is that two or more slashes are equivalent to one, so it is better if you have two slashes rather than none (disaster).
Can we invoke one of our scripts from within another script?
In principle yes, but it is far safer if you do not unless you know what you are doing. If I were you I would opt for cut and paste.
How do I use the .std_dbrc from within my own script?
You source it. Sourcing is normally just a dot (.).

. ${HOME}/.std_dbrc

If you create this file and it seems lost use

ls -a

Why can't I see my .std_dbrc with ls?
Because files that start with a dot are hidden in Unix/Linux. Use ls -a to see all your files in the current directory. I mostly use ls -ltr or ls -latr, to see the most recent (and thus most relevant files) last (the first one might have scrolled above the screen).
What does it mean to "source" a file?
The manual says:

. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command exe-
cuted from filename. If filename does not contain a slash,
file names in PATH are used to find the directory containing
filename. The file searched for in PATH need not be exe-
cutable. When bash is not in posix mode, the current directory
is searched if no file is found in PATH. If the sourcepath
option to the shopt builtin command is turned off, the PATH is
not searched. If any arguments are supplied, they become the
positional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read.

most of the details there are irrelevant to you, but the thing to keep in mind is that it executes commands from filename in the current shell environment.
Do I need to change the PATH environment variable for this assignment?
NO! You should not change the PATH. In general we do not play with PATH unless we know what we are doing.
 
Old 08-05-2007, 12:13 AM   #4
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
Please don't ask Homework questions.
The point of an assignment is to learn. How can you learn if we do all the work?

If you want to ask a question, then come up with a question that shows that you have put some effort into understanding what is being asked.

It looks like you are trying to import the contents of your .std_dbrc file. Obviously, you will need to open it for reading, read the contents into local variables and then close the file. Then, you will need to do something based on the contents of the variables.

--Ian
 
  


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
Shell programming with CGI/Letting users write to files. sunksullen Programming 8 07-02-2007 01:30 AM
How to read long files on the shell? Gins Linux - General 12 06-24-2007 02:07 AM
shell programming - removing files with spaces ANU Programming 4 08-08-2006 04:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:02 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
Open Source Consulting | Domain Registration