Code Snippets

Joining flat files using shell scripting

Well, I like databases and thought why don't make a small relational database based on flat files and do joining them using Linux/Unix shell. I tried to invent this small example:

1. Have 2 flat text files, one for Employees (emp.tbl) and the other for Departments (dept.tbl), like the following:

emp.tbl content:
emp_id emp_name dept_id
1      Rami     1
2      Emad     1
3      Mary     2
4      Hassan   1
5      Rasha    2

كيف تضع رابطة لبيان "معاً أمام الله" فى موقعك؟

إختار حجم الصورة التى تريد وضعها فيما يلى وإنسخ الـHTML code إلى نموذج موقعك (Template) فى المكان اللى إنت عايزه:

الصورة الصغيرة
Call for Peace & Openness
HTML Code:

<a href="http://egyptnow.blogspot.com/" target="_blank"><img src="http://img463.imageshack.us/img463/3382/mm21xc.jpg" alt="Call for Peace & Openness" /></a>

Create a Recent Posts Block in Drupal

I needed to make a block in drupal that contain my recent posts and so, I write this code snippet.

<?php

// Get all node IDs and it title from the "node" table
// Customize the types retrieved from the "type" field
// (Status=0 & Moderate=0) mean the post is published
$result = mysql_query("SELECT nid, title
                       FROM node
                       WHERE (type='story' or type='blog'
                              OR type='flexinode-1')
                       AND NOT (status=0 and moderate=0)
                       ORDER BY nid DESC"
)
                       or die(
mysql_error());

echo "<ul>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
  
// Print node titles linked to its page into unsorted list
  
echo "<li>";
   echo
"<a href=\"http://ikhnaton2.com/
            node/{$row['nid']}\">$row['title']}</a>"
;
   echo
"</li>";
}
echo
"</ul>";

?>

Enjoy :)

Another trick: Display a list of node titles from a specific category

Convert Date/Time to Unix Timestamp using PHP

Very helpful function to convert date/time string to Unix timestamp. I needed this function to update the timestamp field of some comments in my Drupal after migrating them from my blog on blogspot.

Retreiving Information from MySQL using PHP

If you need to get data in your MySQL database and display them on your dynamic PHP page.

In this example we will select everything in our table "example" and put it into a nicely formatted HTML table.

My First Experience with Desktop Korn Shell (dtksh)

The Desktop Korn Shell (dtksh) is a standard part of the Common Desktop Environment (CDE), available on commercial Unix systems such as Solaris, HP-UX, and AIX. It is based on a somewhat older version of ksh93. It's a full Korn shell that has extensions for graphical user interface (GUI) programming in the X Window System environment. It is typically found in /usr/dt/bin/dtksh.

There are various GUI development tools that allow you to construct user interfaces with a graphics-based editor rather than with programming language code. But such tools are typically huge, expensive, and complex. dtksh, on the other hand, is inexpensive and unbeatable for the smoothness of its integration with Unix -- it's the only such tool that you can use as your login shell!

Benefits of Desktop Korn Shell:
  • Dtksh based on KornShell
  • New desktop builtin APIs
  • Dtksh shipped with CDE compliant systems
  • Plug-n-play
  • Machine independent compatibility
  • Migration path to "C"

It's really interesting to drive a GUI very fast and easy from within a shell. Here is a script that I had played with to give the flavor of dtksh code. It simply implements two buttons, one for listing files and the other to exit the GUI.

#!/usr/dt/bin/dtksh

XtInitialize TOPLEVEL dttest1 Dtksh $0
XtSetValues title:"My IT Experience"
XtCreateManagedWidget BBOARD bboard XmBulletinBoard resizePolicy:RESIZE_NONE height:300 width:350 background:Blue

activateCB() {
ls -ltr
}

XtCreateManagedWidget BUTTON pushbutton XmPushButton background:red foreground:white labelString:"List Files" height:50 width:250 x:75 y:60 shadowThickness:3
XtAddCallback activateCallback activateCB

activateCB2() {
exit 0
}

XtCreateManagedWidget BUTTON2 pushbutton XmPushButton background:green foreground:white labelString:"Exit" height:50 width:250 x:75 y:130 shadowThickness:3
XtAddCallback activateCallback activateCB2

XtRealizeWidget
XtMainLoop


More References:
Common Desktop Environment: Desktop KornShell User's Guide, published by Sun Microsystems
Article by George Kraft IV
Tutorial by George Kraft IV
Common Desktop Environment, Programmer's Guide by HP

----
Your comments or questions are mostly welcomed.

Syndicate content