Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Monday, 15 June 2009

HTML forms & CGI C-Shell scripting

HTML forms are used to select different kinds of user input (Here's a quick tutorial). For example, open your favourite text editor, copy-paste the commands from listing 18.6. HTML -- Form processing example from here) and save the file as text.html.

Then from your browser, open the file to see what the form looks like. It should look like this:


Reading the form data from your shell script is a bit more tricky. You will probably have encountered html pages that look like http://www.someaddress.com/somescript.cgi?param1=value1&param2=value2

value1 and value2 are the parameters that your HTML form has submitted when you hit the submit button (assuming your form only has 2 parameters).

To read them in your c-shell cgi script, use
set input = $<>param1=value1&param2=value2)

so then you will have to split the string and read the parameters individually in different variables:
set param1 = `echo $input | cut -d '&' -f1 | cut -d '=' -f2`
and the same for param2.



Tuesday, 26 May 2009

Editing multiple files with sed

Sed is a very useful stream editor for Linux.
A stream editor is used to perform basic text transformations
on an input stream, such as replacing a text string with another
text string. While in some ways similar to an editor which permits
scripted edits(such as ed), sed works by making only one
pass over the input(s), and is consequently more efficient. But it
is sed's ability to filter text in a pipeline which particularly
distinguishes it from other types of editors.

For example, if you want to replace the text "a rainy day" with
the text "a bright and starry night" in a text file containing
the sentence "It was a rainy day outside.", you can do it from the prompt
like this:

sed -i "s/a rainy day/a bright and starry night/g" textfile.txt

This command will have changed the text in your file to "It was a bright
and starry night outside.
". The nice
thing is that you can use it to edit
the same string in multiple
files at once. You can also use other
separators than "/". The
above command would have been equally valid
if it was written as:


sed -i "s|a rainy day|a bright and starry night|g" textfile.txt

All in all, sed is quite a powerful way to manipulate strings.

Thursday, 14 May 2009

Timing program executions on Linux

Linux has a useful command to do this: time

The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard output giving timing statistics about this program run. These statistics consist of (i) the elapsed real time between invocation and termination, (ii) the user CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2)), and (iii) the system CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2)).


For example, if your c-shell script is called blah.csh and takes two arguments (say, arg_1 and arg_2), you can find out how long it takes to run by executing it as:
> time source blah.csh arg_1 arg_2