Shell Scripting Practice

Shell Scripting Practice

Post by Karec on Thu Feb 26, 2009 1:39 pm
([msg=18734]see Shell Scripting Practice[/msg])

For those who are interested, I am currently taking an "Intro to Systems Programming" class. Here's a sample assignment I got recently to give you guys an idea what the hell they expect from Comp Sci Major's in college.

I'll probably post my (Sucky) solution March 2nd when this is due.

This assignment is an introduction to shell programming in a Unix environment. The purpose is to learn to make use of a variety of Unix utilities for some other utilitarian purpose.

NOTE: There should be NO C programming in this assignment (or any other compiled language). Also, shell programs should also be kept to a minimum, to maximize use of the provided utilities.
PROGRAM SPECIFICATIONS
Create these three utility scripts:

recomment Replaces C99 comments with C89 comments.
This would make one big step towards being able to use ctrace on a program.
No other conversions of C99 to C89 will be required for this assignment.

pretty Pretty Print a C99 program, including (no particular order)

1. Place all { and }'s on their own line, EXCEPT when they are empty.
2. Only one statement per line (statement ends with a ';')
3. Convert all lowercase #define's to uppercase, to highlight them
4. Indent all nested code by a command-line specified number of columns.
This includes the bodies of if statements, loops, and functions.
5. Break long lines no later than column 70; indent continuations one further position.
6. Make NO changes to the interior of comments!


e.g. To indent 4 columns at a time: pretty -i4 prog.c

spellchk Identify possible spelling errors in a text file.
Display all lines with suspected errors, with the error highlighted from beneath, with the line number from the file. E.g.

79 --- These are some rather poweful utilities
-------


Use /usr/dict/words as a list of valid words. Include appropriate requirements and checks for lowercase vs. uppercase.

In addition, allow one or more additional word files listed in the command line.
e.g. spellchk -f mynouns textfile

USEFUL UNIX COMMAND UTILITIES
These are some rather powerful utilities you may use:
awk pattern scanning and processing language
grep search a file for a pattern
sed stream editor (line by line)

These utilities have a much more defined focus, but are also very useful.
cat concatenate and display files
comm select or reject lines common to two files
diff compare two files (line-by-line)
echo echo the arguments
getopts parse utlity options
sort sort, merge, or sequence check text files
tee replicate the standard input
tr translate characters
uniq report or filter out repeated lines in a file

But these utilities may not be used (for obvious reasons) except for evaluating results.
cb C program beautifier
indent indent and format a C program source file
spell report spelling errors

A REAL SHORT EXAMPLE
Sample input:

// Greatest Common Factor
// recomputes A mod B

#define mod(a,b) a%b

int gcf( int argA, int argB )
{
int a = abs(argA);
int b = abs(argB);
int rem;
while (rem = mod(a,b)) {
a = b; b = rem; }
return b;
}

Recomment:

/* Greatest Common Factor */
/* recomputes A mod B */

#define mod(a,b) a%b

int gcf( int argA, int argB )
{
int a = abs(argA);
int b = abs(argB);
int rem;
while (rem = mod(a,b)) {
a = b; b = rem; }
return b;
}

Pretty:

// Greatest Common Factor
// recomputes A mod B

#define MOD(a,b) a%b

int gcf( int argA, int argB )
{
int a = abs(argA);
int b = abs(argB);
int rem;
while (rem = MOD(a,b))
{
a = b;
b = rem;
}
return b;
}

TESTING AND VERIFICATION
Summary of usages (all results to standard output)

recomment
pretty [-i ]
spellchk [-f file]

The TA will be given some other syntactically valid C99 program to test two of these scripts, and a sample test file to verify the spellchecker.

More credit will be given for doing less coding, that is, in having the existing utilities do the work. Use pipes to pass information from one utility to another.

A script may use a loop for a foreach item in a list, but should do little more than that. At the extreme, solutions that do all of their work in the shell script (or worse, in C) instead of using the utilities will receive minimal credit.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-Rick Cook, The Wizardry Compiled
Karec
Poster
Poster
 
Posts: 127
Joined: Sun Apr 13, 2008 2:39 pm
Blog: View Blog (0)


Re: Shell Scripting Practice

Post by godofcereal on Thu Feb 26, 2009 2:17 pm
([msg=18735]see Re: Shell Scripting Practice[/msg])

Seems pretty interesting. Cant wait to see more. Think about doing a lecture some time.
Im off, last year of school and all, I had something longer but char limit fucked that up. So yeah, had a good run here. Thanks for the memories. Thanks to the staff and users.

Best regards, your posting whore,
godofcereal

p.s. Defience, you the man ;)
User avatar
godofcereal
Addict
Addict
 
Posts: 1068
Joined: Wed Aug 20, 2008 6:11 pm
Location: ireland
Blog: View Blog (0)


Re: Shell Scripting Practice

Post by Israel on Sat Mar 07, 2009 5:38 pm
([msg=19513]see Re: Shell Scripting Practice[/msg])

I'm pretty sure you could do this by taking it in C and throwing it in a hex editor. But I would still like to learn how your going to do it the teachers way. Definitely think about that lecture GodofCereal talked about :)
User avatar
Israel
Experienced User
Experienced User
 
Posts: 74
Joined: Thu Sep 18, 2008 9:53 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests