Linux Journal Scripting Games?

For those of you who were unhappy that Script Frenzy was not about writing scripts but about writing...well...scripts, here is something to entertain you...The 2010 Scripting Guy Games! Huh? Windows? Um...

OK, yes the Scripting Guy Games are for Windows. But what if we did it for Linux. What sort of meaty script challenge could we come up with? Certainly something more challenging than determining the time or reading the registry. Here are some thoughts, with apologies to the Scripting Guy(s) for stealing their format.

Day 1: Beginner: Create a script to add a user to the system, provision their mail, IM, shares and home page.

Day 1: Advanced: Create a script to add a new system to the infrastructure, provision it and bring it on line without manual configuration.

Day 2: Beginner: Create a script to deprovision a departing user account, back up their files and move them off into storage.

Day 2: Advanced: Create a script to migrate an high-availability web cluster with MySQL database to new hardware, provisioned using the script from Day 1.

All four of these tasks were ones that I got hit with about two days into a job several years ago. The fun part was that we had to use basic shell commands. We did not have perl installed on the systems for security reasons, and we did not have ruby, or python or any of those wonderful tools. Just a bare bones installation of the kernel and bash (although feel free to use your shell of choice...ksh anyone?) - the tools needed to do the job at hand.

So, what do you think? Are you up to the challenge? Maybe we should set up a Scripting Gurus help and trade area on the site! Hmm...did I just hear our beloved webmistress whimper?

______________________

David Lane, KG4GIY is a member of Linux Journal's Editorial Advisory Panel and the Control Op for Linux Journal's Virtual Ham Shack

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

A couple challenges

Vance's picture

Here's one that's almost as trivial as the Scripting Guy's exercises.

Beginner: Take as input a list of tab-separated values. One column of this input will be numeric. Sort the list by the numeric column in ascending value and output the sorted list.

Example input:

yellow	2	banana
red	1	apple
purple	3	passion fruit

Example output:

red	1	apple
yellow	2	banana
purple	3	passion fruit

Advanced: Same task, but the number of numeric columns is unknown. You may assume that each column contains either text or numbers, but not a mix of both (unless you go for the third extra points goal). The script must exit with an error if no numeric column is found. If multiple numeric columns exist, prompt the user for which one to sort by.

  • Extra points awarded if the script accepts a command-line switch allowing the user to pre-select the sort column without prompting.
  • Extra points awarded if the script permits the user to specify (by prompt or command-line switch) ascending or descending sort.
  • Extra points awarded if the script checks all the rows in the input before deciding that a column is numeric.
  • Extra points awarded if a separator character other than tab can be specified on the command line.

This one is more involved, but accomplishes a more useful task.

Beginner: You have a directory hierarchy full of files that may have odd filenames (mixed case, spaces and other special characters). Create a script to copy these files to another location while maintaining the directory structure, changing all names so they only use the letters a through z (lowercase), numerals, period, underscore, and hyphen. The first character of any filename or path component may not be a hyphen. The script must avoid filename collisions (e.g., if the original directory contains foo.txt and FOO.TXT, these files must both end up in the new hierarchy and have different names).

Advanced: Same task, with the added constraint that the script must maintain the file extension (e.g., foo.txt and FOO.TXT can become foo.txt and foo_2.txt, but not foo.txt and foo.txt_2). Also, generate a list on standard output of the old filenames mapped to the new ones.

  • Extra points awarded for adding a command-line switch to save disk space that, instead of copying the files, will create the new files as symbolic links to the original files.
  • Extra points awarded for minimizing mangling of filenames (e.g., converting accented characters to their unaccented equivalents, special characters to a descriptive name [50%foo.txt to 50_percent_foo.txt]).

I keep meaning to try these.

Anonymous Joe's picture

I keep meaning to try these. I know how I would go about doing them. I love awk so I'd do something like this for the first beginners challenge (off the top of my head)...


#! /bin/bash

awk '{print $2, $1, $3}' inputfile | \
sort | \
awk '{printf("%s\t%s\t%s\n", $2, $1, $3)}'

Re: I keep meaning to try these.

Vance's picture

That's very close to working on the example. But awk by default splits on any whitespace, not just tabs, so "passion fruit" would be split into two fields (hint: check out the FS variable or -F option).

Actually, you could rewrite your script to remove awk entirely if you used the -k option of sort.

However, one thing I didn't make explicit (how tricky of me :-) in describing the first exercise is that you don't know which column is the numeric one. Here's another possible input:

loop	cross	wind	27.4
string	link	Et tu?	1375
wire	AC/DC	ring	3

And the correct output:

wire	AC/DC	ring	3
loop	cross	wind	27.4
string	link	Et tu?	1375

Your script needs to somehow figure out which column is the numeric one. I hadn't thought of using awk to do this, but since you brought it up, I think it would work nicely for that job. Happy scripting!

(P.S. I don't know how long comments stay open, so if you want to reach me directly you can do so at vkochend at nyx.net).

i would love that, but i

supereater14's picture

i would love that, but i would go with different challenges

Challenges

David Lane's picture

I just pulled something off the top of my head as the challenge...that being said, what sort of challenges would you like to see?

David Lane, KG4GIY is a member of Linux Journal's Editorial Advisory Panel and the Control Op for Linux Journal's Virtual Ham Shack

well, your ideas seem to be

supereater14's picture

well, your ideas seem to be centered towards setting up a network. every network is different, therefore, it would be difficult to judge scripts based on a specific network. i would also go with something other than setup. but that's just my opinion.

I am up for that :)

aitd's picture

I am up for that :)

Sounds like a great idea

sevaine's picture

This sounds like the kind of thing I'd really enjoy. Be very interested in getting involved if something like this came about.

why?

frank-green's picture

why? there are literally hundreds of well established sites doing this already. support them rather than invent the wheel again.

Because...

p2bc's picture

it would be fun.

If you don't agree,then...

Sounds like fun

mattcen's picture

Sounds like heaps of fun.

I really don't need another distraction from my uni assignments at the moment, which have their own programming involved, but I'm definitely up for some scripting :-).

--
Regards,
Matthew Cengia

This would be awsome. I'm no

Anonymous's picture

This would be awsome. I'm no guru but I'd love to help.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix