├── .gitignore ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # shell入门到简单 学习总结1-12章 2 | 3 | ## 第一章Shell前言: 4 | 5 | Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁。用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操作。在Linux GUI日益完善的今天在运维领域Shell编程仍然起着不可忽视的作用。深入地了解和熟练地掌握Shell编程,是每一个Linux用户的必修。 6 | 7 | **1.1 Linux的Shell种类:Bash在日常工作中被广泛使用,同时,Bash也是大多数Linux系统默认的Shell. 经常可以看到`#!/bin/sh`,它同样也可以改为`#!/bin/bash`。** 8 | 9 | ## 第二章Shell脚本格式 10 | 11 | **1.1 第一行#!的作用是指定该脚本程序的命令解释器:** 12 | 13 | ```bash 14 | #!/bin/bash 15 | 16 | echo "Hello the world" 17 | ``` 18 | 19 | **2.1 执行脚本需要添加权限和运行脚本的方式** 20 | 21 | ```bash 22 | chmod a+x print.sh 23 | 24 | 如果没有权限通过bash/sh方式: 25 | bash print.sh #调用bash程序解释器脚本内容执行 26 | sh print.sh #调用sh程序解释脚本内容并执行 27 | ``` 28 | 29 | ## 第三章Shell变量 30 | 31 | shell变量 32 | 变量是shell 传递数据的一种方法。变量是用来代表每个值的符号名。 33 | 34 | **1.1 Shell 有两类变量:临时变量和永久变量。** 35 | 36 | 临时变量: 是shell 程序内部定义的,其使用范围仅限于定义它的程序,对其它程序不可见。 37 | 永久变量: 是环境变量,其值不随shell 脚本的执行结束而消失。 38 | 39 | 例: 40 | 41 | ```bash 42 | [root@xuegod63 test]# echo $PATH 43 | /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin 44 | ``` 45 | 46 | **2.1 用户定义变量:由字母或下划线打头。 由字母、数字或下划线组成,并且大小写字母意义不同。变量名长度没有限制。** 47 | 引用变量值时,要在变量名前加上前缀“$” 48 | 49 | ```bash 50 | [root@xuegod63 test]# A=aaa 51 | # echo $A 52 | aaa 53 | [root@xuegod63 test]# A = aaa 54 | bash: A: command not found 55 | ``` 56 | 57 | **3.1 位置变量和特殊变量** 58 | 59 | 位置变量:Shell解释执行用户的命令时,将命令行的第一个字作为命令名,而其它字作为参数。由出现在命令行上的位置确定的参数称为位置参数。 60 | 位置变量:使用$N 来表示 61 | 62 | ```bash 63 | [root@xuegod63 test]# ./example.sh file1 file2 file3 64 | $0 这个程序的文件名 example.sh 65 | $n 这个程序的第n个参数值,n=1..N 66 | ``` 67 | 68 | **4.1 特殊变量:** 69 | 70 | 有些变量是一开始执行Script脚本时就会设定,且不能被修改,但我们不叫它只读的系统变量,而叫它特殊变量。这些变量当一执行程序时就有了,以下是一些等殊变量: 71 | 72 | ```bash 73 | $* 这个程序的所有参数 74 | $# 这个程序的参数个数 75 | $ 这个程序的PID 76 | $! 输出上一个后台程序的PID 77 | $? 输出上一个指令的返回值 78 | ``` 79 | 80 | **5.1 自定义使用变量** 81 | 82 | 使用一个定义过的变量,只要在变量名前面加美元符号即可,如: 83 | 84 | • 中间不能有空格,可以使用下划线(_)。 85 | • 不能使用标点符号。 86 | • 不能使用bash里的关键字(可用help命令查看保留关键字)。 87 | 88 | ```bash 89 | [root@xuegod63 test]# cat expr.sh 90 | #! /bin/sh 91 | a=10 92 | b=20 93 | c=30 94 | value1=`expr $a + $b + $c` 95 | echo "The value of value1 is $value1" 96 | value2=`expr $c / $b` 97 | echo "The value of value2 is $value2" 98 | value3=`expr $c \* $b` #整除 99 | echo "The value of value3 is $value3" 100 | value4=`expr $a + $c / $b` 101 | echo "The value of value4 is $value4" 102 | ``` 103 | 104 | 测试: 105 | 106 | ```bash 107 | [root@xuegod63 test]# ./expr.sh 108 | The value of value1 is 60 109 | The value of value2 is 1 110 | The value of value3 is 600 111 | The value of value4 is 11 112 | ``` 113 | 114 | 115 | 这里在说明点: 116 | 117 | 变量名外面的花括号是可选的,加不加都行,加花括号是为了帮助解释器识别变量的边界,比如下面这种情况: 118 | 119 | 120 | ```bash 121 | for skill in Ada Coffe Action Java 122 | do 123 | echo "I am good at ${skill}Script" 124 | done 125 | ``` 126 | 如果不给skill变量加花括号,写成`echo "I am good at $skillScript"`,解释器就会把`$skill`Script当成一个变量(其值为空),代码执行结果就不是我们期望的样子了。 127 | 128 | 推荐给所有变量加上花括号,这是个好的编程习惯。 129 | 130 | 131 | 这里在变量使用read,课外补充: 132 | 133 | read命令接收标准输入(键盘)的输入,或者其他文件描述符的输入。得到输入后,read命令将数据放入一个标准变量中。 134 | 135 | ```bash 136 | 1.基本读取 137 | 138 | #!/bin/bash 139 | echo -n "Enter your name:" #参数-n的作用是不换行,echo默认是换行 140 | read name #从键盘输入 141 | echo "hello $name, welcome to my program" 142 | exit 0 #退出shell程序。 143 | 144 | 其等效于以下: 145 | read -p "Enter your name:" name #-p参数,允许在read命令行中直接指定一个提示 146 | ``` 147 | 使用read命令存在着潜在危险。脚本很可能会停下来一直等待用户的输入。如果无论是否输入数据脚本都必须继续执行,那么可以使用 -t 选项指定一个计时器,指定read命令等待输入的秒数。当计时满时,read命令返回非零值(0为正常退出状态); 148 | 149 | 举例: 150 | 151 | ```bash 152 | #!/bin/bash 153 | if read -t 5 -p "please enter your name:" name 154 | then 155 | echo "hello $name, welcome to my script" 156 | else 157 | echo "sorry,too slow" 158 | fi 159 | exit 0 160 | ``` 161 | 162 | 163 | ## 第四章Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数 164 | 165 | 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:`$n`。n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 166 | 167 | 下面是$其他参数使用方法,一般用的比较多的都是传递脚本参数: 168 | 169 | ```bash 170 | $# 传递到脚本的参数个数 171 | $* 以一个单字符串显示所有向脚本传递的参数。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 172 | $$ 脚本运行的当前进程ID号 173 | $! 后台运行的最后一个进程的ID号 174 | $@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。 175 | $- 显示Shell使用的当前选项,与set命令功能相同。 176 | $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 177 | ``` 178 | 179 | 180 | 这个跟我们自定义变量有点区别, 181 | 182 | 看例子脚本: 183 | 184 | ```bash 185 | #!/bin/bash 186 | #author=yangc 187 | #blog.yangcvo.me 188 | 189 | echo "shell 脚本很好用1:$1"; 190 | echo "shell 脚本很好用2:$2"; 191 | echo "shell 脚本很好用3:$3"; 192 | echo "File Name: $0" 193 | echo "First Parameter : $1" 194 | echo "First Parameter : $2" 195 | echo "Quoted Values: $@" 196 | echo "Quoted Values: $*" 197 | echo "Total Number of Parameters : $#" 198 | ``` 199 | 使用脚本传递参数: 200 | 201 | ```bash 202 | shell 脚本很好用1:3 203 | shell 脚本很好用2:4 204 | shell 脚本很好用3:6 205 | File Name: sh.sh 206 | First Parameter : 3 207 | First Parameter : 4 208 | Quoted Values: 3 4 6 209 | Quoted Values: 3 4 6 210 | Total Number of Parameters : 3 211 | ``` 212 | 213 | ## 第五章 Shell替换:Shell变量替换,命令替换,转义字符 214 | 215 | 如果表达式中包含特殊字符,Shell 将会进行替换。例如,在双引号中使用变量就是一种替换,转义字符也是一种替换。 216 | 217 | ```bash 218 | #!/bin/bash 219 | a=10 220 | echo -e "Value of a is $a \n" 221 | ``` 222 | 运行结果: 223 | 224 | ```bash 225 | Value of a is 10 226 | ``` 227 | 228 | 这里 -e 表示对转义字符进行替换。如果不使用 -e 选项,将会原样输出: 229 | 230 | ```bash 231 | Value of a is 10\n 232 | ``` 233 | 234 | **2.1 命令替换** 235 | 236 | 命令替换是指Shell可以先执行命令,将输出结果暂时保存,在适当的地方输出。 237 | 238 | ```bash 239 | #!/bin/bash 240 | DATE=`date` 241 | echo "Date is $DATE" 242 | USERS=`who | wc -l` 243 | echo "Logged in user are $USERS" 244 | UP=`date ; uptime` 245 | echo "Uptime is $UP" 246 | ``` 247 | 248 | 运行结果: 249 | 250 | ``` 251 | Date is 2017年 04月 28日 星期五 11:41:34 CST 252 | Logged in user are 1 253 | Uptime is 2017年 04月 28日 星期五 11:41:34 CST 254 | 11:41:34 up 266 days, 10:43, 1 user, load average: 0.33, 0.14, 0.11 255 | ``` 256 | 257 | ## 第六章Shell注释 258 | 259 | 以“#”开头的行就是注释,会被解释器忽略。 260 | 这里因为很多写脚本都带有个性的风格,这样写的更加有乐趣。 261 | 262 | sh里没有多行注释,只能每一行加一个#号。只能像这样: 263 | 264 | ```bash 265 | #-------------------------------------------- 266 | # 这是一个自动打ipa的脚本,基于webfrogs的ipa-build书写: 267 | # https://github.com/webfrogs/xcode_shell/blob/master/ipa-build 268 | # 功能:自动为etao ios app打包,产出物为14个渠道的ipa包 269 | # 特色:全自动打包,不需要输入任何参数 270 | #-------------------------------------------- 271 | ##### 用户配置区 开始 ##### 272 | # 273 | # 274 | # 项目根目录,推荐将此脚本放在项目的根目录,这里就不用改了 275 | # 应用名,确保和Xcode里Product下的target_name.app名字一致 276 | # 277 | ##### 用户配置区 结束 ##### 278 | ``` 279 | 280 | 如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢?每一行加个#符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。 281 | 282 | ## 第七章Shell echo命令和printf命令:格式化输出语句 283 | 284 | echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式: 285 | 286 | 287 | **1.1 显示转义字符** 288 | 289 | ```bash 290 | echo "\"It is a test\"" 291 | 执行结果将是: 292 | "It is a test" 293 | ``` 294 | **显示变量** 295 | 296 | ```bash 297 | name="OK" 298 | echo "$name It is a test" 299 | 300 | 执行结果将是: 301 | OK It is a test 302 | ``` 303 | 304 | 同样双引号也可以省略。 305 | 306 | **2.1 shell 脚本中echo 颜色显示内容** 307 | 308 | 这里我因为之前在学习shell没有明确提到这个,这里我自己总结就加进来,因为很多次看到专业团队写shell跑整个脚本过程,特别舒服,开头有提示颜色说明,特别是国外哪些人写的不止好用,而且脚本跑起来也很有意思。 309 | 310 | 后面我写脚本都会该注意和提示都会有突出颜色标注。(这是个好习惯) 311 | 312 | shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要使用参数 `-e` 313 | 314 | 脚本举例: 315 | 316 | ```bash 317 | echo -e "\033[字背景颜色;文字颜色m字符串\033[0m" 318 | ``` 319 | 320 | 例如: 321 | 322 | ```bash 323 | echo -e "\033[41;36m something here \033[0m" 324 | ``` 325 | 其中41的位置代表底色, 36的位置是代表字的颜色. 326 | 327 | ⚠️注: 328 |   1、字背景颜色和文字颜色之间是英文的"" 329 |   2、文字颜色后面有个m 330 |   3、字符串前后可以没有空格,如果有的话,输出也是同样有空格 331 |   下面是相应的字和背景颜色,可以自己来尝试找出不同颜色搭配 332 | 333 | 334 | ```bash 335 | unset -f pathmunge 336 | echo -e '\t\t\t\t\t\e[1;31m登录情况\e[0m\n' 337 | last -n 6|nali 338 | uptime 339 | ``` 340 | 341 | 这里贡献下代码颜色: 342 | 343 | ```bash 344 |   echo -e “\033[30m 黑色字 \033[0m” 345 |   echo -e “\033[31m 红色字 \033[0m” 346 |   echo -e “\033[32m 绿色字 \033[0m” 347 |   echo -e “\033[33m 黄色字 \033[0m” 348 |   echo -e “\033[34m 蓝色字 \033[0m” 349 |   echo -e “\033[35m 紫色字 \033[0m” 350 |   echo -e “\033[36m 天蓝字 \033[0m” 351 |   echo -e “\033[37m 白色字 \033[0m” 352 | ``` 353 | 354 | 查看效果: 355 | 356 | ![](http://7xrthw.com1.z0.glb.clouddn.com/shell_01.png) 357 | 358 | 效果图 359 | 360 | ![](http://7xrthw.com1.z0.glb.clouddn.com/shell_02.png) 361 | 362 | 363 | **3.1 printf 命令用于格式化输出, 是echo命令的增强版。它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同。** 364 | 365 | 注意:printf 由 POSIX 标准所定义,移植性要比 echo 好。 366 | 367 | 举例: 368 | 369 | ```bash 370 | # 没有引号也可以输出 371 | $ printf %s abcdef 372 | abcdef 373 | # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用 374 | $ printf %s abc def 375 | abcdef 376 | $ printf "%s\n" abc def 377 | abc 378 | def 379 | $ printf "%s %s %s\n" a b c d e f g h i j 380 | a b c 381 | d e f 382 | g h i 383 | ``` 384 | 385 | ## 第八章 Shell if else 判断语句 386 | 387 | 这里我要说下 这里入门说的前面几章和第八章都是经常用的比较多的。 388 | 389 | if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句: 390 | if ... fi 语句; 391 | if ... else ... fi 语句; 392 | if ... elif ... else ... fi 语句。 393 | 394 | 395 | **1.1 if ... else 语句** 396 | 397 | if ... else 语句的语法: 398 | 399 | ```bash 400 | if [ expression ] 401 | then 402 | Statement(s) to be executed if expression is true 403 | fi 404 | ``` 405 | 406 | 如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。 407 | 408 | 最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。 409 | 410 | 注意:`expression `和方括号`([ ])`之间必须有空格,否则会有语法错误。 411 | 412 | 举个例子: 413 | 414 | ```bash 415 | #!/bin/sh 416 | a=10 417 | b=20 418 | if [ $a == $b ] 419 | then 420 | echo "a is equal to b" 421 | fi 422 | if [ $a != $b ] 423 | then 424 | echo "a is not equal to b" 425 | fi 426 | ``` 427 | 运行结果: 428 | 429 | a is not equal to b 430 | 431 | 432 | **2.1文件表达式** 433 | 434 | 我们经常用到文件判断, 整数变量判断。 435 | 436 | ```bash 437 | if [ -f file ] 如果文件存在 438 | if [ -d ... ] 如果目录存在 439 | if [ -s file ] 如果文件存在且非空 440 | if [ -r file ] 如果文件存在且可读 441 | if [ -w file ] 如果文件存在且可写 442 | if [ -x file ] 如果文件存在且可执行 443 | if [ -z file ] -z代表的是该变量是否有值。 444 | ``` 445 | 446 | 举例: 447 | 448 | ```bash 449 | # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 450 | if [ ! -x "$myPath"]; then 451 | mkdir "$myPath" 452 | fi 453 | 454 | # 这里的-d 参数判断$myPath是否存在 455 | if [ ! -d "$myPath"]; then 456 | mkdir "$myPath" 457 | fi 458 | 459 | # 这里的-f参数判断$myFile是否存在 460 | if [ ! -f "$myFile" ]; then 461 | touch "$myFile" 462 | fi 463 | 464 | # 其他参数还有-n,-n是判断一个变量是否是否有值 465 | if [ ! -n "$myVar" ]; then 466 | echo "$myVar is empty" 467 | exit 0 468 | fi 469 | 470 | # 两个变量判断是否相等 471 | if [ "$var1" = "$var2" ]; then 472 | echo '$var1 eq $var2' 473 | else 474 | echo '$var1 not eq $var2' 475 | fi 476 | ``` 477 | **3.1整数变量表达式** 478 | 479 | ```bash 480 | if [ "int1" -eq "int2" ] 如果int1等于int2 481 | if [ "int1" -ne "int2" ] 如果不等于 482 | if [ "int1" -ge "int2" ] 如果>== 483 | if [ "int1" -gt "int2" ] 如果> 484 | if [ "int1" -le "int2" ] 如果<= 485 | if [ "int1" -lt "int2" ] 如果< 486 | ``` 487 | 488 | 489 | 490 | if !=不等于使用 491 | 492 | ```bash 493 | if [ $TMZONE != Asia/Shanghai ];then 494 | 495 | 496 | 这个时区不等于Asia/Shanghai 也就是没有这个名字 497 | 498 | 就\cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 499 | ``` 500 | 501 | if [ -f $1] 的含义: 502 | 503 | 这是一个判断语句的头半句,意思是:将一个文件普通名传给传给$1,并判断这个文件是否存在。后半句应该还有:then...,存在应该怎样做;和else...不存在应该怎样做。 504 | 505 | 506 | 举例: 507 | 508 | ```bash 509 | 510 | [root@xuegod63 test]# cat elif.sh 511 | #!/bin/bash 512 | 513 | echo "input a file name:" 514 | read file_name 515 | 516 | if [ -d $file_name ] ; then 517 | echo " $file_name is a dir" 518 | elif [ -f $file_name ] ; then 519 | echo " $file_name is file" 520 | elif [ -c $file_name -o -b $file_name ] ; then 521 | echo " $file_name is a device file" 522 | else 523 | echo " $file_name does not exist " 524 | fi 525 | ``` 526 | 527 | ## 第九章 Shell for循环 528 | 529 | 530 | **1.1 for循环一般格式为:** 531 | 532 | ```bash 533 | for 变量 in 列表 534 | do 535 | command1 536 | command2 537 | ... 538 | commandN 539 | done 540 | ``` 541 | 列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。 542 | 543 | in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。 544 | 545 | 例如,顺序输出当前列表中的数字: 546 | 547 | ```bash 548 | for loop in 1 2 3 4 5 549 | do 550 | echo "The value is: $loop" 551 | done 552 | ``` 553 | 运行结果: 554 | 555 | ```bash 556 | The value is: 1 557 | The value is: 2 558 | The value is: 3 559 | The value is: 4 560 | The value is: 5 561 | ``` 562 | 563 | 564 | 这里比如举例用的比较多的 yum循环安装依赖包。 565 | 566 | ```bash 567 | for i in \ 568 | gcc \ 569 | gcc-c++ \ 570 | vim-enhanced \ 571 | lrzsz \ 572 | ntpdate \ 573 | ; 574 | do 575 | yum -y install $i 576 | ``` 577 | 578 | 顺序输出字符串中的字符: 579 | 580 | ```bash 581 | for str in 'This is a string' 582 | do 583 | echo $str 584 | done 585 | ``` 586 | 运行结果: 587 | 588 | ```bash 589 | This is a string 590 | ``` 591 | 592 | 显示主目录下以 .bash 开头的文件: 593 | 594 | ```bash 595 | #!/bin/bash 596 | for FILE in $HOME/.bash* 597 | do 598 | echo $FILE 599 | done 600 | ``` 601 | 602 | 运行结果: 603 | 604 | ```bash 605 | /root/.bash_history 606 | /root/.bash_logout 607 | /root/.bash_profile 608 | /root/.bashrc 609 | ``` 610 | 611 | ## 第十章 Shell while循环 612 | 613 | while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为: 614 | 615 | 以下是一个基本的while循环,测试条件是:如果COUNTER小于5,那么返回 true。COUNTER从0开始,每次循环处理时,COUNTER加1。运行上述脚本,返回数字1到5,然后终止。 616 | 617 | ```bash 618 | COUNTER=0 619 | while [ $COUNTER -lt 5 ] 620 | do 621 | COUNTER='expr $COUNTER+1' 622 | echo $COUNTER 623 | done 624 | ``` 625 | 626 | 运行结果: 627 | 628 | ```bash 629 | 运行脚本,输出: 630 | 1 631 | 2 632 | 3 633 | 4 634 | 5 635 | ``` 636 | 637 | 638 | ## 第十一章: Shell输入输出重定向:Shell Here Document,/dev/null文件 639 | 640 | 命令输出重定向的语法为: 641 | 642 | 输出重定向会覆盖文件内容,请看下面的例子: 643 | 644 | ```bash 645 | echo line 1 > users 646 | $ cat users 647 | line 1 648 | $ 649 | ``` 650 | 651 | 652 | 653 | 654 | 也可以 将 Here Document 用在脚本中,例如: 655 | 656 | ```bash 657 | #!/bin/bash 658 | cat << EOF 659 | +------------------------------------+ 660 | | | 661 | | This is a simple lookup program | 662 | | for good (and bad) restaurants | 663 | | in Cape Town. | 664 | |------------------------------------+ 665 | EOF 666 | ``` 667 | 668 | 运行结果: 669 | 670 | ```bash 671 | sh test.sh 672 | +------------------------------------+ 673 | | | 674 | | This is a simple lookup program | 675 | | for good (and bad) restaurants | 676 | | in Cape Town. | 677 | |------------------------------------+ 678 | ``` 679 | 680 | **2.1/dev/null 文件** 681 | 682 | 以前有人问过我这个/dev/null是什么意思。 这个是在shell脚本里面经常用到的。 683 | 684 | 比如说:不明白grep "tomcat" /etc/passwd>/dev/null 2>&1 685 | 686 | 在/etc/passwd查找是否包含字符串tomcat 并把标准输出和标准错误一起重定向到/dev/null 687 | 688 | 1. 输出重定向(>)操作在命令执行发生错误时,会将错误信息直接显示到屏幕,并不记录到文件中,没必要放在内存 689 | 2. 标准输出与错误输出重定向(&>)可以将标准输出和错误输出信息一并重新定向到文件,屏幕上不会显示任何信息 ,如果没有 >/dev/null 2>&1 ,结果 就直接显示在屏幕上咯。 690 | 691 | grep "tomcat" /etc/passwd>/dev/null 2>&1 692 | 首先是grep "tomcat" /etc/passwd>/dev/null 将标准输出重定向到/dev/null 然后2>&1把标准错误重定向到标准输出 也就是也被重定向到了/dev/null 那结果就是标准输出和标准错误都被重定向到了/dev/null 693 | 694 | 695 | 696 | /dev/null 是一个特殊的文件,写入到它的内容都会被丢弃;如果尝试从该文件读取内容,那么什么也读不到。但是 /dev/null 文件非常有用,将命令的输出重定向到它,会起到”禁止输出“的效果。 697 | 698 | 699 | 如果希望屏蔽 stdout 和 stderr,可以这样写: 700 | 701 | ```bash 702 | command > /dev/null 2>&1 703 | ``` 704 | 705 | 706 | ## Shell个人总结常规使用较多的一些参数语法。 707 | 708 | 我个人写shell脚本都使用到sleep参数。 709 | 710 | ##### 1.Shell脚本中让进程休眠的方法(sleep用法) 711 | 712 | ```bash 713 | 有时候写Shell的脚本,用于顺序执行一系列的程序。 有些程序在停止之后并没能立即退出,就例如有一个 tomcat 挂了,就算是用 kill -9 命令也还没瞬间就结束掉。 714 | 715 | 这么如果 shell 还没等其退出就接着执行下一行,这么就出乱子了。 刚知道了原来 shell 也能有 sleep 的参数。 716 | 717 | 复制代码 代码如下: 718 | 719 | sleep 1 睡眠1秒 720 | sleep 1s 睡眠1秒 721 | sleep 1m 睡眠1分 722 | sleep 1h 睡眠1小时 723 | 用法如下,例如重启tomcat: 724 | 725 | 复制代码 代码如下: 726 | 727 | #!/bin/sh 728 | /opt/tomcat/bin/shutdown.sh 729 | sleep 3 #等3秒后执行下一条 730 | /opt/tomcat/bin/startup.sh 731 | ``` 732 | 733 | ##### 2.SHELL脚本中有exit 0,和exit 1 的区别 734 | 735 | 这里我先举个例子: 736 | 737 | ```bash 738 | #!/bin/sh 739 | 740 | x=10 741 | if [ "$x" = 10 ]; then 742 | echo "is 10" 743 | exit 0 744 | fi 745 | 746 | 747 | #!/bin/sh 748 | 749 | x=10 750 | if [ "$x" = 10 ]; then 751 | echo "is 10" 752 | exit 1 753 | fi 754 | ``` 755 | 当你 exit 0 的时候 756 | 在调用环境echo $? 就返回0 ,也就是说调用环境就认为 你的这个程序执行正确. 757 | 当 exit 1 的时候,一般是出错定义这个1,也可以是其他数字,很多系统程序这个错误编号是有约定的含义的。 但不为0 就表示程序运行出错。 758 | 759 | 调用环境就可以根据这个返回值判断 你这个程序运行是否ok。 760 | 如果你用 脚本a调用脚本b,要在a中判断b是否正常返回,就是根据exit 0or1来识别。 761 | 执行完b后,判断 $? 就是返回值. 762 | 763 | ##### 3.shell中获取时间 764 | 765 | ```bash 766 | 取当天时间日期赋值给DATE变量 767 | DATE=$(date +%Y%m%d) 768 | 有时候我们需要使用今天之前或者往后的日期,这时可以使用date的 -d参数 769 | 获取明天的日期 770 | date -d next-day +%Y%m%d 771 | 获取昨天的日期 772 | date -d last-day +%Y%m%d 773 | 获取上个月的年和月 774 | date -d last-month +%Y%m 775 | 获取下个月的年和月 776 | date -d next-month +%Y%m 777 | 获取明年的年份 778 | date -d next-year +%Y 779 | ``` 780 | 781 | ##### 4.shell 过滤文件 重复如何去重 782 | 783 | ```bash 784 | sort |uniq 用这个命令。 785 | 先 sort 排序 ,再 uniq 去重 ,要是想知道重复的数量就 用 uniq -c 786 | 这个执行看下就可以了。 787 | ``` 788 | 789 | 790 | ##### 5. shell 单引号,双引号 区别与使用 791 | 792 | ```bash 793 | 单引号 794 | 795 | str='this is a string' 796 | 797 | 单引号字符串的限制: 798 | * 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的; 799 | * 单引号字串中不能出现单引号(对单引号使用转义符后也不行)。 800 | ``` 801 | 802 | 803 | script ,脚本 804 | 805 | ##### 1. 搞开发的同事让我把他写个脚本可以实现ipset.sh eth0 192.168.1.1 255.255.255.0 192.168.254.254来配置某个网卡的信息 806 | 807 | ``` 808 | #!/bin/bash 809 | hwaddr=ifconfig |grep HWaddr |awk '{print $5}' 810 | echo “DEVICE=$1 811 | HWADDR=$hwaddr 812 | TYPE=Ethernet 813 | ONBOOT=yes 814 | NM_CONTROLLED=yes 815 | BOOTPROTO=static 816 | IPADDR=$2 817 | NETMASK=$3 818 | GATEWAY=$4” > /etc/sysconfig/network-scripts/ifcfg-$1 819 | service network restart 820 | ``` 821 | 822 | ###### 2. shell脚本实现将文件中的几行输出合并成一行显示现在有一个文件aa.txt ,其实现格式如下,而我想实现的结果是192.168.1.17 down 823 | ``` 824 | [root@localhost shell]# cat aa.txt 825 | 192.168.1.17 826 | down 827 | 192.168.1.103 828 | open 829 | 192.168.1.221 830 | open 831 | 192.168.1.104 832 | down 833 | 192.168.1.175 834 | down 835 | 192.168.1.112 836 | open 837 | 192.168.1.11 838 | down 839 | [root@localhost shell]# awk ‘{if (NR%2==0){print $0} else {printf”%s “,$0}}’ aa.txt 840 | 192.168.1.17 down 841 | 192.168.1.103 open 842 | 192.168.1.221 open 843 | 192.168.1.104 down 844 | 192.168.1.175 down 845 | 192.168.1.112 open 846 | 192.168.1.11 down 847 | 或者 848 | [root@localhost shell]# cat aa.txt | awk ‘{if(NR%2!=0)ORS=” “;else ORS=”\n”;print}’ 849 | 192.168.1.17 down 850 | 192.168.1.103 open 851 | 192.168.1.221 open 852 | 192.168.1.104 down 853 | 192.168.1.175 down 854 | 192.168.1.112 open 855 | 192.168.1.11 down 856 | ``` 857 | 但是如果aa.txt 的内容如果换种形式 858 | 859 | 860 | 861 | 862 | ## sed 863 | 864 | ##### 1. sed 取某时段内apache的访问日志 865 | 开发部有如下需求: 866 | * 导出 2013-05-24 15:00:00 ~ 2013-05-28 16:00:00 之间的apache访问日志 867 | Apache日志格式为: 868 | ``` 869 | 222.92.115.194 - - [28/May/2013:17:01:00 +0800] "GET /media/js/jquery.eislideshow.js HTTP/1.1" 304 - 870 | 222.92.115.194 - - [28/May/2013:17:01:00 +0800] "GET /media/style/tpl/tpl_buy_left/tpl_buy_left.js HTTP/1.1" 304 - 871 | 222.92.115.194 - - [28/May/2013:17:01:01 +0800] "GET /favicon.ico HTTP/1.1" 404 17846 872 | 222.92.115.194 - - [28/May/2013:17:01:01 +0800] "GET /large-display/interactive/ HTTP/1.1" 200 21382 873 | 222.92.115.194 - - [28/May/2013:17:01:02 +0800] "GET /favicon.ico HTTP/1.1" 404 17846 874 | 222.92.115.194 - - [28/May/2013:17:01:04 +0800] "GET /large-display/single/ HTTP/1.1" 200 21386 875 | 222.92.115.194 - - [28/May/2013:17:01:04 +0800] "GET /favicon.ico HTTP/1.1" 404 17846 876 | 222.92.115.195 - - [28/May/2013:17:01:05 +0800] "GET /dsc/ HTTP/1.1" 200 34530 877 | 222.92.115.195 - - [28/May/2013:17:01:05 +0800] "GET /media/img/channel_icon.jpg HTTP/1.1" 404 17846 878 | 879 | 880 | 截取命令: 881 | 882 | [root@style logs]# sed -n '/24\/May\/2013:15:00:01/,/28\/May\/2013:16:59:58/p' xxxx-access_log > 20130524.15-20130528.16-access_log.txt 883 | 884 | PS:需要注意的是如果起始时间在日志中不存在,则整个截取将返回 0 行结果。而如果结束时间在日志中不存在,则会截取到日志的最后一条。所以在截取前得要找到最日志中最合适的起始点和结束点。 885 | 我的做法是先使用grep去找到两个点 再使用sed去截取 886 | 887 | # 找出 2013-05-24 15点第一条记录的时间 888 | [root@style logs]# grep '24/May/2013:15' xxxx-access_log | head -1 889 | 10.200.114.183 - - [24/May/2013:15:00:01 +0800] "GET /gp10/pic_259_218_1368781965.png HTTP/1.0" 401 484 890 | # 找出 2013-05-28 16点最后一条记录的时间 891 | [root@style logs]# grep '28/May/2013:16' xxxx-access_log | tail -1 892 | 222.92.115.195 - - [28/May/2013:16:59:58 +0800] "GET /favicon.ico HTTP/1.1" 404 17846 893 | # 然后取这两个时间段之间的记录 894 | ``` 895 | --------------------------------------------------------------------------------