Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Monday, 1 April 2013

Shell Special variables and TEST flags

April 01, 2013 Posted by Dinesh , No comments


Here are some shell special variables:


Special Shell Variables:

VariableMeaning
$0Filename of script
$1Positional parameter #1
$2 - $9Positional parameters #2 - #9
${10}Positional parameter #10
$#Number of positional parameters
"$*"All the positional parameters (as a single word) *
"$@"All the positional parameters (as separate strings)
${#*}Number of positional parameters
${#@}Number of positional parameters
$?Return value
$$Process ID (PID) of script
$-Flags passed to script (using set)
$_Last argument of previous command
$!Process ID (PID) of last job run in background





TEST Operators: Files


OperatorTests Whether-----OperatorTests Whether
-eFile exists-sFile is not zero size
-fFile is a regular file
-dFile is a directory-rFile has read permission
-hFile is a symbolic link-wFile has write permission
-LFile is a symbolic link-xFile has execute permission
-bFile is a block device
-cFile is a character device-gsgid flag set
-pFile is a pipe-usuid flag set
-SFile is a socket-k"sticky bit" set
-tFile is associated with a terminal
-NFile modified since it was last readF1 -nt F2File F1 is newer than F2 *
-OYou own the fileF1 -ot F2File F1 is older than F2 *
-GGroup id of file same as yoursF1 -ef F2Files F1 and F2 are hard links to the same file *
!NOT (inverts sense of above tests)
* Binary operator (requires two operands).


Parameters/String Operations on shell (Bash) without using any third party tools

April 01, 2013 Posted by Dinesh , No comments


Parameter Substitution and Expansion:

ExpressionMeaning
${var}Value of var (same as $var)
${var-DEFAULT}If var not set, evaluate expression as $DEFAULT *
${var:-DEFAULT}If var not set or is empty, evaluate expression as $DEFAULT *
${var=DEFAULT}If var not set, evaluate expression as $DEFAULT * (value is assigned to var)
${var:=DEFAULT}If var not set, or is empty, evaluate expression as $DEFAULT * (value is assigned to var)
${var+OTHER}If var set, evaluate expression as $OTHER, otherwise as null string
${var:+OTHER}If var set, evaluate expression as $OTHER, otherwise as null string
${var?ERR_MSG}If var not set, print $ERR_MSG and abort script with an exit status of 1.*
${var:?ERR_MSG}If var not set, or is empty, print $ERR_MSG and abort script with an exit status of 1.*
${!varprefix*}Matches all previously declared variables beginning with varprefix
${!varprefix@}Matches all previously declared variables beginning with varprefix



* If var is set, evaluate the expression as $var with no side-effects.

String Operations:


ExpressionMeaning
${#string}Length of $string
${string:position}Extract substring from $string at $position
${string:position:length}Extract $length characters substring from $string at $position [zero-indexed, first character is at position 0]
${string#substring}Strip shortest match of $substring from front of $string
${string##substring}Strip longest match of $substring from front of $string
${string%substring}Strip shortest match of $substring from back of $string
${string%%substring}Strip longest match of $substring from back of $string
${string/substring/replacement}Replace first match of $substring with $replacement
${string//substring/replacement}Replace all matches of $substring with $replacement
${string/#substring/replacement}If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement}If $substring matches back end of $string, substitute $replacement for $substring
expr match "$string" '$substring'Length of matching $substring* at beginning of $string
expr "$string" : '$substring'Length of matching $substring* at beginning of $string
expr index "$string" $substringNumerical position in $string of first character in $substring* that matches [0 if no match, first character counts as position 1]
expr substr $string $position $lengthExtract $length characters from $string starting at $position [0 if no match, first character counts as position 1]
expr match "$string" '\($substring\)'Extract $substring*, searching from beginning of $string
expr "$string" : '\($substring\)'Extract $substring* , searching from beginning of $string
expr match "$string" '.*\($substring\)'Extract $substring*, searching from end of $string
expr "$string" : '.*\($substring\)'Extract $substring*, searching from end of $string


* Where $substring is a Regular Expression.

Refer http://itsjustsosimple.blogspot.in/2013/02/all-about-variablesparameters-in-shell.htmlfor few examples.