Friday, January 28, 2011

Linux tricks I learned today

I learned some Linux related tricks today:
1 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.

2 Tutorial: The best tips & tricks for bash, explained

3 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'.


4 Advanced Bash-Scripting Guide

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

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

7 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.
"


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

   Early continuation with continue statement

9 Best of Vim Tips

Labels: , , , ,

Monday, January 10, 2011

Some good Linux articles

  1. Using cut – Shellscript string manipulation
  2. Linux find command
  3. Advanced Bash-Scripting Guide - An in-depth exploration of the art of shell scripting (Mendel Cooper)
  4. Linux: Recovering deleted /etc/shadow password file
  5. Mommy, I found it! — 15 Practical Linux Find Command Examples
  6. The Sort Command
  7. vim search and replace
  8. 100 Vim commands every programmer should know
  9. Search for string and get count in VI editor
  10. Linux tip: Bash test and comparison functions - Demystify test, [, [[, ((, and if-then-else

Labels: , , , , ,