Friday, May 30, 2008

Script Shell Syntax checking

I'm writing a lot of stuff in shell (Korn,Bourne,Bash) and I had a need for syntax checker in the build time. Something I could use either in the command line (like a compiler) or in the make time which should stop the build whenever there is a syntax error in my script.

Here is what I came up to. Simply copy/paste the code below to a file named shck.sh (or whatever you wish to call it), do chmod+x shck.sh and off you go!

Usage:
shck.sh myscript.sh
-or-
cat myscript.sh | shck.sh

You can also use it in the makefile. It will return value 1 if there was an error.
-----------------------------------------------------
shck_return() {
    return $1
}
shck_errno=0
echo "set -n ">random.$$.sh
cat $1 >>random.$$.sh
chmod +x random.$$.sh
./random.$$.sh >errors.$$.err 2>errors.$$.err
rm -f random.$$.sh
grep -v ": warning: line" errors.$$.err | sed -e s/random.$$.sh/$1/g >errors.$$.err2
[ -s errors.$$.err2 ] && {
    shck_errno=1
    cat errors.$$.err2
}
rm -f errors.$$.err errors.$$.err2
shck_return $shck_errno

Wednesday, May 28, 2008

Opening unopenable attachments in Microsoft Outlook

The Microsoft Outlook software, starting, as I believe, from version 2003, turned opening some of the attachments for good. If you received, for example, an .mdb file (from Microsoft Access,) then you will only see that you got it, but you won't be able to copy it to the hard drive. Don't try searching in the options as suggested in the email. There is no option to turn the ability to detach the attachment on.
Microsoft says it clearly - because it is one of the most popular applications for managing emails, it has been constantly under attach by the hackers, so to 'save' their customers, they turned several options off for good. There is a workaround though. You can ask the sender to send you the file again, but ask him/her to rename the extension of the file or to zip/rar it. If such option is not a good one, or at least not for the very moment because you're in a hurry, there the following solution:

Run regedit: WARNING, WARNING: These operations are dangerous, so if you're not feeling secure, don't play with regedit. Screwing up in this file may screw up with your Windows installation and ultimatelly you may have to reinstall your OS!!!!

Open HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Security
(11.0 is for Outlook 2003)

Create or add a New String Value under this key with the name: Level1Remove

Enter the file extensions separated with the semicolon (;) that you would like to support in the attachment. The file extensions should start with a coma.

EXAMPLE:
Level1Remove .mdb;.xlsx

Typical errors:
- forgetting to put a dot in the front of the extension
- putting spaces into the Level1Remove value
- Putting Level1Remove string into improper place in registry
- Creating a KEY instead of a STRING VALUE in registry.

When you are done, close the regedit program, close your Microsoft Outlook application, and open it again. From that moment, the files with the extensions you have just added into registry will be available for you.

Make sure you don't overdo and don't add others files 'just in case'. You may put yourself in danger by allowing to accept .exe, .bat, .js and other executable files. Use your head!

jpg - changing internal date and time stamp

Ooops. I have set an improper date on my digital camera and now, Picassa shows my pictures in improper order. I take care of setting the date and time stamp of the file but even though, Picassa puts my new pictures amont the last year ones.

