Thursday, October 27, 2011

Standard streams shift command

Bash script

 "$#" will expand to the number of arguments passed to the script.

Ref: http://www.ibm.com/developerworks/linux/library/l-bash2/index.html
http://www.ibm.com/developerworks/linux/library/l-bash/index.html



The shift Command

The shift command moves the current values stored in the positional parameters (command line args) to the left one position. For example, if the values of the current positional parameters are:$1 = -f $2 = foo $3 = bar
and you executed the shift command the resulting positional parameters would be as follows:
$1 = foo $2 = bar

Ref:  http://www.freeos.com/guides/lsst/ch04sec14.html



What does those mean? 

2>&1
>&2
On the unix command line, each command can print to stdout (standard output) or stderr (standard error). By convention, error messages go to stderr, and normal messages go to stdout. You usually connect stdout to the stdin (standard input) of another process. Having a long pipe of commands and this stderr/stdout convention means that the error messages from one command don't go polluting the input to the next command. It also means that you can see the error messages of the commands, since stderr is shown on your terminal (if they were printed to stdout, then you would not see the error messages, since they would be sent to the input of another command).
When writing your own little scripts, it's a good idea to print your error messages to stderr. The usual way to print in a bash script is to use the echo shell builtin command. You can "echo" to stderr like this:
echo "Your error message here" >&2
This is a normal echo (which goes to stdout), however the >&2 (which is shorthand for 1>&2), means 'mix the stdout to the stderr'. 1 is stdout, and 2 is stderr here.

Ref: http://www.kindle-maps.com/blog/how-to-echo-to-stderr.html
http://en.wikipedia.org/wiki/Standard_streams
http://en.wikipedia.org/wiki/Pipeline_%28Unix%29
http://en.wikipedia.org/wiki/File_descriptor
http://www.unix.com/shell-programming-scripting/113138-what-does-code-mean.html

No comments:

Post a Comment