Friday, October 12, 2012

Shell script exit status


http://www.thegeekstuff.com/2010/03/bash-shell-exit-status/




In our bash introduction article, we learned that a shell-script file contains list of commands to be executed by the shell interpreter. In this article let us review about shell commands and its internals.
A command is a sequence of words. The first word indicates the command to be executed and remaining words are passed as an arguments, where arguments could be the options or parameters to the command.

Some of the common Unix commands you execute at the command line are shell commands. For example, ls, lpr and grep command.
$ ls -alF

$ lpr filename

$ grep "string" filename

Shell Command Exit Status

The return value of a command is its exit status, or 128 + N if the command is terminated by signal N. Exit status is used to check the result (success/failure) of the execution of the command. If the exit status is zero, then the command is success. If the command is failed the exit status will be non-zero.
Exit ValueExit Status
0 (Zero)Success
Non-zeroFailure
2Incorrect usage
127Command Not found
126Not an executable

$? Shell Variable

The shell variable name $? is a special built-in variable which has the exit status of the last command executed.
  • After the shell function execution, $? returns the exit status of the last command executed in a function.
  • After the shell script execution, $? returns the exit status of the last command executed in the script.

Sample Shell Script that Explains Shell-Command Exit Status

The following exitstatus.sh shell-script shows examples of various shell-command exit status.
$ cat exitstatus.sh
#! /bin/bash

echo -e "Successful execution"
echo -e "====================="
echo "hello world"
# Exit status returns 0, because the above command is a success.
echo "Exit status" $? 

echo -e "Incorrect usage"
echo -e "====================="
ls --option
# Incorrect usage, so exit status will be 2.
echo "Exit status" $? 

echo -e "Command Not found"
echo -e "====================="
bashscript
# Exit status returns 127, because bashscript command not found
echo "Exit status" $? 

echo -e "Command is not an executable"
echo -e "============================="
ls -l execution.sh
./execution.sh
# Exit status returns 126, because its not an executable.
echo "Exit status" $?
Now, execute the above exitstatus.sh to see the various exit statues given by the sample shell script.
$ bash exitstatus.sh
Successful execution
=====================
hello world
Exit status 0
Incorrect usage
=====================
ls: unrecognized option `--option'
Try `ls --help' for more information.
Exit status 2
Command Not found
=====================
exitstaus.sh: line 15: bashscript: command not found
Exit status 127
Command is not an executable
=============================
-rw-r--r-- 1 root root 659 Mar  9 13:36 execution.sh
exitstatus.sh: line 21: ./execution.sh: Permission denied
Exit status 126
Note: Checking the return value of a function or a command is one of the main responsibility of a programmer. This should become your second nature while writing any code.



JSON " for strings, with neat diagrams


http://www.json.org/



http://stackoverflow.com/questions/4162642/python-single-vs-double-quotes-in-json
http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript


which is easier to read? alert("It's game time"); or alert('It\'s game time'); – Ryan Miller Jun 9 '11 at 16:10
33 
How about this Ryan? alert("It's \"game\" time."); or alert('It\'s "game" time.');? – Francisc Oct 11 '11 at 18:37
3 
If single quotes always used and occasionally double quotes where the literal contains single quote, then we will have to type far less shift buttons and our left little finger will give us blessings. But yes, as @arne said, for JSON double quote should be used. – iSid Oct 31 '11 at 15:46
1 
Single quoting is easier to do when you're on a european keyboard (double quote is Shift+2 which isn't as sweet as tapping a single key conveniently by your right pinky). – Arne Apr 25 at 20:46
1 
@Arne There is no such a thing as an "European keyboard". E.g. the German keyboard requires shift for both types of quotes. (But single quotes are easier.) – ANeves Jun 18 at 15:53


MySQL COMMANDS and EXPRESSIONS




http://dev.mysql.com/doc/refman/5.0/en/expressions.html
http://dev.mysql.com/doc/refman/5.0/en/functions.html





Chapter 12. Functions and Operators

