{"id":3549,"date":"2014-09-11T10:27:07","date_gmt":"2014-09-11T02:27:07","guid":{"rendered":"http:\/\/rmohan.com\/?p=3549"},"modified":"2014-09-11T10:27:07","modified_gmt":"2014-09-11T02:27:07","slug":"48-tips-for-bash","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3549","title":{"rendered":"48 TIPS FOR BASH"},"content":{"rendered":"<p>We collected a bunch of shell scripting skills, I said, \u00a0 I write a blog mainly to make some study notes, to facilitate their access to,<\/p>\n<p>so I would come up with such an article, there is no incomprehensible. About the origin of these skills, eh,<br \/>\nI forgot, it may come from theunixschool, commandlinefu, cool ground network and igigo.net, of course, there are some of my own ideas and experiences,<br \/>\nwho cares, into my mind that I of the.<br \/>\n0. shell debugging<br \/>\nCopy the code code below: sh -x somefile.sh<br \/>\nPlus set + x set-x in somefile.sh file<br \/>\n1. &amp;&amp; || simplified if else<br \/>\nCopy the code code below: gzip -t a.tar.gz<\/p>\n<p>gzip -t a.tar.gz<br \/>\nif [[ 0 == $? ]]; then<br \/>\necho &#8220;good zip&#8221;<br \/>\nelse<br \/>\necho &#8220;bad zip&#8221;<br \/>\nfi<\/p>\n<p>Can be simplified to: Copy the code code below:<\/p>\n<p>gzip -t a.tar.gz &amp;&amp; echo &#8220;good zip&#8221; || echo &#8220;bad zip&#8221;<\/p>\n<p>2 to determine the file is not empty<\/p>\n<p>Copy the code following code:<\/p>\n<p>if [[ -s $file ]]; then<br \/>\necho &#8220;not empty&#8221;<br \/>\nfi<\/p>\n<p>3 Get File Size<br \/>\nCopy the code code below:<\/p>\n<p>stat -c %s $file<br \/>\nstat &#8211;printf=&#8217;%s\\n&#8217; $file<br \/>\nwc -c $file<\/p>\n<p>4 string replacement<br \/>\nCopy the code following code:<\/p>\n<p>${string\/\/pattern\/replacement}<br \/>\na=&#8217;a,b,c&#8217;<br \/>\necho ${a\/\/,\/ \/}<\/p>\n<p>5. Contains substring?<br \/>\nstring=&#8221;My string&#8221;<br \/>\nif [[ $string == *My* ]]; then<br \/>\necho &#8220;It&#8217;s there!&#8221;<br \/>\nfi<\/p>\n<p>6. rsync backup<br \/>\nCopy the code code below:<br \/>\nrsync -r -t -v \/source_folder \/destination_folder<br \/>\nrsync -r -t -v \/source_folder [user@host:\/destination_folder<\/p>\n<p>7 batch rename files<\/p>\n<p>Txt file with all .bak suffix:<br \/>\nCopy the code code below:<br \/>\nrename &#8216;.txt&#8217; &#8216;.txt.bak&#8217; *.txt<br \/>\nRemove all bak suffix:<br \/>\nrename &#8216;*.bak&#8217; &#8221; *.bak<br \/>\nAll the spaces into underscores:<br \/>\nCopy the code code below:<br \/>\nfind path -type f -exec rename &#8216;s\/ \/_\/g&#8217; {} \\;<\/p>\n<p>The file names are changed to uppercase:<br \/>\nCopy the code code below:<\/p>\n<p>find path -type f -exec rename &#8216;y\/a-z\/A-Z\/&#8217; {} \\;<\/p>\n<p>8. for \/ while loop<\/p>\n<p>for ((i=0; i &lt; 10; i++)); do echo $i; done<br \/>\nfor line in $(cat a.txt); do echo $line; done<br \/>\nfor f in *.txt; do echo $f; done<br \/>\nwhile read line ; do echo $line; done &lt; a.txt<br \/>\ncat a.txt | while read line; do echo $line; done<br \/>\n9 delete blank lines<\/p>\n<p>cat a.txt | sed -e &#8216;\/^$\/d&#8217;<br \/>\n(echo &#8220;abc&#8221;; echo &#8220;&#8221;; echo &#8220;ddd&#8221;;) | awk &#8216;{if (0 != NF) print $0;}&#8217;<\/p>\n<p>10. compare file modification time<\/p>\n<p>[[ file1.txt -nt file2.txt ]] &amp;&amp; echo true || echo false<br \/>\n[[ file1.txt -ot file2.txt ]] &amp;&amp; echo true || echo false<\/p>\n<p>11. achieve Dictionary structure<br \/>\nCopy the code code below: hput () {<br \/>\neval &#8220;hkey_ $ 1&#8221; = &#8220;$ 2&#8221;<br \/>\n}<br \/>\nhget () {<br \/>\neval echo &#8216;$ {&#8216; &#8220;hkey_ $ 1&#8221; &#8216;}&#8217;<br \/>\n}<br \/>\n$ Hput k1 aaa<br \/>\n$ Hget k1<br \/>\naaa<\/p>\n<p>12 removed from the second row<\/p>\n<p>Copy the code the code below:<br \/>\n$echo &#8216;a b c d e f&#8217; | cut -d &#8216; &#8216; -f1,3-<br \/>\n$a c d e f<br \/>\n13 Save the stderr output to a variable<br \/>\nCopy the code the code below:<\/p>\n<p>$ a=$( (echo &#8216;out&#8217;; echo &#8216;error&#8217; 1&gt;&amp;2) 2&gt;&amp;1 1&gt;\/dev\/null)<br \/>\n$ echo $a<br \/>\nerror<\/p>\n<p>14)<br \/>\n3 lines deleted before 14<\/p>\n<p>$cat a.txt | sed 1,3d<\/p>\n<p>15. read multiple domains into a variable<br \/>\nCopy the code the code below:<br \/>\nread a b c &lt;&lt;&lt; &#8220;xxx yyy zzz&#8221;<br \/>\n16. iterate<\/p>\n<p>Copy the code code below:<\/p>\n<p>array=( one two three )<br \/>\nfor i in ${array[@]}<br \/>\ndo<br \/>\necho $i<br \/>\ndone<br \/>\n17 view directory size<\/p>\n<p>Copy the code following code:<\/p>\n<p>du \u2013sh ~\/apps<\/p>\n<p>18 View CPU information<br \/>\nCopy the code following code:<\/p>\n<p>cat \/proc\/cpuinfo<br \/>\n19. date<\/p>\n<p>$ date +%Y-%m-%d<br \/>\n2012-12-24<br \/>\n$ date +%Y-%m-%d \u2013date \u2018-1 day&#8217;<br \/>\n2012-12-23<br \/>\n$ date +%Y-m-%d \u2013date \u2018Dec 25&#8242;<br \/>\n2011-12-25<br \/>\n$ date +%Y-m-%d \u2013date \u2018Dec 25 \u2013 10 days&#8217;<br \/>\n2011-12-15<br \/>\n20. get the path and file name<br \/>\nCopy the code the code below:<\/p>\n<p>$ dirname \u2018\/home\/lalor\/a.txt&#8217;<br \/>\n\/home\/lalor<br \/>\n$ basename \u2018\/home\/lalor\/a.txt&#8217;<br \/>\na.txt<\/p>\n<p>21. union and intersection<br \/>\ncomm can be used to seek union, intersection, difference, assuming that there are now two documents a and b,<br \/>\nwhich reads as follows:<\/p>\n<p>Copy the code following code:<\/p>\n<p>$cat a<br \/>\n1<br \/>\n3<br \/>\n5<\/p>\n<p>$cat b<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n$ cat b<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<\/p>\n<p>$ comm a b<br \/>\n1<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<\/p>\n<p>$ comm -1 -2 a b # intersection<br \/>\n3<br \/>\n5<\/p>\n<p>$ comm a b | sed &#8216;s\/\\t\/\/g&#8217; # and set<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<\/p>\n<p>$ comm -1 -3 a b | sed &#8216;s\/\\t\/\/g&#8217; # b-a<br \/>\n4<br \/>\n6<br \/>\n7<br \/>\n22. awk complex delimiter<\/p>\n<p>Multi-character as the delimiter<\/p>\n<p>Copy the code following code:<\/p>\n<p>Multiple delimiters 1<\/p>\n<p>Copy the code the code below:<\/p>\n<p>$ echo &#8220;a||b||c||d&#8221; | awk -F &#8216;[|][|]&#8217; &#8216;{print $3}&#8217;<br \/>\nc<\/p>\n<p>Multiple delimiters 2<br \/>\nCopy the code following code:<\/p>\n<p>$echo &#8220;a||b,#c d&#8221; | awk -F &#8216;[| ,#]+&#8217; &#8216;{print $4}&#8217;<br \/>\nd<\/p>\n<p>$echo &#8220;a||b##c|#d&#8221; | awk -F &#8216;([|][|])|([#][#])&#8217; &#8216;{print $NF}&#8217;<br \/>\nc|#d<\/p>\n<p>23 generates a random number<\/p>\n<p>Copy the code the code below:<br \/>\necho $ RANDOM<\/p>\n<p>24. The mode split file<\/p>\n<p>Copy the code code below:<br \/>\ncsplit server.log \/PATTERN\/ -n 2 -s {*} -f server_result -b &#8220;%02d.log&#8221; -z<\/p>\n<p>\/ PATTERN \/ used to match a row, the segmentation process starts<br \/>\n{*} Based on the matching, segmentation is repeated<br \/>\n-s silent mode<br \/>\nfilename suffix after -n split, the number of digits<br \/>\nfile name prefix -f divided<br \/>\nSpecify suffix format -b<\/p>\n<p>25. get the file name or extension<br \/>\nCopy the code following code: var = hack.fun.book.txt<br \/>\nvar=hack.fun.book.txt<br \/>\necho ${var%.*}<br \/>\nhack.fun.book<br \/>\necho ${var%%.*}<br \/>\nhack<br \/>\necho ${var#.*}<br \/>\nfun.book.txt<br \/>\necho ${var##.*}<br \/>\ntxt<\/p>\n<p>26. to the root account to perform on a command.<\/p>\n<p>Copy the code the code below:<br \/>\n$ sudo !!<\/p>\n<p>Among them: * !! refers to the previous command * The last parameter $ on a command parameter * on * all * of a command:!!! First three parameters of a command on 3<br \/>\nFor example:<br \/>\n$ls \/tmp\/somedir<br \/>\nls: cannot access \/tmp\/somedir: No such file or directory<\/p>\n<p>$mkdir !$<br \/>\nmkdir \/tmp\/somedir<br \/>\n27. use python to build a simple Web server via http:\/\/$HOSTNAME:8000 visit.<\/p>\n<p>Copy the code the code below:<br \/>\npython -m SimpleHTTPServer<\/p>\n<p>28. in Vim without permission to save the file to edit.<\/p>\n<p>Copy the code code below :w !sudo tee %<br \/>\n29. on a command replaces foo bar, and executed.<br \/>\nCopy the code code below: ^foo^bar<\/p>\n<p>30. quickly backup or copy files.<br \/>\nCopy the code code below: cp filename{,.bak}<br \/>\n31. ssh keys will be copied to the user @ host to enable SSH login without a password.<br \/>\nCopy the code code below: $ssh-copy-id user@host<br \/>\n32. the linux desktop recording video.<br \/>\nCopy the code code below: ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq \/tmp\/out.mpg<br \/>\n33. man Magical<br \/>\nCopy the code code below:<br \/>\nman ascii<br \/>\nman test<\/p>\n<p>34 on the vim to edit a command<\/p>\n<p>Copy the code code below: fc<br \/>\n35. delete 0 byte files or junk files<br \/>\nCopy the code code below:<\/p>\n<p>find . -type f -size 0 -delete<br \/>\nfind . -type f -exec rm -rf {} \\;<br \/>\nfind . -type f -name &#8220;a.out&#8221; -exec rm -rf {} \\;<br \/>\nfind . type f -name &#8220;a.out&#8221; -delete<br \/>\nfind . type f -name &#8220;*.txt&#8221; -print0 | xargs -0 rm -f<br \/>\n36 When writing SHELL display multiple lines of information<\/p>\n<p>Copy the code the code below:<\/p>\n<p>cat &lt;&lt; EOF<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n| === Welcome to Tunoff services === |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\nEOF<\/p>\n<p>Note that when you specify the terminator, it must be the only contents of the line, and the line must begin with this character.<br \/>\n37 How to build mysql soft link<\/p>\n<p>Copy the code code below:<\/p>\n<p>cd \/usr\/local\/mysql\/bin<br \/>\nfor i in *<br \/>\ndo ln \/usr\/local\/mysql\/bin\/$i \/usr\/bin\/$i<br \/>\ndone<\/p>\n<p>38 to obtain an IP address:<br \/>\nifconfig eth0 |grep &#8220;inet addr:&#8221; |awk &#8216;{print $2}&#8217;|cut -c 6-<\/p>\n<p>39 the number of open files<br \/>\nCopy the code code below: lsof<br \/>\n40. Clear zombie process<br \/>\nCopy the code code below:<\/p>\n<p>ps -eal | awk &#8216;{ if ($2 == &#8220;Z&#8221;){ print $4}}&#8217; | kill -9<br \/>\n41. print only lines<\/p>\n<p>Copy the code code below:<br \/>\nawk &#8216;!a[$0]++&#8217; file<br \/>\n42 print odd line<br \/>\nCopy the code code below:<br \/>\nawk &#8216;i=!i&#8217; file<br \/>\nawk &#8216;NR%2&#8217; file<\/p>\n<p>43. print matching lines after a row<br \/>\nCopy the code code below:<br \/>\nseq 10 | awk &#8216;\/4\/{f=4};&#8211;f==0{print;exit}&#8217;<\/p>\n<p>44 After printing a line 10 rows behind<br \/>\ncat file | grep -A100 string<br \/>\ncat file | grep -B100 string # front<br \/>\ncat file | grep -C100 string # before and after<\/p>\n<p>sed -n &#8216;\/string\/,+100p&#8217;<\/p>\n<p>awk &#8216;\/string\/{f=100}&#8211;f&gt;=0&#8217;<\/p>\n<p>45. get the last argument on the command line<br \/>\nCopy the code following code:<br \/>\necho ${!#}<br \/>\necho ${$#} #wrong attempt<br \/>\n46. ??output redirection<\/p>\n<p>If you would like to you, STDERR and STDOUT output can be redirected to an output file,<br \/>\ntherefore, bash provides special redirection symbols &amp;&gt;<\/p>\n<p>ls file nofile &amp;&gt; \/dev\/null<\/p>\n<p>How do we redirect script inside? Nothing special, and the general redirection same.<br \/>\nCopy the code following code:<\/p>\n<p>#!\/bin\/bash<br \/>\n#redirecting output to different locations<br \/>\necho &#8220;now redirecting all output to another location&#8221; &amp;&gt;\/dev\/null<br \/>\nThe problem comes if we want to redirect all output to a file it?<br \/>\nWe do not want to redirect the output every time about it, as the saying goes, Hermit own good ideas.<br \/>\nWe can use exec to permanent redirect, as follows:<\/p>\n<p>#!\/bin\/bash<br \/>\n#redirecting output to different locations<br \/>\nexec 2&gt;testerror<br \/>\necho &#8220;This is the start of the script&#8221;<br \/>\necho &#8220;now redirecting all output to another location&#8221;<\/p>\n<p>exec 1&gt;testout<br \/>\necho &#8220;This output should go to testout file&#8221;<br \/>\necho &#8220;but this should go the the testerror file&#8221; &gt;&amp; 2<\/p>\n<p>The output is as follows:<\/p>\n<p>This is the start of the script<br \/>\nnow redirecting all output to another location<br \/>\nlalor@lalor:~\/temp$ cat testout<br \/>\nThis output should go to testout file<br \/>\nlalor@lalor:~\/temp$ cat testerror<br \/>\nbut this should go the the testerror file<br \/>\nlalor@lalor:~\/temp$<br \/>\nAdditional ways to redirect:<br \/>\nCopy the code following code: exec 3 &gt;&gt; testout<br \/>\nCancel redirection:<br \/>\nCopy the code following code: exec 3&gt; &#8211;<\/p>\n<p>47. function<\/p>\n<p>Any variables are global variables defined elsewhere, if you want to define local variables need to add local keywords<br \/>\nthe shell function can also be used recursively<br \/>\nCopy the code following code:<br \/>\n#!\/bin\/bash<\/p>\n<p>function factorial {<br \/>\nif [[ $1 -eq 1 ]]; then<br \/>\necho 1<br \/>\nelse<br \/>\nlocal temp=$[ $1 &#8211; 1 ]<br \/>\nlocal result=`factorial $temp`<br \/>\necho $[ $result * $1 ]<br \/>\nfi<br \/>\n}<\/p>\n<p>result=`factorial 5`<br \/>\necho $result<br \/>\nCreate a library<\/p>\n<p>The function of a set in another file, and then load the file into the current command by source<\/p>\n<p>At the command line using the function<\/p>\n<p>The function is defined in ~ \/ .bashrc in to<\/p>\n<p>Passing arrays to functions<br \/>\nCopy the code the code below:<\/p>\n<p>#!\/bin\/bash<br \/>\n#adding values in an array<\/p>\n<p>function addarray {<br \/>\nlocal sum=0<br \/>\nlocal newarray<br \/>\nnewarray=(`echo &#8220;$@&#8221;`)<br \/>\nfor value in ${newarray[*]}<br \/>\ndo<br \/>\nsum=$[ $sum+$value ]<br \/>\ndone<br \/>\necho $sum<br \/>\n}<\/p>\n<p>myarray=(1 2 3 4 5)<br \/>\necho &#8220;The original array is: ${myarray[*]}&#8221;<br \/>\narg1=`echo ${myarray[*]}`<br \/>\nresult=`addarray $arg1`<br \/>\necho &#8220;The result is $result&#8221;<br \/>\n48. regex<br \/>\nMatch ip address: ?d+.d+.d+.d+<br \/>\nCommentary: When extracting useful ip address<br \/>\nMatch a specific number:<br \/>\n^[1-9]d*$ \/\/ match the positive integers<br \/>\n^-[1-9]d*$? \/\/ match negative integers<br \/>\n^-?[1-9]d*$? \/\/ match integers<br \/>\n^[1-9]d*|0$ \/\/ match non-negative integer (positive integer + 0)<br \/>\n^-[1-9]d*|0$? \/\/ matching non-positive integers (negative integer + 0)<br \/>\n^[1-9]d*.d*|0.d*[1-9]d*$ \/\/ match float<br \/>\n^-([1-9]d*.d*|0.d*[1-9]d*)$ \/\/ match negative float<br \/>\n^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ \/\/ match float<br \/>\n^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ \/\/ match non-negative floating point numbers (positive float + 0)<br \/>\n^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$? \/\/ matching non-float (negative float + 0 )<br \/>\nCommentary: When handling large amounts of data useful to note that amendments to the specific application<br \/>\nMatch a specific string:<br \/>\n^[A-Za-z]+$ \/\/ match a string of 26 English letters<br \/>\n^[A-Z]+$ \/\/ match by 26 uppercase letters of the alphabet composed of a string<br \/>\n^[a-z]+$ \/\/ match a string of 26 lowercase letters of the alphabet consisting of<br \/>\n^[A-Za-z0-9]+$ \/\/ match the string of numbers and 26 letters of the English<br \/>\n^ w + $ \/\/ matching string by numbers, 26 English letters or underscores the<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We collected a bunch of shell scripting skills, I said, I write a blog mainly to make some study notes, to facilitate their access to,<\/p>\n<p>so I would come up with such an article, there is no incomprehensible. About the origin of these skills, eh, I forgot, it may come from theunixschool, commandlinefu, cool ground [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3549"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3549"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3549\/revisions"}],"predecessor-version":[{"id":3550,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3549\/revisions\/3550"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}