User Tools

Site Tools


linux:bashshell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
linux:bashshell [2016/01/15 04:43] – [variable local and global] adminlinux:bashshell [2021/05/24 15:18] – [Find files that contain the text] admin
Line 4: Line 4:
 Bash script: http://www.linuxconfig.org/Bash_scripting_Tutorial Bash script: http://www.linuxconfig.org/Bash_scripting_Tutorial
 ====== Linux Bash Shell Script ====== ====== Linux Bash Shell Script ======
-===== create simple code =====+===== Simple code =====
   * create helloworld.sh<code bash>   * create helloworld.sh<code bash>
 cat > helloworld.sh cat > helloworld.sh
Line 163: Line 163:
 ==== Bitwise operators ==== ==== Bitwise operators ====
 ==== comparision operators ==== ==== comparision operators ====
-=== integer comparison ===+=== Integer comparison ===
 -eq : is equal to<code bash> -eq : is equal to<code bash>
 if [ "$a" -eq "$b" ] if [ "$a" -eq "$b" ]
Line 194: Line 194:
 (("$a" >= "$b")) (("$a" >= "$b"))
 </code> </code>
-=== string comparison ===+=== String comparison ===
 String Comparisions: String Comparisions:
   * =: is equal to<code bash>   * =: is equal to<code bash>
Line 236: Line 236:
 echo $date echo $date
 </code> </code>
-==== loop with while ====+==== Loop with while ====
 loop with while:<code bash> loop with while:<code bash>
 <code bash> <code bash>
Line 246: Line 246:
 done < /tmp/a1.txt done < /tmp/a1.txt
 </code> </code>
-==== array ====+==== Array ====
   * Init the array and access the elements of array:<code bash>   * Init the array and access the elements of array:<code bash>
 arr=( zero one two three four five ) arr=( zero one two three four five )
Line 315: Line 315:
 </code> </code>
 ==== Find files that contain the text ==== ==== Find files that contain the text ====
 +  * Simple<code bash>
 +grep -rnw . -e "Begin Facebook Code"
 +</code>
 +  * Custom find with regular expression<code bash>
 +grep -rnw . -e 'mongo.*'
 +</code>
 +==== Find files that file name length > 29 ====
 +<code bash>
 +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
 +</code>
 +==== Manipulating String ====
 +refer: http://tldp.org/LDP/abs/html/string-manipulation.html
 +  * String length: **${#string}**:<code bash>
 +str="First String"; echo ${#str}
 +</code>output:<code>
 +12
 +</code>
 +  * Replace String:<code>${string//substring/replacement}</code>Example:<code bash>
 +str="First String"; echo ${str//First/Second}
 +</code>output:<code>
 +Second String
 +</code>
 +==== update array to file ====
 +Below is array configs.txt:<code>
 +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
 +</code>
 +update array to files:
 <code bash> <code bash>
-grep -rnw babyshopvn -e "Begin Facebook Code"+index=0;for i in `cat configs.txt | awk '{print $3}'`; do let "index = $index + 1";echo $i > c$index.txt; done
 </code> </code>
linux/bashshell.txt · Last modified: 2022/10/29 16:15 by 127.0.0.1