Once heard a great God told, with purpose to learn, knowledge often can remember prison, remember stability. With the help of mind Map this tool, some of the knowledge points I am interested in classification management. It is convenient for you to review later.
I'll roll back to learn Linux shell programming in mind map + code Snippets.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/58/FA/wKiom1TCUMziHNpwAAFVIO3v-Io357.jpg "title=" Linux Shell Raiders. png "alt=" wkiom1tcumzihnpwaafvio3v-io357.jpg "/>
escaped/Color
interface to user interaction
#打印一个普通的字符串 [[email protected] ~]# echo "It ' s isa dog" it ' s is a dog #打印一个带有单引号和换行符的字符串, single quotation marks can output normally, but line breaks do not have the effect of [[email protected] ~]# echo "it ' s isa dog\n this is new line "It ' s is a dog\n this is new line # -e turn on escape function [[email protected] ~]# echo -e "it" sis a dog\nthis is new line "It ' s is a dogthis is new line-e enable interpretation of backslash escapes [[ email protected] ~]# echo it is a dogit is a dog #红字 [[ email protected] ~]# echo -e "\e [1;31mthisis a color\e[0m" this Is a color[[email protected] ~]# echo -e "\033[1;31mthis is a red color\033[0m "This is a red color# Green Bottom [[email protected] ~]# echo -e] \e[1;42mthis is a red color\e[0m "this is a red color #红字绿底 [[email Protected] ~]# echo -e "\e[1;31;42mthis is a red color\e[0m" This is a red color #有效数字echo "SCALE=3;3/8" |bcecho $BC
Calculation
This is one of the functions of the programming language.
va=1;vb=2, #echo $ ($va + $VB); Error#echo $ (VA+VB); Errorecho [$va + $vb] #output: [1+2] Echo $[VA+VB] #okecho $ (($va + $vb)) #//ok let result= $va +vb #okecho $resultresult = ' expr 3 + 1 ' #ok, note the equal sign, no spaces on either side, result= ' expr $va + 1 ' can also echo $resultresult =$ (expr $va + 1) #ok, note equal sign, no space on either side, + must have a space, or it will be a string output Echo $result
Output Variable length
Built-in features (interest only)
[Email protected] test]# exportstr= "1234" [[email protected] test]# echo $STR 1234[[email protected] test]# echo ${#STR}4
function
This is the most basic, not the statement list it
#括号里不能有参数, get parameters through $1,$2 ... get function SayHello () {Echohello $1}#[email protected]: Parameter list #$*: parameter string SayHello zgy; #这样调用
Read command sequence
Can get the result of a command
#!/bin/bash commands=ls|cat-n Echo $COMMANDS #输出为空 commands=$ (ls|cat-n) # $COMMANDS #error echo $COMMANDS #输出期望结果 Ech O ' $COMMANDS ' #error echo ' ls|cat-n ' #输出期望结果 anti-reference ############################################### #子shell, in the child shell operation, Does not affect the main Shellecho ' pwd '; Cd/binecho ' pwd '; # output##/root/test#/bin echo ' pwd ';(cd/bin) echo ' pwd '; # output##/root/test#/root/test
time taken for printing
Evaluate the efficiency of an algorithm
start=$ (date +%s) #start = ' Date +%s ', the equal sign cannot have spaces, and if there is a space, the variable will be treated as a command for ((i = 0; i < 100000; i++);d o echo$i >/dev/n ulldoneend= ' Date +%s ' diff=$ (($end-$start)) echo "Use Times (ms):" $diff echo "use Times (ms):" $ (($end-$start))
Common tests
The Shell programming summary function is commonly used for judging permissions, etc.
#[[]] must have a space # whether it is a file, whether the file exists [[email protected] test]# [[ -f 1.txt ]]&& echo "1.txt is file" | | echo whether the "1.txt is notfile" 1.txt is file# is an executable [[email protected] test]# [[ -x 1.txt ]]&& echo "1.txt can be execute" | | echo "1.txt can be execute" 1.txt can be execute[[email protected] test]# [[ -x 1.txt ]]&& echo "1.txt can Be execute " | | echo "1.txt can ' T be execute" 1.txt can ' t be execute [[email protected] test]# chmod +x 1.txt[[email protected] test]# [[ -x 1.txt ]]&& echo "1.txt can be execute" | | echo "1.txt can ' T&NBSP;BE&NBSp;execute "1.txt can be execute[[email protected] test]# #是否是目录 [[email protected] test]# [[ -d 1.txt ]]&& echo "1.txt is dir" | | echo "1.txt is ' T dir" 1.txt is ' t dir[[email protected] test]# [[ -d /bin ]]&& echo "1.txt is dir" | | echo "1.txt is ' T dir" 1.txt is dir# judgment is empty string? [[Email protected] test]# [[ -z] 1 " ]] && echo " Is null " | | echo "Is not null" is not null[[email protected] test]# [[ -z "" ]] && echo "Is null" | | echo "Is not null" is null-z is opposite to-n function
Subtotal
Look at the book, very simple code, that is, a look at the code to understand. It's not easy to actually write it out and get results in the run. Above his business is not a tolerable.
I just write programs that are often encountered in some such situations. Sometimes a space is required (such as when the condition is judged). Sometimes you can't have a space (when a variable is assigned a value). Sometimes the single quotation marks are sometimes inverted. Ah to pay attention to these small details, experience.
Small code is also not easy.
If the vast number of readers, you can also look at my brain map, step by step to write a script, will also have some gains.
Let's get a beginning. On and off, as the study progresses, the examples will be deepened. Hope your shell level, can have a breakthrough.
This article is from a "simple" blog, so be sure to keep this source http://dba10g.blog.51cto.com/764602/1607563
Mind Mapping Linux Shell introduction of the small trial sledgehammer chapter