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
cat > helloworld.sh #!/bin/bash strhello="Hello World" #print variable on a screen echo $strhello
chmod +x helloworld.sh ./helloworld.sh
sh -x script [arg1 ...] bash -x script [arg1 ...]
set +x#turn on set -x#turn off
bash -x helloworld.sh + strhello='Hello World' + echo Hello World Hello World
sh -v script [arg1 ...] bash -v script [arg1 ...]
set +v#turn on set -v#turn off * example<code bash> bash -v helloworld.sh #!/bin/bash strhello="Hello World" #print variable on a screen echo $strhello Hello World
Declare simple bash variable and print it on the screen:
#!/bin/bash strhello="Hello World" #print variable on a screen echo $strhello
#!/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
#!/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
bash test.sh
Output:
global variable local variable global variable
#!/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: $#"
./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
! 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.
-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 Comparisions:
if [ "$a" = "$b" ]
Caution: Note the whitespace framing the =.
if [ "$a"="$b" ] is not equivalent to the above.
if [ "$a" == "$b" ]
This is a synonym for =
if [ "$a" != "$b" ]
This operator uses pattern matching within a ... construct.
if [[ "$a" < "$b" ]] if [ "$a" \< "$b" ]
Note that the “<” needs to be escaped within a [ ] construct.
if [[ "$a" > "$b" ]] if [ "$a" \> "$b" ]
Note that the “>” needs to be escaped within a [ ] construct.
String='' # Zero-length ("null") string variable. if [ -z "$String" ] then echo "\$String is null." else echo "\$String is NOT null." fi # $String is null.
#!/bin/bash if [ "aa$1" = "aa" ]; then date=`date +%Y_%m_%d` else date=$1 fi echo $date
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
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]
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
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
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
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
if [ -f filename ]; then echo "file exists" else echo "file not exists" fi;
grep -rnw . -e "Begin Facebook Code"
grep -rnw . -e 'mongo.*'
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
refer: http://tldp.org/LDP/abs/html/string-manipulation.html
str="First String"; echo ${#str}
output:
12
${string//substring/replacement}
Example:
str="First String"; echo ${str//First/Second}
output:
Second String
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