advanced shell scripting
Page 1 of 1

Author:  jhurley03 [ Wed Jul 14, 2010 10:29 am ]
Post subject:  advanced shell scripting

We're going to go back to our text from Assignment 4, except this time we're going to write an utility to change the matching string for us.

"Bill Gates" with "Mr. Noodles",
"Gates" with "Noodles",
"Paul Allen" or "Allen" as "The Other Guy",
"Altair" as "WTF",
"Apple" as "The Prodigal Son",
"Tandy" as "Who?"
and "Microsoft" as "Blue Screen of Death"

"Bill Gates was born in Seattle in 1955, the second of three children in a well-to-do family. His father, William H. Gates II, was a lawyer, while his mother, Mary Gates, was a teacher, a regent of the University of Washington, and member of several corporate boards. Gates was first exposed to computers at school in the late 1960s with his friend Paul Allen, the son of two Seattle librarians. By the time Gates was 14, the two friends were writing and testing computer programs for fun and profit.
In 1972 they established their first company, Traf-O-Data, which sold a rudimentary computer that recorded and analyzed traffic data. Allen went on to study computer science at the University of Washington and then dropped out to work at Honeywell, while Gates enrolled at Harvard. Inspired in 1975 by an issue of Popular Electronics that showed the new Altair microcomputer kit just released by MITS Computer, Gates and Allen wrote a version of BASIC for the machine. Later that year Gates left college to work full time developing programming languages for the Altair, and he and Allen relocated to Albuquerque, New Mexico, to be near MITS Computer, where Allen took a position as director of software development. Gates and Allen named their partnership Micro-soft. Their revenues for 1975 totaled $16,000.
A year later, Gates published "An Open Letter to Hobbyists" in the Altair newsletter, in which he enjoined users to avoid illegally copied software. Arguing that software piracy prevented "good software from being written," Gates wrote prophetically, "Nothing would please me more than being able to hire ten programmers and deluge the hobby market with good software." In November 1976 Allen left MITS to devote his full attention to Microsoft, and the company's tradename was registered. In 1977 Apple and Radio Shack licensed Microsoft BASIC for their Apple II and Tandy computers, with the Apple license going for a flat fee of $21,000. As Apple sold a million machines complete with BASIC, Microsoft's unit revenues dropped to two cents a copy.
That same year Microsoft released its second programming language, Microsoft FORTRAN, which was followed in 1978 by a version of COBOL. Both were written for the CP/M operating system, one of many available in the rapidly expanding but still unstandardized microcomputer market. As CP/M was adopted by computer manufacturers including Sirius, Zenith, and Sharp, Microsoft became the leading distributor for microcomputer languages. By the end of 1978 Microsoft had 13 employees, a sales subsidiary in Japan, and $1 million in revenues. The following year Gates and Allen moved the company to Bellevue, Washington."

The following script reads in a file (supplied by a line argument) line by line into a varilable called linein:

#sample code for reading in a file line by line.

while read linein
echo $linein
done < $fileIn

The following snippet replaces the first occurance of a substring inside of a string variable:

${stringZ/abc/xyz} #This looks at a string named 'stringZ' and would replace the first occurance 'abc' with 'xyz'

This snippet replaces ALL occurances of the substring inside of a string variable:


So now you need to put it all together:
your script will work like this:

fileStringReplace <filename> <stringToSearchFor> <stringToReplaceWith>

It will replace 'stringToSearchFor' with 'stringToReplaceWith' inside 'filename'. It will return how many strings were replaced.

Author:  Patsie [ Wed Jul 14, 2010 12:57 pm ]
Post subject:  Re: advanced shell scripting

Sounds like a nice homework assignment :)
Have fun!

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group •