[Repost] /dev/null

Technically /dev/null is defined as a null device. Anything written to it is lost forever. If you read the wiki description of /dev/null, you will get to know lots of jargons/metaphors written/referred by UNIX techies a lot. In this article, we will concentrate on the importance of /dev/null, why it is there, and how to use it.

Importance

When things written to it are lost forever, then why do we need it? This is a valid questions, but we need /dev/null/ to lose information. That’s correct, to explain let’s take an example:

Example 1:

Suppose, you want to list all files of directory /var/tmp/ having word “foo” in its content. To achieve the task, we will write something as:

]$ grep -l foo /var/tmp/*
  /var/tmp/storagedata
  /var/tmp/storagedata.0
  grep: /var/tmp/td.log: Permission denied

So we got two files having word “foo”; but we also an annoying error message which was part of STDERR. We were not interested in any error message, and we wanted to see only those files which I am permitted to read. So, how do I get rid of this error message? Luckily newer version of grep provides a silent option “-s”, using which we can get rid of this message. But what if I am working on a traditional system, having traditional grep? What if the system command is not having the silent option? The solution is given below:

Capturing STDOUT only

  1. Using newer grep:
       ]$ grep -ls foo /var/tmp/*
            /var/tmp/storagedata
            /var/tmp/storagedata.0
  2. Using traditional grep:
       ]$ grep -l foo /var/tmp/* 2>/dev/null
            /var/tmp/storagedata
            /var/tmp/storagedata.0
  3. In general using any system command :
       ]$ cmd 2>/dev/null

Most of you should have captured the importance of /dev/null by now. In simple words, by using “2>/dev/null”, we are asking the shell to redirect the STDERR to /dev/null. This is very useful when we execute system command through a program and expect to get only the STDOUT.

There may also be a case, when we are only interested to see the error message (STDERR), and are not at all interested in STDOUT. To get STDERR, and to discard the STDOUT we will redirect the STDERR to STDOUT and will redirect the STDOUT to /dev/null; as given below:

Capturing STDERR only

]$ cmd 2>&1 1>/dev/null

You may be thinking, that if I am redirecting STDERR to STDOUT which in-turn is redirected to /dev/null, so there should not be any output. But that is not the case, and you get the error messages.

Read More

Tags: unix command

Understanding callback

What’s callback?

In computer programming, a callback is a reference to a piece of executable code that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer. (source:wikipedia)

My understanding:

TBD

Callback in android:

Example in C:

#include <stdio.h>
#include <stdlib.h>
 
/* The calling function takes a single callback as a parameter. */
void PrintTwoNumbers(int (*numberSource)(void)) {
    printf("%d and %d\n", numberSource(), numberSource());
}
 
/* A possible callback */
int overNineThousand() {
    return (rand() % 1000) + 9001;
}
 
/* Another possible callback. */
int meaningOfLife() {
    return 42;
}
 
/* Here we call PrintTwoNumbers() with three different callbacks. */
int main() {
    PrintTwoNumbers(&rand);
    PrintTwoNumbers(&overNineThousand);
    PrintTwoNumbers(&meaningOfLife);
    return 0;
}

android development notes

An Intent is an object that provides runtime binding between separate components (such as two activities). The Intent represents an app’s “intent to do something.” You can use intents for a wide variety of tasks, but most often they’re used to start another activity.

Eclipse shortcuts

Maximize or unmaximize the current tab: ctrl+M

Delete row: ctrl+D (command+D in Mac)

Go to the start/end of the row: cmd+ <- or ->

Show all the files in editor: ctrl+E (command+E in Mac)

Move between tabs:ctrl+f6 (command+[ or ] in Mac)

Correct indenation:ctrl+i

Import missing classes: ctrl+shift+O (Cmd+Shift+O on Mac).

Search: ctrl + H

Source: http://eclipse.dzone.com/news/effective-eclipse-shortcut-key

Tags: shell study

凯撒沙拉的由来

凯撒·卡狄尼(Caesar Cardini)在1924年发明了凯撒沙拉。卡狄尼是个墨西哥蒂华纳(Tijuana)的意式餐馆老板兼主厨,他住在圣迭戈,但为了避开禁酒令(Prohibition)而在蒂华纳工作。关于沙拉发明的故事有许多种版本,最常见的说法是,在有次七月四日那天,餐厅耗尽了厨房内所有的东西,因此卡狄尼用他仅剩的食材,并以他的天赋异禀,制作出了凯撒沙拉。另一个说法是说,沙拉是为了一群好莱坞明星在周末派对后所制作出的。在现今的墨西哥蒂华纳,凯撒饭店(Hotel Caesar)已经不再经营餐馆,但重新装修的饭店旁边的酒吧与餐馆近来也按照传统开始提供凯撒沙拉。

一份凯撒沙拉通常由下列食材料理制作:

Source: Wikipedia

Tags: food

"某女校晨間練跑的女子隊員們,擦身而過時會大聲向我招呼早安。那樣的時候,我想人生和世界都還不太壞。"

— 村上春樹 《慢跑路程的極致》

Javascript Object

In general, if you want to add a method to a class such that all members of the class can use it, we use the following syntax to extend the prototype:

className.prototype.newMethod =

function() {
   statements;
};


Moving on with javascript reviewing.

Set the Penguin class’s prototype to a new instance of Animal by adding this line after you make the constructor:

Penguin.prototype = new Animal();

This means that Penguininherits properties and methods from Animal.

A class with constructor:

function Person(name, age) {

this.name = name;

this.age = age;

}

Person.prototype.sayHi(){

     method body

}

Inside Object:

var obj = {

name: “Qian”,

age: 25,

sayHi : function(param1,param2){

method body

},

sayYo : function(param1){

}

Privatize a variable => don’t using “this.xxx” but using “var xxx”

Privatize a method => dont using “this.xxxx = function(){}” but using “var xxxx =”

to loop in a object and print values => for (var prop in obj) {console.log(“obj[prop]”)

To see what type Object.prototype is, we should use typeof Object.prototype.

The property we want to check for is actually "hasOwnProperty", so line 6 should look like: Object.prototype.
hasOwnProperty
("hasOwnProperty")

Hybrid Mobile App

What’s a hybrid mobile app? 

Comparing to Native app, mobile web app, what’s the advantage of hybrid mobile app? This article is worth reading too.

Some existing solutions: PhoneGap, Titanium, Kirin

PhoneGap Setup

Plugin Development Guide

A Cordova plugin bridges a bit of functionality between the WebView powering a Cordova application and the native platform the Cordova application is running on. Plugins are composed of a single JavaScript interface used across all platforms, and native implementations following platform-specific Plugin interfaces that the JavaScript will call into. It should be noted that all of the core Cordova APIs are implemented using this exact architecture.


Tags: phonegap study