Let's review the usage of the last section redirect:
1. Quickly empty files
Cat Demo.txt </dev/null
Note: Linux has a classic saying "Everything is a file",/dev/null can be considered a special empty file, more image point, can be understood as a sci-fi film in the black hole, any information to the output to it, there is no return, of course, there is no information in the black hole can come out.
In general, the above meaning is the use of < black hole as demo.txt standard input, black hole without any content, the content of any file is swallowed by it, naturally there is nothing left, so the end is demo.txt by the black hole washed empty.
/dev/null There are other uses, such as using it to let nohup do not generate nohup.out files, see: http://www.cnblogs.com/yjmyzz/p/4831182.html
2. Output source code at execution time
#!/bin/bash-vprintf '%0.2f\n ' 12.12334
The results of the implementation are as follows:
#!/bin/bash-vprintf '%0.2f\n ' 12.1233412.12
Note: Before the output of line 3rd, the source code is printed out, the secret is the last of the 1th line of the- v parameter
3. Debug mode
#!/bin/bash-xprintf '%0.2f\n ' 12.12334echo ' Hello '
The results of the implementation are as follows:
+ printf '%0.2f\n ' 12.1233412.12+ echo Hellohello
Note: The parameter after the first line becomes- x, plus this, when executed, each line of code before execution, will first output the corresponding source code, and start with +, very convenient debugging.
4. If and test and []
4.1 Digital Judgments
#!/bin/bash-xi=$1 #变量i的值取第1个参数的值if test $i-GT 89; Then #如果i >89 Echo ' A ' elif test $i-gt, then #如果i >79 Echo ' B ' elif test $i-eq 60-o $i-gt 60;then #如果i =60 or i>60 (i.e.: i>=60) echo ' C ' elif test $i-gt 0;then #如果i >0 echo ' D ' elif test $i-lt 0;then #如果i <0 Echo ' Invalid ' else #i ==0 's case echo ' zero ' fi
Note:if test condition; Then statement fi This is the basic format, pay attention to the condition ; Do not omit, the other end symbol is fi (ie: the idea of a kind of palindrome), in addition to remember a bunch of abbreviations
-LT is the abbreviation forLess THan, which represents less than
-GT-the abbreviation for theGreater THan, which represents greater than
-EQ is the abbreviation forequal, which means equal, and also
-ne is the abbreviation forNot Equal, indicating that it is not equal to
-O is-or, indicating that the two logical judgments are " or " relationships, similar
-A is-and, which means that the relationship between two logical judgments is " with "
Elif is the abbreviation for else if
The above example runs the result:
./demo.sh 90+ i=90+ Test 90-gt 89+ Echo AA
The test statement also has a simplified wording, that is, the "test condition " into the " [condition] ", note that the two-terminal square brackets around to add a space , so the above can be changed to the wording:
i=$1if [$i-GT 89]; Then echo ' A ' elif [$i-gt], then echo ' B ' elif [$i-eq 60-o $i-gt], then echo ' C ' elif [$i-gt 0]; Then echo ' D ' elif [$i-lt 0], then echo ' invalid ' else echo ' zero ' fi
This looks much more beautiful, if you do not like the logic of-o or the wording, the 6th line can also be replaced by this
elif [$i-eq 60] | | [$i-GT 60]; Then
However, the details of the implementation are slightly different, in debug mode can be compared, with | | Input of the notation (test case: 61)
./demo2.sh 61+ i=61+ ' [' 61-gt '] ' + ' [' 61-gt '] ' + ' [' 61-eq '] ' + ' [' 61-gt '] ' + echo CC
And the output with the-O notation:
./demo2.sh 61+ i=61+ ' [' 61-gt '] ' + ' [' 61-gt '] ' + ' [' 61-eq 60-o 61-gt '] ' + echo CC
Compare the next 5-6 lines can be found, the difference is to judge once, or judge two times
4.2 String Judgments
#!/bin/bash-xstr1= "ABC" If [-Z "$str 1"]; Then Echo ' str1 was empty ' else echo ' str1 is not empty ' fiprintf "\ n" str2= "" if [-N "$str 2"]; then Echo ' str2 I s not empty ' else echo ' str2 is empty ' fiprintf ' \ n ' If ["$str 1" = "$str 2"]; then echo ' str1 = str2 ' Else Echo ' str1 <> str2 ' fi
Note:-N is-not empty to determine the string is not empty,-Z is-zero judgment string is empty, = judgment string is the same (when judging the string, remember to add double quotation marks)
Operation Result:
+ str1=abc+ ' ['-Z-ABC '] ' + echo ' str1 is not empty ' str1 are not empty+ printf ' \ n ' + str2=+ ' ['-n ' + '] ' + echo ' str2 is em Pty ' str2 is empty+ printf ' \ n ' + ' [' abc = ' '] ' + echo ' str1 <> str2 ' str1 <> str2
4.3 File and directory judgments
#!/bin/bash-xif [-F ~/.bash_profile]; Then Echo ' ~/.bash_profile was a file ' else Echo ' ~/.bash_profile is not a file ' fiprintf ' \ n ' if [-D ~/]; Then Echo ' ~/is a directory ' else echo ' ~/are not a directory ' fi
-F is the decision whether to file,-D that is to determine whether directory, output results:
+ ' ['-f/users/yjmyzz/.bash_profile '] ' + echo ' ~/.bash_profile is a file ' ~/.bash_profile is a file+ printf ' \ n ' + ' ['-D/ users/yjmyzz/' + echo ' ~/is a directory ' ~/is a directory
Bash/shell Programming Learning (2)