Friday, January 28, 2011

Linux tricks I learned today

I learned some Linux related tricks today:
Tutorial: Conditions in bash scripting (if statements)
This tutorial probably is one of the best and thorough explanation of IF conditions syntaxes I can find so far.

Tutorial: The best tips & tricks for bash, explained

Manipulating Strings
I was looking for ways to replace strings in a bash script, and found out this article is really helpful.


Substring Replacement
${string/substring/replacement}
Replace first match of $substring with $replacement.
${string//substring/replacement}
Replace all matches of $substring with $replacement.
   1 stringZ=abcABC123ABCabc
   2 
   3 echo ${stringZ/abc/xyz}           # xyzABC123ABCabc
   4                                   # Replaces first match of 'abc' with 'xyz'.
   5 
   6 echo ${stringZ//abc/xyz}          # xyzABC123ABCxyz
   7                                   # Replaces all matches of 'abc' with # 'xyz'.
${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.
   1 stringZ=abcABC123ABCabc
   2 
   3 echo ${stringZ/#abc/XYZ}          # XYZABC123ABCabc
   4                                   # Replaces front-end match of 'abc' with 'XYZ'.
   5 
   6 echo ${stringZ/%abc/XYZ}          # abcABC123ABCXYZ
   7                                   # Replaces back-end match of 'abc' with 'XYZ'.


Advanced Bash-Scripting Guide

Highlight all search pattern matches
To turn off search pattern matches highlight, do ":set hls!" in vi command mode.

Turn on or off color syntax highlighting in vi or vim\

Arithmetic Expressions in BASH
"

In bash version 3.2 and later you can (and should) use $(( )) or let for integer arithmetic expressions. The idea of ((...)) construct is similar to [[...]] construct introduced in ksh88 -- provide built in capabilities for arithmetic instead of calling external functions. The (( ))compound command evaluates an arithmetic expression and sets the exit status to 1 if the expression evaluates to 0, or to 0 if the expression evaluates to a non-zero value.  All arithmetical operations are performed  on integers. For example:
 i=$(( i + 1 ))
 let i+=1
or
 i=$(( i++))
 let i++
You must not have spaces around the equals sign, as with any bash variable assignment.
"


Bash For Loop Examples
   Three-expression bash for loops syntax
   Conditional exit with break

   Early continuation with continue statement

Best of Vim Tips

Labels: , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home