Table of Contents

refer: linux_shell_scripting_with_bash_2004.pdf
Advanced Bash-Scripting Guide:http://tldp.org/LDP/abs/html/
Bash script: http://www.linuxconfig.org/Bash_scripting_Tutorial

Linux Bash Shell Script

Simple code

Debug with bash script

Echo commands before running them

Basic syntax

Function and variable in bash script

Declare simple bash variable and print it on the screen:

#!/bin/bash
strhello="Hello World"
#print variable on a screen
echo $strhello

Bash function

#!/bin/bash
# BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER
function function_0 {
  echo "Function 0"
}
function function_1 {
  echo "Function 1: $1"
}
function function_2 {
  echo "Function 2: $1,$2"
}
function function_3 {
  echo "Function 3: $1,$2,$3"
}
function_0
function_1 1
function_2 1 2
function_3 "hello" 1 2

Output:

Function 0
Function 1: 1
Function 2: 1,2
Function 3: hello,1,2

Variable local and global

Passing arguments to the bash script

Logic Operators

! NOT

if [ ! -f $FILENAME ]
then
  ...

&& AND

if [ $condition1 ] && [ $condition2 ]
#  Same as:  if [ $condition1 -a $condition2 ]
#  Returns true if both condition1 and condition2 hold true...
 
if [[ $condition1 && $condition2 ]]    # Also works.
#  Note that && operator not permitted inside brackets

OR

if [ $condition1 ] || [ $condition2 ]
# Same as:  if [ $condition1 -o $condition2 ]
# Returns true if either condition1 or condition2 holds true...
 
if [[ $condition1 || $condition2 ]]    # Also works.
#  Note that || operator not permitted inside brackets
#+ of a [ ... ] construct.

Arithmetic operators

Bitwise operators

comparision operators

Integer comparison

-eq : is equal to

if [ "$a" -eq "$b" ]

-ne : is not equal to

if [ "$a" -ne "$b" ]

-gt : is greater than

if [ "$a" -gt "$b" ]

-ge : is greater than or equal to

if [ "$a" -ge "$b" ]

-lt : is less than

if [ "$a" -lt "$b" ]

-le : is less than or equal to

if [ "$a" -le "$b" ]

< : is less than (within double parentheses)

(("$a" < "$b"))

⇐: is less than or equal to (within double parentheses)

(("$a" <= "$b"))

is greater than (within double parentheses)

(("$a" > "$b"))

is greater than or equal to (within double parentheses)

(("$a" >= "$b"))

String comparison

String Comparisions:

If condition

#!/bin/bash
if [ "aa$1" = "aa" ]; then
   date=`date +%Y_%m_%d`
else
   date=$1
fi
echo $date

Loop with while

loop with while:

<code bash>
while read line; do 
  username=`echo $line | awk '{print $1}'`
  serverid=`echo $line | awk '{print $2}'`
  echo $username,$serverid
  mysql -uzf_9thien -p'.7#:qyJ&$&>bsU83r/#j[=yC' zf_9thien <<< "update fastreg set server_id='$serverid' where username='$username' limit 1;"
done < /tmp/a1.txt

Array

Custom APIs

Check file exists

if [  -f filename ]; then
   echo "file exists"
else
   echo "file not exists"
fi;

Find files that contain the text

Find files that file name length > 29

for i in `ls -1`;do sub_i=(`echo $i|cut -d'-' -f 1`);lensub_i=${#sub_i}; if [ $lensub_i -ge 29 ]; then echo $sub_i;echo $i; fi  done

Manipulating String

refer: http://tldp.org/LDP/abs/html/string-manipulation.html

update array to file

Below is array configs.txt:

c1 = 0,4,5,8,9,12,13,16,17,19,22,21,45,2,3,6,7,10,11,14,15,20,1,23,44,50,26,27,28,29,30,31,32,33,34,35,38,49,47,18,36,37,24,39,40,41,42,43,46,48,25,51
c2 = 51,4,5,8,9,12,13,16,17,25,22,21,45,2,3,6,7,10,11,14,15,20,1,23,44,50,26,27,28,29,30,31,32,33,34,35,38,49,47,18,36,37,24,39,40,0,42,43,46,48,19,41
c3 = 26,27,33,34,8,9,12,13,40,41,0,30,31,2,3,6,7,10,11,14,15,20,21,23,44,50,45,1,28,29,32,19,22,4,5,35,38,49,47,18,36,37,24,39,16,17,42,43,46,48,25,51

update array to files:

index=0;for i in `cat configs.txt | awk '{print $3}'`; do let "index = $index + 1";echo $i > c$index.txt; done