Console

Grep duplicate JSON keys

If you have large JSON files with application settings in, you need to be sure that the settings only appear once. Not a problem until you get to the stage of very large files, being edited by all sorts of people manually.

[
"setting_1" : "some value",
"setting_2" : "another value",
"setting_1" : "different again"
]

Run a script to check for duplicate key names:

grep -Po '"[a-z_0-9]+"[ ]*:' <filename> | uniq -d

The above will output the duplicated setting(s) if any, to the console. Tested on Ubuntu 12.04

Compress files and folders in tar (Linux)

GZIP and TAR are two different utilities bound by the same cause. Compressing files and making file catalogs.

To compress a file, simply gzip it:

$> gzip database_dump.sql

That will compress the database_dump.sql file, and rename it to database_dump.sql.gz. Easy, and to uncompress it:

$> gunzip database_dump.sql.gz

That will restore the uncompressed version and rename to file back to .sql

But, what if you want to do more than one, or a folder. GZIP is not the tool, however it is used.

You need to TAR the files (which creates one file with the specified files in) and then gzip it:

$> tar -cvzpf compressed_file.tgz foldername

That will compress all files in the foldername folder into a file specified, and it will GZIP it for you.

If you want to just compress a few files, then instead of using the foldername have a list of the files separated by space

$> tar -cvzpf compressed_files.tgz file1 file2 file3

Easy, so to unTar them:

$> tar -zxvf compressed_file.tgz

The above will extract the files to the current folder, if you want them somewhere else:

$> tar -C /foldername -zxvf compressed_file.tgz

The above will extract the files to the /foldername folder.

Hope that helps a little.

 

Preventing pesky consoles. (forgetful developers)

I always end up leaving debug code in my pages for one reason or another. One of which is console.log() for the firebug extension. Now, if you leave this and a user opens your page on IE or something like that, they will get an error.

Put the following code into a JS file, like your base file. And it will check for the existence of console and avoid the problem.

if (!window.console) {
    window.console = {};
    window.console.log = function() {};
}

Zend Console – get values passed

When using a CLI script with options, I like to use the Zend_Console.

If my options are as follows:

$console = new Zend_Console_Getopt(
    array(
        'i-s'  => 'test option 1',
        'e'    => 'test option 2')
    );

For instance,

> php myScript.php -i OPTION

To get the “OPTION” string for the -i parameter

$arg = $console->getOption('i');

$arg will now contain the passed option, but if none was passed, it will be NULL

When outlining the available options that can be used with your script, you use the following syntax:

long|short  => description;
test|t => 'Test the script';

You can use,

> php myScript.php -t

or

> php myScript.php --test

to run the -test parameter. You can also pass values, such as an integer or a string:

'test|t=i' => 'Option with required integer parameter';
'test|t-s' => 'Option with optional string parameter'

For more information, refer to the Zend Documentation : http://framework.zend.com/manual/en/zend.console.getopt.introduction.html