I started searching for a tool that would allow me to change the internal jpeg (jpg) date and time stamps, and I have run onto a very nice one - file Tweak (http://filetweak.com) The sad part this tool, being really an excellent Windows addition to the file properties dialog box, cost money, and I didn't want to spend money to, from time to time, fixing my mistakes. (You can download a limited in time and functionality filetweak from them.)

I have decided to take a closer look to the jpg file and check if I can change the date and time opening the file in binary mode (I use Visual Studio, but you can find other free tools that can do it for you.)

What a surprize - the date time is stored in DOS open format!
Example: 2008:05:25 22:36:40

In my jpeg files are three dates (when the picture has been taken, when was digitalized and when was modified.)

The easiest way then, to change the timestamp of your jpeg files is:

Search for the year your picture has been taken (Right-click on the file, Properties,Summary tab,Make sure Advanced button is on, Search for Date Picture Taken.)
Simply, using any binary editor, modify the date and/or time of the jpg files (up to three places to be changed) and save the file. You're done. Picassa and you will be happy :)

Saturday, May 24, 2008

Running a command for each value comming back from an ODBC query in PHP

I have a need to read an ODBC datasource and run a query on a particual table. For each unique value of the table, I need to run a command in the shell.

Here is anexample:


<?php
$values = Array() ;
$command = "echo" ; /* for the example purposes the command is echo */
$columnName = "state" ; /* just for this example */

$hdbc = odbc_connect ( "dsn-name", NULL, NULL ) ;
if ( $hdbc != NULL )
{
$hstmt = odbc_prepare(
$hdbc
, "select distinct " . $columnName . " from tablename"
) ;
if ( $hstmt != NULL )
if ( odbc_execute( $hstmt ))
{
while ( odbc_fetch_row( $hstmt ))
$values[] = odbc_result( $hstmt, 1 ) ;
}
else
printf( "*** Unable to execute\n" ) ;
else
printf( "*** Unable to prepare the query.\n" ) ;

odbc_close( $hdbc ) ;
}
else
printf( "*** Unable to connect\n" ) ;

foreach ( $values as $value )
system( $command . " \"" . $value . "\"" ) ;
?>

Friday, May 23, 2008

Publishing an Excel table as static html using jscript.

Oh man! I have worked on it a few days, posted numerous questions on various newsgroups, spent hours on the Internet and in a book store, and finaly I got what I needed!

Requirement:
I have a sheet (Sheet1) in my xlsx document (Excel 2007) which I wanted to save as a static html page using.
I want to use jscript run in the command line, so I could automate the process.
I don't want to use SaveAs( ..., 44 ) method because it creates the sheet name at the bottom.
I also don't want to have the ActiveX embedded into the page (no dynamic html.)

Assuming that the document is c:\mydoc\test.xlsx, I want to generate c:\mydoc\test.html file.

Here is the code of doit.js file:

ea = new ActiveXObject( "Excel.Application" ) ;
ea.DisplayAlerts = 0 ; /* Don't want to see any questions on the screen! */
wb = ea.Workbooks.Open( "c:\\mydoc\\test.xlsx" ) ;
po = wb.PublishObjects ;
po.Add( 1, "c:\\mydoc\\test.html", "Sheet1" ) ; /* 1 == static html. */
po.Item( 1 ).Publish( true ) ; /* Yahoo! */
wb.Close( false ) ; /* I don't want to save the newly added PublishObject */

Isn't that cool!

Tuesday, May 13, 2008

Setting up registry to run .php scripts from command line

I wanted to have a script in php that I could run in the following way:

c:\tmp> myscript.php argument1 "second argument"

This is what I came up to:

a) In the file explorer (not internet! file!) double click on your .php script, select the 'Select the program from a list', Browse to php.exe, mark 'always use the selected program to open this kind of file', and accept.

This, will create a HKEY_CLASS_ROOT/.php entry with the value php_auto_file.
It will also create the HKEY_CLASS_ROOT/php_auto_file/shell/open/command with the default entry: "c:\php\php.exe" "%1"
(the path to the php.exe file depends on where you have installed php in!)

This kind of registration will allow you now running your scripts from the command line but it WON'T allow you to pass any arguments.

b) That's why you have to the HKEY_CLASS_ROOT/php_auto_file/shell/open/command entry to the following: "c:\php\php.exe" "%1" %*

Here you go. Now you can prepare your myscript.php to look like this, and test the execution:
print_r( $argv ) ;
?>

myscript.php argument1 "second argument"

Result:

Array
(
[0] => c:\tmp\myscript.php
[1] => argument1
[2] => second argument
)

Friday, May 2, 2008

poll: protocol failure in circuit setup (Linux)

To tell the truth, I'm not sure it's the Linux issue only. It can be on other platforms as well.

If you saw something like this in telnet/ssh:

connect to address 1.2.3.4 port 544: Connection refused
Trying krb4 rsh...
connect to address 1.2.3.4 port 544: Connection refused
trying normal rsh (/usr/bin/rsh)
poll: protocol failure in circuit setup

then what probably causes it is firewall (iptables)
The quicky method is simply to shutdown the iptables firewall:
> su -
> services iptables stop

However, if you reboot your machine, the iptables will start and you will hit the problem again.

To permanently shut down the firwall service then, do the following:
> su -
> chkconfig --list iptables

- you will probably see this:
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off

- then do:
> chkconfig --level 345 iptables off
> chkconfig --list iptables

- now you should see this:
iptables 0:off 1:off 2:on 3:off 4:off 5:off 6:off

Of course, for the current session, you still will have to call service iptables stop, but I guess you already know it.