Expressions can be used at several points in SQL statements, such as in the ORDER BY or HAVING clauses of SELECTstatements, in the WHERE clause of a SELECT, DELETE, orUPDATE statement, or in SET statements. Expressions can be written using literal values, column values, NULL, built-in functions, stored functions, user-defined functions, and operators. This chapter describes the functions and operators that are permitted for writing expressions in MySQL. Instructions for writing stored functions and user-defined functions are given in Section 18.2, “Using Stored Routines (Procedures and Functions)”, and Section 21.2, “Adding New Functions to MySQL”. See Section 9.2.3, “Function Name Parsing and Resolution”, for the rules describing how the server interprets references to different kinds of functions.
An expression that contains NULL always produces a NULLvalue unless otherwise indicated in the documentation for a particular function or operator.
Note
By default, there must be no whitespace between a function name and the parenthesis following it. This helps the MySQL parser distinguish between function calls and references to tables or columns that happen to have the same name as a function. However, spaces around function arguments are permitted.







EXPRESSIONS SYNTAX:


expr:
    expr OR expr
  | expr || expr
  | expr XOR expr
  | expr AND expr
  | expr && expr
  | NOT expr
  | ! expr
  | boolean_primary IS [NOT] {TRUE | FALSE | UNKNOWN}
  | boolean_primary

boolean_primary:
    boolean_primary IS [NOT] NULL
  | boolean_primary <=> predicate
  | boolean_primary comparison_operator predicate
  | boolean_primary comparison_operator {ALL | ANY} (subquery)
  | predicate

comparison_operator: = | >= | > | <= | < | <> | !=

predicate:
    bit_expr [NOT] IN (subquery)
  | bit_expr [NOT] IN (expr [, expr] ...)
  | bit_expr [NOT] BETWEEN bit_expr AND predicate
  | bit_expr SOUNDS LIKE bit_expr
  | bit_expr [NOT] LIKE simple_expr [ESCAPE simple_expr]
  | bit_expr [NOT] REGEXP bit_expr
  | bit_expr

bit_expr:
    bit_expr | bit_expr
  | bit_expr & bit_expr
  | bit_expr << bit_expr
  | bit_expr >> bit_expr
  | bit_expr + bit_expr
  | bit_expr - bit_expr
  | bit_expr * bit_expr
  | bit_expr / bit_expr
  | bit_expr DIV bit_expr
  | bit_expr MOD bit_expr
  | bit_expr % bit_expr
  | bit_expr ^ bit_expr
  | bit_expr + interval_expr
  | bit_expr - interval_expr
  | simple_expr

simple_expr:
    literal
  | identifier
  | function_call
  | simple_expr COLLATE collation_name
  | param_marker
  | variable
  | simple_expr || simple_expr
  | + simple_expr
  | - simple_expr
  | ~ simple_expr
  | ! simple_expr
  | BINARY simple_expr
  | (expr [, expr] ...)
  | ROW (expr, expr [, expr] ...)
  | (subquery)
  | EXISTS (subquery)
  | {identifier expr}
  | match_expr
  | case_expr
  | interval_expr
Notes:
For literal value syntax, see Section 9.1, “Literal Values”.
For identifier syntax, see Section 9.2, “Schema Object Names”.
Variables can be user variables, system variables, or stored program local variables or parameters:
param_marker is '?' as used in prepared statements for placeholders. See Section 13.5.1, “PREPARESyntax”.
(subquery) indicates a subquery that returns a single value; that is, a scalar subquery. SeeSection 13.2.9.1, “The Subquery as Scalar Operand”.
{identifier expr} is ODBC escape syntax and is accepted for ODBC compatibility. The value isexpr. The curly braces in the syntax should be written literally; they are not metasyntax as used elsewhere in syntax descriptions.
match_expr indicates a MATCH expression. See Section 12.9, “Full-Text Search Functions”.
case_expr indicates a CASE expression. See Section 12.4, “Control Flow Functions”.
interval_expr represents a time interval. The syntax is INTERVAL expr unit, where unit is a specifier such as HOUR, DAY, or WEEK. For the full list of unit specifiers, see the description of theDATE_ADD() function in Section 12.7, “Date and Time Functions”.
The meaning of some operators depends on the SQL mode:
  • By default, || is a logical OR operator. With PIPES_AS_CONCAT enabled, || is string concatenation, with a precedence between ^ and the unary operators.
  • By default, ! has a higher precedence than NOT as of MySQL 5.0.2. For earlier versions, or from 5.0.2 on with HIGH_NOT_PRECEDENCE enabled, ! and NOT have the same precedence.