refer:
{{:linux: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 =====
* create helloworld.sh
cat > helloworld.sh
#!/bin/bash
strhello="Hello World"
#print variable on a screen
echo $strhello
* chmod to excute and run:
chmod +x helloworld.sh
./helloworld.sh
===== Debug with bash script =====
==== Print command traces before executing command ====
* syntax:
sh -x script [arg1 ...]
bash -x script [arg1 ...]
* turn on/off debug mode:
set +x#turn on
set -x#turn off
* example
bash -x helloworld.sh
+ strhello='Hello World'
+ echo Hello World
Hello World
==== Echo commands before running them ====
* syntax:
sh -v script [arg1 ...]
bash -v script [arg1 ...]
* turn on/off debug mode:
set +v#turn on
set -v#turn off
* example
bash -v helloworld.sh
#!/bin/bash
strhello="Hello World"
#print variable on a screen
echo $strhello
Hello World
===== 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 ===
* create script test.sh with content below:
#!/bin/bash
#Define bash global variable
#This variable is global and can be used anywhere in this bash script
var_test="global variable"
function test {
#This variable is local to bash function only
local var_test="local variable"
echo $var_test
}
echo $var_test
test
# Note the bash global variable did not change
# "local" is bash reserved word
echo $var_test
* run script:
bash test.sh
Output:
global variable
local variable
global variable
==== Passing arguments to the bash script ====
* create script arguments.sh:
#!/bin/bash
echo "use predefined variables to access passed arguments"
echo $1 $2 $3
echo "We can also store arguments from bash command line in special array"
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]}
echo "use \$@ to print out all arguments at once"
echo $@
echo "Number of arguments passed: $#"
* run script:
./function.sh param1 param2 param3
output:
use predefined variables to access passed arguments
param1 param2 param3
We can also store arguments from bash command line in special array
param1 param2 param3
use $@ to print out all arguments at once
param1 param2 param3
Number of arguments passed: 3
==== 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:
* =: is equal to
if [ "$a" = "$b" ]
Caution: Note the whitespace framing the =.
if [ "$a"="$b" ] is not equivalent to the above.
* ==: is equal to
if [ "$a" == "$b" ]
This is a synonym for =
* !=: is not equal to
if [ "$a" != "$b" ]
This operator uses pattern matching within a [[ ... ]] construct.
* <: is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
* >: is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
* -z: string is null, that is, has zero length
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.
* -n: string is not null
==== 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:
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 ====
* Init the array and access the elements of array:
arr=( zero one two three four five )
echo ${arr[0]}
echo ${arr[1]}
echo $arr[0]
echo $arr[1]
output:
zero
one
zero[0]
zero[1]
* Assign the value for array:
arr=( zero one two three four five )
echo "before update:"
echo ${arr[0]}
arr[0]="new zero"
echo "after update:"
echo ${arr[0]}
output:
before update:
zero
after update:
new zero
* Access all elements of array with **while**:
arr=( zero one two three four five )
element_count=${#arr[@]}
index=0
while [ "$index" -lt "$element_count" ]
do
echo ${arr[$index]}
let "index = $index + 1"
# Or: ((index++))
done
* Access all elements of array with **for**:
arr=( zero one two three four five )
element_count=${#arr[@]}
let "element_count = $element_count - 1"
for index in `seq 0 $element_count`
do
echo ${arr[$index]}
done
* Access all elements of array with **for**:
arr=( zero one two three four five )
element_count=${#arr[@]}
for (( i = 0 ; i < $element_count ; i++ ))
do
echo "Element [$i]: ${arr[$i]}"
done
output:
Element [0]: zero
Element [1]: one
Element [2]: two
Element [3]: three
Element [4]: four
Element [5]: five
===== Custom APIs =====
==== Check file exists ====
if [ -f filename ]; then
echo "file exists"
else
echo "file not exists"
fi;
==== Find files that contain the text ====
* Simple
grep -rnw . -e "Begin Facebook Code"
* Custom find with regular expression
grep -rnw . -e 'mongo.*'
==== 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
* String length: **${#string}**:
str="First String"; echo ${#str}
output:
12
* Replace String:${string//substring/replacement}
Example:
str="First String"; echo ${str//First/Second}
output:
Second String
==== 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