对ASP.NET程序员都非常有用的85个工具

介绍

这篇文章列出了针对ASP.NET开发人员的有用工具。

工具

1.Visual Studio

  1. Visual Studio Productivity Power tool:Visual Studio专业版(及以上)的扩展,具有丰富的功能,如快速查找,导航解决方案,可搜索的附加参考对话框等
  2. ReSharper:提高.NET开发人员生产力的工具,提高代码质量,通过提供快速修复消除错误,等等
  3. MZ-Tools:它可以在方法、文件、项目、解决方案或项目组、选定的文本,文件组合或项目组合中找到字符串。结果示于下面的结果窗口中,比由Microsoft IDE提供的要更方便。
  4. Web Essentials:提高生产力和帮助高效编写CSS,JavaScript,HTML等
  5. MSVSMON:远程调试监视器(msvsmon.exe)是一个Visual Studio连接进行远程调试的小型应用程序。在远程调试时,Visual Studio运行在一台计算机(调试器主机)上,远程调试监视器运行与你正在调试的应用程序运行在一台远程计算机上。
  6. WIX toolset:从XML源代码构建Windows安装程序包。
  7. Code digger::Code Digger是Visual Studio 2012/2013扩展程序,它可以帮助你了解你的代码行为。
  8. CodeMaid:CodeMaid是一个开源的Visual Studio 2012/2013/2015扩展程序,用于清理,挖掘和简化你的代码。
  9. OzCode:强大的Visual Studio调试器可视化工具。
  10. CodeRush:这是一个Visual Studio的重构和生产率插件。
  11. T4 Text Template::在Visual Studio中,T4 Text Template用作生成代码文件的模板。模板可以通过编写文本块和控制逻辑来定义。
  12. Indent Guides:在每个缩进级别添加垂直线。
  13. PowerShell Tools:一套用于开发和调试PowerShell脚本以及Visual Studio 2015中模块的工具。
  14. Visual Studio Code:免费的跨平台编辑器,用来构建和调试现代web和云的应用程序。 [由Cheung Tat Ming提供]
  15. AutoPoco:AutoPoco是一个高度可配置的框架,用于流畅构建可读的来自于Plain Old CLRObjects的测试数据。
  16. Supercharger:这是一个旨在显著改善Visual Studio开发体验的扩展程序。它建立在并增强了先前的VS10x产品,如CodeMAP、Editor View Enhancer、Comments Extender,同时还增加了新的高品质工具。

2.ASP.NET

  1. Fiddler:捕捉HTTP请求/响应以及模拟请求行为。
  2. AutoMapper:对象到对象的映射。例如,该工具可用于映射实体对象到领域对象,而不是写手动映射代码。
  3. Unity/Ninject/Castle Windsor/StructureMap/Spring.Net:依赖注入框架。有很多可用的DI框架。
  4. .NET Reflector:.NET程序集反编译器。
  5. dotPeek:.NET程序集反编译器。
  6. ILSpy::.NET程序集反编译器。
  7. memprofiler:查找内存泄漏并优化内存使用的强大工具。
  8. PostSharp:删除重复编码,并防止由于横切关注点以及面向方面编程而造成的代码膨胀。
  9. ASPhere:有GUI的web.config编辑器。

3.REST API

  1. Swagger UI:API测试和文档工具。[视频]
  2. PostMan:REST客户端Chrom扩展程序。 [由 Cheung Tat Ming提供]

4.WCF

  1. SOAP UI:API测试工具,支持所有标准协议和技术。
  2. WireShark:这是一个针对Unix和Windows的网络协议分析仪。它可以在TCP层捕获流量,帮助你发现soap封套。
  3. Svc TraceViewer:可以更好地查看由WCF产出的巨大跟踪文件。
  4. Svc Config Editor:用于管理WCF相关配置的GUI工具。

5.MSMQ

  1. QueueExplorer 3.4:复制,移动或删除邮件,保存和加载,压力测试,查看和编辑完整的邮件主体(通过.NET序列化对象的特殊支持),以及更多可对MSMQ做的。

6.LINQ

  1. LINQ Pad?:LINQPad是一个轻量级的工具,用于测试对SQL Server数据库的LINQ查询。它也可以测试用不同的.NET语言,如C#,VB等写的代码片断。
  2. LINQ Insight:LINQ Insight Express是一个Visual Studio插件,它允许你在设计时分析你的LINQ查询,并简化了编写和调试LINQ查询。

7.RegEx

  1. RegEx tester:用于正则表达式测试的Visual Studio扩展程序。
  2. regexr:在线RegEx开发和测试工具。
  3. regexpal:在线RegEx开发和测试工具。
  4. Expresso:Expresso是一个用于RegEx开发和测试的桌面工具。
  5. RegexMagic :用于自动生成来自于文本模式的正则表达式的工具。用户需要通过标记字串和选择不同的选项来培养模式。在此基础上,将自动生成正则表达式。这些工具还可以生成不同语言所需的代码。 [由: Samuel Christison提供]

8.Javascript / JQuery/ AngularJS

  1. JSHint:JavaScript代码质量的工具。还有一个工具,JSLine,它执行更严格的规则。
  2. JSFiddle:提供在浏览器中的一个环境,用来测试HTML,CSS和Javascript / JQuery。
  3. Protractor:端到端的框架用来测试angular应用程序。
  4. Batarang:添加工具用于调试和分析AngularJS应用程序。

9.SQL服务器

  1. SQL Profiler:SQL跟踪用来监测数据库引擎实例。
  2. ExpressProfiler:ExpressProfiler(又名SqlExpress Profiler)是有着基本GUI和集成的SQL Server Profiler简单又快捷的替代品。这可以与Express 和SQL Server 2005/2008 / 2008R2 / 2012/2014的non-Express 版本一起使用。 [由RickZeeland提供]
  3. SQL Sentry Plan explorer:工具提供SQL查询执行计划更好的图形视图。
  4. SQL Complete:提供SQL Server Management Studio和Visual Studio智能感知功能和改进的SQL格式器。
  5. NimbleText:文本操作和代码生成工具。
  6. Query Express:轻量级SQL查询分析器。
  7. IO Meter:提供IO子系统的细节。
  8. sqldecryptor:破译SQL Server对象,如存储过程,函数,触发器,视图,通过加密选项加密。
  9. SpatialViewer:查看和创建空间数据。
  10. ClearTrace:导入跟踪和探查文件到SQL Server并显示汇总性能信息。
  11. Internals Viewer for SQL Server:Internals Viewer是一个查看SQL Server存储引擎,查阅数据如何物理分配,组织和存储的工具。
  12. PAL:在性能日志读取,使用已知阈值分析。
  13. sqlquerystress:用T-SQL查询和程序的性能压力测试助攻。

10.NHibernate

  1. NHibernate Mapping Generator:生成NHibernate映射文件和对应于现有DB表的实体类。

11.Tally

  1. Tally ERP 9
  2. Tally dll:.NET的一个动态链接库,用于集成Tally Accounting软件以便于用编程的方式push和pull数据。

12.代码审查

  1. StyleCop:StyleCop是静态的代码分析工具,它强制你的C#源代码执行配置风格和一致性规则设置。它可以从Visual Studio内部运行或集成到MSBuild项目。
  2. FxCop?:FxCop是一个静态代码分析工具,它通过分析.NET程序集强制开发标准。

13.流量捕获

  1. WireShark:这是一个用于Unix和Windows的网络协议分析仪。它可以捕获TCP层的流量。
  2. HTTP Monitor:使开发人员可以查看你的计算机和互联网之间的所有HTTP流量。这包括请求数据(例如HTTP响应头和表单GET和POST数据)和响应数据(包括HTTP响应头和正文)。

14.诊断

  1. Glimpse:提供服务器端诊断数据。如,对于ASP.NET MVC项目,你需要从NuGet添加它。Glimpse的数据可以告诉你不同层面的延迟,真正表明你可以优化代码/解决方案以提高性能的区域。

15.性能

  1. PerfMon:使用性能计数器监控系统性能。
  2. yslow:YSlow分析web页面,并基于Yahoo!高性能网站的规则指出它们为什么这么缓慢。

16.代码转换器

  1. Telerik Code Converter:C#到VB以及VB到C#的代码转换器。这是一个在线编辑器。但是你可以选择“批量转换”以及zip格式上传文件。

17.数据提取和加载

  1. FileHelpers:.NET库,导入/导出文件、字符串或流中固定长度或有分隔记录的数据。
  2. LogParser:你可以写SQL到查询来应对各种日志文件,以及导出数据到各种目的地,如SQL表、CSV文件。

18.屏幕录制

  1. Wink:演示文稿制作软件。使用Wink,你可以捕捉截图,添加说明,注释等,以及创建演示。

19.文本编辑器

  1. Notepad++:源代码编辑器。
  2. Notepad2:轻量级又功能丰富的记事本般的文本编辑器。
  3. sublimetext:一个功能丰富的文本编辑器。

20.文档

  1. GhostDoc:GhostDoc是一个Visual Studio扩展程序,自动生成类型、参数、名称及其他相关信息方法和属性的XML文档注释。
  2. helpndoc:helpndoc是一个创建帮助文件的工具。它可以从单个源生成不同格式的文件。

21.其他

  1. FileZilla:FileZilla是一个免费的FTP解决方案。FileZilla Client用于FTP文件上传,FileZilla Server用于文件共享。
  2. TreeTrim:TreeTrim是用于修整源代码树的工具。它消除了调试文件,源代码控制绑定和临时文件。
  3. BrowserStack:跨浏览器测试网站。
  4. Firebug:功能丰富的针对于CSS,HTML和JavaScript开发关于生成网页的Firefox插件。
  5. BugShooting:屏幕截图软件,截屏并放到工作项,bug,问题跟踪项等。
  6. Web developer checklist:确保web开发最佳实践。
  7. XRAY:Firefox插件。功能丰富的书签。提供有关网页元素的信息。
  8. PowerGUI:有助于快速接纳和使用PowerShell来高效管理完整的Windows环境。
  9. Beyond Compare:它允许比较目录树和单个文件的内容。适应强,有流行语言的插件。 [由Ron Matuszek提供]
  10. Devart Codecompare:文件diff工具,读取C#,C ++,VB代码的结构。包括:文件夹比较工具,比较和合并文件和文件夹的独立app,代码审查支持。 [由Cheung Tat Ming提供]

警告

使用之前请验证工具。

许可证

这篇文章以及任何相关的源代码和文件,遵循 The Code Project Open License (CPOL)。

 

译文链接:http://www.codeceo.com/article/85-aspnet-tools.html
英文原文:Useful Tools for ASP.NET Developers

知道这20个正则表达式,能让你少写1,000行代码

知道这20个正则表达式,能让你少写1,000行代码

正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑。熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升。

正则表达式经常被用于字段或任意字符串的校验,如下面这段校验基本日期格式的JavaScript代码:

<code class="scala"><span class="hljs-keyword">var</span> reg = /^(\\d{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>})(-|\\/)(\\d{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>})\\<span class="hljs-number">2</span>(\\d{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>})$/; 
<span class="hljs-keyword">var</span> r = fieldValue.<span class="hljs-keyword">match</span>(reg);             
<span class="hljs-keyword">if</span>(r==<span class="hljs-literal">null</span>)alert(<span class="hljs-symbol">'Date</span> format error!');</code>

下面是技匠整理的,在前端开发中经常使用到的20个正则表达式。


1 . 校验密码强度

密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。

<code class="cpp">^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{<span class="hljs-number">8</span>,<span class="hljs-number">10</span>}$</code>

2. 校验中文

字符串仅能是中文。

<code class="tex">^<span class="hljs-special">[</span><span class="hljs-command">\\</span>u4e00-<span class="hljs-command">\\</span>u9fa5<span class="hljs-special">]</span><span class="hljs-special">{</span>0,<span class="hljs-special">}</span><span class="hljs-formula">$</span></code>

3. 由数字、26个英文字母或下划线组成的字符串

<code class="tex">^<span class="hljs-command">\\</span>w+<span class="hljs-formula">$</span></code>

4. 校验E-Mail 地址

同密码一样,下面是E-mail地址合规性的正则检查语句。

<code class="markdown">[<span class="hljs-link_label">\\w!#$%&amp;'*+/=?^_`{|}~-</span>]+(?:\\.[<span class="hljs-link_label">\\w!#$%&amp;'*+/=?^_`{|}~-</span>]+)<span class="hljs-emphasis">*@(?:[\\w](?:[\\w-]*</span>[<span class="hljs-link_label">\\w</span>])?\\.)+[<span class="hljs-link_label">\\w</span>](<span class="hljs-link_url">?:[\\w-]*[\\w]</span>)?</code>

5. 校验身份证号码

下面是身份证号码的正则校验。15 或 18位。

15位:

<code class="cpp">^[<span class="hljs-number">1</span>-<span class="hljs-number">9</span>]\\d{<span class="hljs-number">7</span>}((<span class="hljs-number">0</span>\\d)|(<span class="hljs-number">1</span>[<span class="hljs-number">0</span>-<span class="hljs-number">2</span>]))(([<span class="hljs-number">0</span>|<span class="hljs-number">1</span>|<span class="hljs-number">2</span>]\\d)|<span class="hljs-number">3</span>[<span class="hljs-number">0</span>-<span class="hljs-number">1</span>])\\d{<span class="hljs-number">3</span>}$</code>

18位:

<code class="cpp">^[<span class="hljs-number">1</span>-<span class="hljs-number">9</span>]\\d{<span class="hljs-number">5</span>}[<span class="hljs-number">1</span>-<span class="hljs-number">9</span>]\\d{<span class="hljs-number">3</span>}((<span class="hljs-number">0</span>\\d)|(<span class="hljs-number">1</span>[<span class="hljs-number">0</span>-<span class="hljs-number">2</span>]))(([<span class="hljs-number">0</span>|<span class="hljs-number">1</span>|<span class="hljs-number">2</span>]\\d)|<span class="hljs-number">3</span>[<span class="hljs-number">0</span>-<span class="hljs-number">1</span>])\\d{<span class="hljs-number">3</span>}([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]|X)$</code>

6. 校验日期

“yyyy-mm-dd“ 格式的日期校验,已考虑平闰年。

<code class="markdown">^(?:(?!0000)[<span class="hljs-link_label">0-9</span>]{4}-(?:(?:0[<span class="hljs-link_label">1-9</span>]|1[<span class="hljs-link_label">0-2</span>])-(?:0[<span class="hljs-link_label">1-9</span>]|1[<span class="hljs-link_label">0-9</span>]|2[<span class="hljs-link_label">0-8</span>])|(?:0[<span class="hljs-link_label">13-9</span>]|1[<span class="hljs-link_label">0-2</span>])-(?:29|30)|(?:0[<span class="hljs-link_label">13578</span>]|1[<span class="hljs-link_label">02</span>])-31)|(?:[<span class="hljs-link_label">0-9</span>]{2}(?:0[<span class="hljs-link_label">48</span>]|[<span class="hljs-link_label">2468</span>][<span class="hljs-link_reference">048</span>]|[<span class="hljs-link_label">13579</span>][<span class="hljs-link_reference">26</span>])|(?:0[<span class="hljs-link_label">48</span>]|[<span class="hljs-link_label">2468</span>][<span class="hljs-link_reference">048</span>]|[<span class="hljs-link_label">13579</span>][<span class="hljs-link_reference">26</span>])00)-02-29)$</code>

7. 校验金额

金额校验,精确到2位小数。

<code class="cpp">^[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]+(.[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]{<span class="hljs-number">2</span>})?$</code>

8. 校验手机号

下面是国内 13、15、18开头的手机号正则表达式。(可根据目前国内收集号扩展前两位开头号码)

<code class="cpp">^(<span class="hljs-number">13</span>[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]|<span class="hljs-number">14</span>[<span class="hljs-number">5</span>|<span class="hljs-number">7</span>]|<span class="hljs-number">15</span>[<span class="hljs-number">0</span>|<span class="hljs-number">1</span>|<span class="hljs-number">2</span>|<span class="hljs-number">3</span>|<span class="hljs-number">5</span>|<span class="hljs-number">6</span>|<span class="hljs-number">7</span>|<span class="hljs-number">8</span>|<span class="hljs-number">9</span>]|<span class="hljs-number">18</span>[<span class="hljs-number">0</span>|<span class="hljs-number">1</span>|<span class="hljs-number">2</span>|<span class="hljs-number">3</span>|<span class="hljs-number">5</span>|<span class="hljs-number">6</span>|<span class="hljs-number">7</span>|<span class="hljs-number">8</span>|<span class="hljs-number">9</span>])\\d{<span class="hljs-number">8</span>}$</code>

9. 判断IE的版本

IE目前还没被完全取代,很多页面还是需要做版本兼容,下面是IE版本检查的表达式。

<code class="cpp">^.*MSIE [<span class="hljs-number">5</span>-<span class="hljs-number">8</span>](?:\\.[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]+)?(?!.*Trident\\/[<span class="hljs-number">5</span>-<span class="hljs-number">9</span>]\\<span class="hljs-number">.0</span>).*$</code>

10. 校验IP-v4地址

IP4 正则语句。

<code class="markdown">\\b(?:(?:25[<span class="hljs-link_label">0-5</span>]|2[<span class="hljs-link_label">0-4</span>][<span class="hljs-link_reference">0-9</span>]|[<span class="hljs-link_label">01</span>]?[<span class="hljs-link_label">0-9</span>][<span class="hljs-link_reference">0-9</span>]?)\\.){3}(?:25[<span class="hljs-link_label">0-5</span>]|2[<span class="hljs-link_label">0-4</span>][<span class="hljs-link_reference">0-9</span>]|[<span class="hljs-link_label">01</span>]?[<span class="hljs-link_label">0-9</span>][<span class="hljs-link_reference">0-9</span>]?)\\b</code>

11. 校验IP-v6地址

IP6 正则语句。

<code class="cpp">(([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">7</span>,<span class="hljs-number">7</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">7</span>}:|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">6</span>}:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">5</span>}(:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">2</span>}|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}(:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">3</span>}|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">3</span>}(:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">2</span>}(:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">5</span>}|[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:((:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">6</span>})|:((:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">1</span>,<span class="hljs-number">7</span>}|:)|fe80:(:[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">0</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">0</span>,<span class="hljs-number">4</span>}%[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-zA-Z]{<span class="hljs-number">1</span>,}|::(ffff(:<span class="hljs-number">0</span>{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}:){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}((<span class="hljs-number">25</span>[<span class="hljs-number">0</span>-<span class="hljs-number">5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span>-<span class="hljs-number">4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>])\\.){<span class="hljs-number">3</span>,<span class="hljs-number">3</span>}(<span class="hljs-number">25</span>[<span class="hljs-number">0</span>-<span class="hljs-number">5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span>-<span class="hljs-number">4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>])|([<span class="hljs-number">0</span>-<span class="hljs-number">9</span>a-fA-F]{<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:){<span class="hljs-number">1</span>,<span class="hljs-number">4</span>}:((<span class="hljs-number">25</span>[<span class="hljs-number">0</span>-<span class="hljs-number">5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span>-<span class="hljs-number">4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>])\\.){<span class="hljs-number">3</span>,<span class="hljs-number">3</span>}(<span class="hljs-number">25</span>[<span class="hljs-number">0</span>-<span class="hljs-number">5</span>]|(<span class="hljs-number">2</span>[<span class="hljs-number">0</span>-<span class="hljs-number">4</span>]|<span class="hljs-number">1</span>{<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]){<span class="hljs-number">0</span>,<span class="hljs-number">1</span>}[<span class="hljs-number">0</span>-<span class="hljs-number">9</span>]))</code>

12. 检查URL的前缀

应用开发中很多时候需要区分请求是HTTPS还是HTTP,通过下面的表达式可以取出一个url的前缀然后再逻辑判断。

<code class="scala"><span class="hljs-keyword">if</span> (!s.<span class="hljs-keyword">match</span>(/^[a-zA-<span class="hljs-type">Z</span>]+:\\/\\<span class="hljs-comment">//))</span>
{
    s = <span class="hljs-symbol">'http</span>:<span class="hljs-comment">//' + s;</span>
}</code>

13. 提取URL链接

下面的这个表达式可以筛选出一段文本中的URL。

<code class="tex">^(f|ht)<span class="hljs-special">{</span>1<span class="hljs-special">}</span>(tp|tps):<span class="hljs-command">\\</span>/<span class="hljs-command">\\</span>/(<span class="hljs-special">[</span><span class="hljs-command">\\</span>w-<span class="hljs-special">]</span>+<span class="hljs-command">\\</span>.)+<span class="hljs-special">[</span><span class="hljs-command">\\</span>w-<span class="hljs-special">]</span>+(<span class="hljs-command">\\</span>/<span class="hljs-special">[</span><span class="hljs-command">\\</span>w- ./?<span class="hljs-comment">%&amp;=]*)?</span></code>

14. 文件路径及扩展名校验

验证windows下文件路径和扩展名(下面的例子中为.txt文件)

<code class="tex">^(<span class="hljs-special">[</span>a-zA-Z<span class="hljs-special">]</span><span class="hljs-command">\\</span>:|<span class="hljs-command">\\</span><span class="hljs-command">\\</span>)<span class="hljs-command">\\</span><span class="hljs-command">\\</span>(<span class="hljs-special">[</span>^<span class="hljs-command">\\</span><span class="hljs-command">\\</span><span class="hljs-special">]</span>+<span class="hljs-command">\\</span><span class="hljs-command">\\</span>)*<span class="hljs-special">[</span>^<span class="hljs-command">\\</span>/:*?"&lt;&gt;|<span class="hljs-special">]</span>+<span class="hljs-command">\\</span>.txt(l)?<span class="hljs-formula">$</span></code>

15. 提取Color Hex Codes

有时需要抽取网页中的颜色代码,可以使用下面的表达式。

<code class="cpp">^<span class="hljs-preprocessor">#([A-Fa-f0-<span class="hljs-number">9</span>]{<span class="hljs-number">6</span>}|[A-Fa-f0-<span class="hljs-number">9</span>]{<span class="hljs-number">3</span>})$</span></code>

16. 提取网页图片

假若你想提取网页中所有图片信息,可以利用下面的表达式。

<code class="tex"><span class="hljs-command">\\</span>&lt; *<span class="hljs-special">[</span>img<span class="hljs-special">]</span><span class="hljs-special">[</span>^<span class="hljs-command">\\</span><span class="hljs-command">\\</span>&gt;<span class="hljs-special">]</span>*<span class="hljs-special">[</span>src<span class="hljs-special">]</span> *= *<span class="hljs-special">[</span><span class="hljs-command">\\</span>"<span class="hljs-command">\\</span>'<span class="hljs-special">]</span><span class="hljs-special">{</span>0,1<span class="hljs-special">}</span>(<span class="hljs-special">[</span>^<span class="hljs-command">\\</span>"<span class="hljs-command">\\</span>'<span class="hljs-command">\\</span> &gt;<span class="hljs-special">]</span>*)</code>

17. 提取页面超链接

提取html中的超链接。

<code class="tex">(&lt;a<span class="hljs-command">\\</span>s*(?!.*<span class="hljs-command">\\</span>brel=)<span class="hljs-special">[</span>^&gt;<span class="hljs-special">]</span>*)(href="https?:<span class="hljs-command">\\</span>/<span class="hljs-command">\\</span>/)((?!(?:(?:www<span class="hljs-command">\\</span>.)?'.implode('|(?:www<span class="hljs-command">\\</span>.)?', <span class="hljs-formula">$follow_list).'))<span class="hljs-special">[</span>^"<span class="hljs-special">]</span>+)"((?!.*<span class="hljs-command">\\</span>brel=)<span class="hljs-special">[</span>^&gt;<span class="hljs-special">]</span>*)(?:<span class="hljs-special">[</span>^&gt;<span class="hljs-special">]</span>*)&gt;</span></code>

18. 查找CSS属性

通过下面的表达式,可以搜索到相匹配的CSS属性。

<code class="tex">^<span class="hljs-command">\\</span>s*<span class="hljs-special">[</span>a-zA-Z<span class="hljs-command">\\</span>-<span class="hljs-special">]</span>+<span class="hljs-command">\\</span>s*<span class="hljs-special">[</span>:<span class="hljs-special">]</span><span class="hljs-special">{</span>1<span class="hljs-special">}</span><span class="hljs-command">\\</span>s<span class="hljs-special">[</span>a-zA-Z0-9<span class="hljs-command">\\</span>s.<span class="hljs-special">#</span><span class="hljs-special">]</span>+<span class="hljs-special">[</span>;<span class="hljs-special">]</span><span class="hljs-special">{</span>1<span class="hljs-special">}</span></code>

19. 抽取注释

如果你需要移除HMTL中的注释,可以使用如下的表达式。

<code class="xml"><span class="hljs-comment">&lt;!--(.*?)--&gt;</span></code>

20. 匹配HTML标签

通过下面的表达式可以匹配出HTML中的标签属性。

<code class="tex">&lt;<span class="hljs-command">\\</span>/?<span class="hljs-command">\\</span>w+((<span class="hljs-command">\\</span>s+<span class="hljs-command">\\</span>w+(<span class="hljs-command">\\</span>s*=<span class="hljs-command">\\</span>s*(?:".*?"|'.*?'|<span class="hljs-special">[</span><span class="hljs-command">\\</span>^'"&gt;<span class="hljs-command">\\</span>s<span class="hljs-special">]</span>+))?)+<span class="hljs-command">\\</span>s*|<span class="hljs-command">\\</span>s*)<span class="hljs-command">\\</span>/?&gt;</code>

正则表达式的相关语法

下面是我找到的一张非常不错的正则表达式 Cheat Sheet,可以用来快速查找相关语法。


学习正则表达式

我在网上看到了一篇相当不错的正则表达式快速学习指南,有兴趣继续深入学习的同学可以参考。


正则表达式在线测试工具

regex101是一个非常不错的正则表达式在线测试工具,你可以直接在线测试你的正则表达式哦。


转:http://www.jianshu.com/p/e7bb97218946

Connecting to MS SQL Server from Ubuntu

And now, in a break from the previous trend of fluffy posts, we have a tutorial on how to (deep breath): connect PHP to a MSSQL Server 2008 instance over ODBC from Ubuntu Linux using the FreeTDS driver and unixODBC. Theoretically it would also work for SQL Server 2005.

I don’t know whether half of the settings flags are necessary or even correct, but what follows Worked for Me™, YMMV, etc, etc.

In the commands below, I’ll use 192.168.0.1 as the server housing the SQL Server instance, with a SQL Server user name of devuser, password devpass. I’m assuming SQL Server is set up to listen on its default port, 1433. Keep an eye out, because you’ll need to change these things to your own settings.

First, install unixODBC:

sudo apt-get install unixodbc unixodbc-dev

I also installed the following (perhaps necessary) packages:
sudo apt-get install tdsodbc php5-odbc
Then download, untar, compile, and install FreeTDS (warning, the URL may change):
cd /usr/local
wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
tar xvfz freetds-stable.tgz
cd freetds-0.82
./configure --enable-msdblib --with-tdsver=8.0 --with-unixodbc=/usr
make
make install
make clean

Attempt a connection over Telnet to your SQL Server instance:
telnet 192.168.0.1 1433

Use the tsql tool to test out the connection:
tsql -S 192.168.0.1 -U devuser

This should prompt you for the password, after which you can hope against hope to see this beautiful sign:
1>

If that worked, I recommend throwing a (coding) party. Next up is some configging. Open the FreeTDS config file.
/usr/local/etc/freetds.conf

Add the following entry to the bottom of the file. We’re setting up a datasource name (DSN) called ‘MSSQL’.
[MSSQL]
host = 192.168.0.1
port = 1433
tds version = 8.0

Now open the ODBC configuration file:
/usr/local/etc/odbcinst.ini

And add the following MSSQL driver entry (FreeTDS) at the end:
[FreeTDS]
Description = FreeTDS driver
Driver = /usr/local/lib/libtdsodbc.so
Setup=/usr/lib/odbc/libtdsS.so
FileUsage = 1
UsageCount = 1 

Then, finally, set up the DSN within ODBC in the odbc.ini file here
/usr/local/etc/odbc.ini
By adding this bit to the file:
[MSSQL]
Description = MS SQL Server
Driver = /usr/local/lib/libtdsodbc.so
Server = 192.168.2.3
UID = devuser
PWD = devpass
ReadOnly = No
Port = 1433

Test out the connection using the isql tool:
isql -v MSSQL devuser 'devpass'
If you see “Connected!” you’re golden, congratulations! If not, I’m truly sorry; see below where there are some resources that might help.

Now restart Apache and test it from PHP using ‘MSSQL’ as the DSN. If something doesn’t work, you might try installing any or all of these packages:
mdbtools libmdbodbc libmdbtools mdbtools-gmdb

Here are some other resources that were helpful to me through this disastrous journey.

Eight Terminal Utilities Every OS X Command Line User Should Know

http://www.mitchchn.me/2014/os-x-terminal/?x

The OS X Terminal opens up a world of powerful UNIX utilities and scripts. If you’re migrating from Linux, you’ll find many familiar commands work the way you expect. But power users often aren’t aware that OS X comes with a number of its own text-based utilities not found on any other operating system. Learning about these Mac-only programs can make you more productive on the command line and help you bridge the gap between UNIX and your Mac.

Update: Thanks to reader feedback, I’ve written about a few more commands in a follow-up post: (And eight hundred more).

1. open

open opens files, directories and applications. Exciting, right? But it really does come in handy as a command-line double-click. For instance, typing:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>open /<span class="hljs-constant">Applications</span>/<span class="hljs-constant">Safari</span>.app/
</code>

…will launch Safari as if you had double-clicked its icon in the Finder.

If you point open at a file instead, it will try to load the file with its associated GUI application. open screenshot.png on an image will open that image in Preview. You can set the -a flag to choose the app yourself, or -e to open the file for editing in TextEdit.

Running open on a directory will take you straight to that directory in a Finder window. This is especially useful for bringing up the current directory by typing open .

Remember that the integration between Finder and Terminal goes both ways – if you drag a file from Finder into a Terminal window, its full path gets pasted into the command line.

2. pbcopy and pbpaste

These two commands let you copy and paste text from the command line. Of course, you could also just use your mouse—but the real power of pbcopy and pbpaste comes from the fact that they’re UNIX commands, and that means they benefit from piping, redirection, and the ability to be in scripts in conjunction with other commands. Typing:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>ls ~ | pbcopy
</code>

…will copy a list of files in your home directory to the OS X clipboard. You can easily capture the contents of a file:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>pbcopy &lt; blogpost.txt
</code>

..or do something crazier. This hacked-up script will grab the link of the latest Google doodle and copy it to your clipboard.

<code class=" hljs coffeescript">$ curl <span class="hljs-attribute">http</span>:<span class="hljs-regexp">//</span>www.google.com<span class="hljs-regexp">/doodles#oodles/archive | grep -A5 'latest-doodle on' | grep 'img src' | sed s/</span>.*<span class="hljs-string">'&lt;img src="\/\/'</span><span class="hljs-regexp">/''/</span> | sed s<span class="hljs-regexp">/'" alt=".*'/</span><span class="hljs-string">''</span>/ | pbcopy
</code>

Using pbcopy with pipes is a great way to capture the output of a command without having to scroll up and carefully select it. This makes it easy to share diagnostic information. pbcopy and pbpaste can also be used to automate or speed up certain kinds of tasks. For instance, if you want to save email subject lines to a task list, you could copy the subjects from Mail.app and run:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>pbpaste &gt;&gt; tasklist.txt
</code>

3. mdfind

Many a Linux power user has tried to use locate to search for files on a Mac and then quickly discovered that it didn’t work. There’s always the venerable UNIX find command, but OS X comes with its own killer search tool: Spotlight. So why not tap into its power from the command line?

That’s exactly what mdfind does. Anything Spotlight can find, mdfind can find too. That includes the ability to search inside files and metadata.

mdfind comes with a few conveniences that make it stand out from its big blue brother. For instance, the -onlyin flag can restrict the search to a single directory:

<code class=" hljs ">$ mdfind -onlyin ~/Documents essay
</code>

The mdfind database should stay up to date in the background, but you can also troubleshoot it (as well as Spotlight) using mdutil. If Spotlight isn’t working the way it should, mdutil -E will erase the index and rebuild it from scratch. You can also turn off indexing entirely with mdutil -i off.

4. screencapture

screencapture lets you take many different kinds of screenshots. It’s similar to Grab.app and the keyboard shortcuts cmd + shift + 3 and cmd + shift + 4, except it’s far more flexible. Here are just a few different ways you can use screencapture:

Capture the contents of the screen, including the cursor, and attach the resulting image (named ‘image.png’) to a new Mail message:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>screencapture -<span class="hljs-constant">C</span> -<span class="hljs-constant">M</span> image.png
</code>

Select a window using your mouse, then capture its contents without the window’s drop shadow and copy the image to the clipboard:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>screencapture -c -<span class="hljs-constant">W</span>
</code>

Capture the screen after a delay of 10 seconds and then open the new image in Preview:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>screencapture -<span class="hljs-constant">T</span> <span class="hljs-number">10</span> -<span class="hljs-constant">P</span> image.png
</code>

Select a portion of the screen with your mouse, capture its contents, and save the image as a pdf:

<code class=" hljs bash">$ screencapture <span class="hljs-operator">-s</span> -t pdf image.pdf
</code>

To see more options, type screencapture --help

5. launchctl

launchctl lets you interact with the OS X init script system, launchd. With launch daemons and launch agents, you can control the services that start up when you boot your computer. You can even set up scripts to run periodically or at timed intervals in the background, similar to cron jobs on Linux.

For example, if you’d like to have the Apache web server start automatically when you turn on your Mac, simply type:

<code class=" hljs bash">$ <span class="hljs-built_in">sudo</span> launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
</code>

Running launchctl list will show you what launch scripts are currently loaded. sudo launchctl unload [path/to/script] will stop and unload running scripts, and adding the -w flag will remove those scripts permanently from your boot sequence. I like to run this one on all the auto-update “helpers” created by Adobe apps and Microsoft Office.

Launchd scripts are stored in the folllowing locations:

<code class=" hljs ruby">~<span class="hljs-regexp">/Library/</span><span class="hljs-constant">LaunchAgents</span>    
/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchAgents</span>          
/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchDaemons</span>
/<span class="hljs-constant">System</span>/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchAgents</span>
/<span class="hljs-constant">System</span>/<span class="hljs-constant">Library</span>/<span class="hljs-constant">LaunchDaemons</span>
</code>

To see what goes into a launch agent or daemon, there’s a great blog post by Paul Annesley that walks you through the file format. And if you’d like to learn how to write your own launchd scripts, Apple provides some helpful documentation on their Developer site. There’s also the fantastic Lingon app if you’d prefer to avoid the command line entirely.

6. say

This is a fun one: say converts text to speech, using the same TTS engine OS X uses for VoiceOver. Without any options, say will simply speak whatever text you give it out loud.:

<code class=" hljs ruby"><span class="hljs-variable">$ </span>say <span class="hljs-string">"Never trust a computer you can't lift."</span>
</code>

You can also use say to speak the contents of a text file with the -f flag, and you can store the resulting audio clip with the -o flag:

<code class=" hljs bash">$ say <span class="hljs-operator">-f</span> mynovel.txt -o myaudiobook.aiff
</code>

The say command can be useful in place of console logging or alert sounds in scripts. For instance, you can set up an Automator or Hazel script to do batch file processing and then announce the task’s completion with say.

But the most enjoyable use for say is rather more sinister: if you have ssh access to a friend or coworker’s Mac, you can silently log into their machine and haunt them through the command line. Give ‘em a Siri-ous surprise.

You can set the voice (and language!) used by say by changing the default setting in the Dictation & Speech panel in System Preferences.

7. diskutil

diskutil is a command line interface to the Disk Utility app that comes with OS X. It can do everything its graphical cousin can, but it also has some extra capabilities—such as filling a disk with zeroes or random data. Simply type diskutil list to see the path names of disks and removable media attached to your machine, and then point the command at the volume you want to operate on. Be careful: diskutil can permanently destroy data if it’s used incorrectly.

8. brew

Alright–this isn’t technically a native command. But no OS X power user should be without Homebrew. The website calls it “The missing package manager for OS X,” and that couldn’t be truer. If you’ve ever used apt-get in Linux, you will feel right at home in Homebrew.

brew gives you easy access to thousands of free utilities and libraries from the open source community. For instance, brew install imagemagick will set you up with ImageMagick, a powerful utility that makes it possible to do anything from whipping up animated gifs to converting images between dozens of different types. brew install node will introduce you to NodeJS, the hot new tool for developing and running server-side JavaScript apps.

You can have fun with Homebrew too: brew install archey will get you Archey, a cool little script for displaying your Mac’s specs next to a colourful Apple logo. The selection in Homebrew is huge—and because it’s so easy to create formulas, new packages are being added all the time.


ARCHEY—My command line brings all the boys to the yard.

But the best part about Homebrew? It keeps all its files in a single directory: /usr/local/. That means you can install newer versions of system software, such as python and mysql, without interfering with the built-in equivalents. And if you ever want to get rid of your Homebrew installation, it’s easy to remove.

Edit—May 15: Someone suggested to me that that you should not blindly delete the contents of /usr/local/. That’s a fair precaution. To remove Homebrew safely, use the uninstall script.

For more fun with Terminal.app, here is an A-Z list of all available console commands in OS X 10.9 Mavericks.

80 Linux Monitoring Tools for SysAdmins

By Jonathan Sundqvist, Engineer at Server Density.
Published on the 4th February, 2015.

 

The industry is hotting up at the moment, and there are more tools than you can shake a stick at. Here lies the most comprehensive list on the Internet (of monitoring tools). Featuring over 80 ways to monitor your machines. Within this article we outline:

  • Command line tools
  • Network related
  • System related monitoring
  • Log monitoring tools
  • Infrastructure monitoring tools

It’s hard work monitoring and debugging performance problems, but it’s easier with the right tools at the right time. But how much of your valuable time do you think it would take you to investigate all of these tools and find out which one is best for you?

Why not check out Server Density first, it has a beautiful UI, an api that’s easy to use and alerts that will keep downtime to a minimum.

server-monitoring-tool

Top 10  System Monitoring Tools

1. Top

Top
This is a small tool which is pre-installed in many unix systems. When you want an overview of all the processes or threads running in the system: top is a good tool. You can order these processes on different criteria and the default criteria is CPU.

2. htop

Top
Htop is essentially an enhanced version of top. It’s easier to sort by processes. It’s visually easier to understand and has built in commands for common things you would like to do. Plus it’s fully interactive.

3. atop

Atop monitors all processes much like top and htop, unlike top and htop however it has daily logging of the processes for long-term analysis. It also shows resource consumption by all processes. It will also highlight resources that have reached a critical load.

4. apachetop

Apachetop monitors the overall performance of your apache webserver. It’s largely based on mytop. It displays current number of reads, writes and the overall number of requests processed.

5. ftptop

ftptop gives you basic information of all the current ftp connections to your server such as the total amount of sessions, how many are uploading and downloading and who the client is.

6. mytop

Top
mytop is a neat tool for monitoring threads and performance of mysql. It gives you a live look into the database and what queries it’s processing in real time.

7. powertop

Top
powertop helps you diagnose issues that has to do with power consumption and power management. It can also help you experiment with power management settings to achieve the most efficient settings for your server. You switch tabs with the tab key.

8. iotop

Top
iotop checks the I/O usage information and gives you a top-like interface to that. It displays columns on read and write and each row represents a process. It also displays the percentage of time the process spent while swapping in and while waiting on I/O.

Network related monitoring

9. ntopng

Top
ntopng is the next generation of ntop and the tool provides a graphical user interface via the browser for network monitoring. It can do stuff such as: geolocate hosts, get network traffic and show ip traffic distribution and analyze it.

10. iftop

Top
iftop is similar to top, but instead of mainly checking for cpu usage it listens to network traffic on selected network interfaces and displays a table of current usage. It can be handy for answering questions such as “Why on earth is my internet connection so slow?!”.

11. jnettop

Top
jnettop visualises network traffic in much the same way as iftop does. It also supports customizable text output and a machine-friendly mode to support further analysis.

12. bandwidthd

Top
BandwidthD tracks usage of TCP/IP network subnets and visualises that in the browser by building a html page with graphs in png. There is a database driven system that supports searching, filtering, multiple sensors and custom reports.

13. EtherApe

EtherApe displays network traffic graphically, the more talkative the bigger the node. It either captures live traffic or can read it from a tcpdump. The displayed can also be refined using a network filter with pcap syntax.

14. ethtool

Top
ethtool is used for displaying and modifying some parameters of the network interface controllers. It can also be used to diagnose Ethernet devices and get more statistics from the devices.

15. NetHogs

Top
NetHogs breaks down network traffic per protocol or per subnet. It then groups by process. So if there’s a surge in network traffic you can fire up NetHogs and see which process is causing it.

16. iptraf

Top
iptraf gathers a variety of metrics such as TCP connection packet and byte count, interface statistics and activity indicators, TCP/UDP traffic breakdowns and station packet and byte counts.

17. ngrep

Top
ngrep is grep but for the network layer. It’s pcap aware and will allow to specify extended regular or hexadecimal expressions to match against packets of .

18. MRTG

Top
MRTG was orginally developed to monitor router traffic, but now it’s able to monitor other network related things as well. It typically collects every five minutes and then generates a html page. It also has the capability of sending warning emails.

19. bmon

Top
Bmon monitors and helps you debug networks. It captures network related statistics and presents it in human friendly way. You can also interact with bmon through curses or through scripting.

20. traceroute

Top
Traceroute is a built-in tool for displaying the route and measuring the delay of packets across a network.

21. IPTState

IPTState allows you to watch where traffic that crosses your iptables is going and then sort that by different criteria as you please. The tool also allows you to delete states from the table.

22. darkstat

Top
Darkstat captures network traffic and calculates statistics about usage. The reports are served over a simple HTTP server and gives you a nice graphical user interface of the graphs.

23. vnStat

Top
vnStat is a network traffic monitor that uses statistics provided by the kernel which ensures light use of system resources. The gathered statistics persists through system reboots. It has color options for the artistic sysadmins.

24. netstat

Top
Netstat is a built-in tool that displays TCP network connections, routing tables and a number of network interfaces. It’s used to find problems in the network.

25. ss

Instead of using netstat, it’s however preferable to use ss. The ss command is capable of showing more information than netstat and is actually faster. If you want a summary statistics you can use the command ss -s.

26. nmap

Top
Nmap allows you to scan your server for open ports or detect which OS is being used. But you could also use this for SQL injection vulnerabilities, network discovery and other means related to penetration testing.

27. MTR


MTR combines the functionality of traceroute and the ping tool into a single network diagnostic tool. When using the tool it will limit the number hops individual packets has to travel while also listening to their expiry. It then repeats this every second.

28. Tcpdump


Tcpdump will output a description of the contents of the packet it just captured which matches the expression that you provided in the command. You can also save the this data for further analysis.

29. Justniffer


Justniffer is a tcp packet sniffer. You can choose whether you would like to collect low-level data or high-level data with this sniffer. It also allows you to generate logs in customizable way. You could for instance mimic the access log that apache has.

System related monitoring

30. nmon


nmon either outputs the data on screen or saves it in a comma separated file. You can display CPU, memory, network, filesystems, top processes. The data can also be added to a RRD database for further analysis.

31. conky


Conky monitors a plethora of different OS stats. It has support for IMAP and POP3 and even support for many popular music players! For the handy person you could extend it with your own scripts or programs using Lua.

32. Glances


Glances monitors your system and aims to present a maximum amount of information in a minimum amount of space. It has the capability to function in a client/server mode as well as monitoring remotely. It also has a web interface.

33. saidar


Saidar is a very small tool that gives you basic information about your system resources. It displays a full screen of the standard system resources. The emphasis for saidar is being as simple as possible.

34. RRDtool


RRDtool is a tool developed to handle round-robin databases or RRD. RRD aims to handle time-series data like CPU load, temperatures etc. This tool provides a way to extract RRD data in a graphical format.

35. monit


Monit has the capability of sending you alerts as well as restarting services if they run into trouble. It’s possible to perform any type of check you could write a script for with monit and it has a web user interface to ease your eyes.

36. Linux process explorer

linux-process-monitor
Linux process explorer is akin to the activity monitor for OSX or the windows equivalent. It aims to be more usable than top or ps. You can view each process and see how much memory usage or CPU it uses.

37. df


df is an abbreviation for disk free and is pre-installed program in all unix systems used to display the amount of available disk space for filesystems which the user have access to.

38. discus


Discus is similar to df however it aims to improve df by making it prettier using fancy features as colors, graphs and smart formatting of numbers.

39. xosview


xosview is a classic system monitoring tool and it gives you a simple overview of all the different parts of the including IRQ.

40. Dstat


Dstat aims to be a replacement for vmstat, iostat, netstat and ifstat. It allows you to view all of your system resources in real-time. The data can then be exported into csv. Most importantly dstat allows for plugins and could thus be extended into areas not yet known to mankind.

41. Net-SNMP

SNMP is the protocol ‘simple network management protocol’ and the Net-SNMP tool suite helps you collect accurate information about your servers using this protocol.

42. incron

Incron allows you to monitor a directory tree and then take action on those changes. If you wanted to copy files to directory ‘b’ once new files appeared in directory ‘a’ that’s exactly what incron does.

43. monitorix

Monitorix is lightweight system monitoring tool. It helps you monitor a single machine and gives you a wealth of metrics. It also has a built-in HTTP server to view graphs and a reporting mechanism of all metrics.

44. vmstat


vmstat or virtual memory statistics is a small built-in tool that monitors and displays a summary about the memory in the machine.

45. uptime

This small command that quickly gives you information about how long the machine has been running, how many users currently are logged on and the system load average for the past 1, 5 and 15 minutes.

46. mpstat


mpstat is a built-in tool that monitors cpu usage. The most common command is using mpstat -P ALL which gives you the usage of all the cores. You can also get an interval update of the CPU usage.

47. pmap


pmap is a built-in tool that reports the memory map of a process. You can use this command to find out causes of memory bottlenecks.

48. ps


The ps command will give you an overview of all the current processes. You can easily select all processes using the command ps -A

49. sar


sar is a part of the sysstat package and helps you to collect, report and save different system metrics. With different commands it will give you CPU, memory and I/O usage among other things.

50. collectl


Similar to sar collectl collects performance metrics for your machine. By default it shows cpu, network and disk stats but it collects a lot more. The difference to sar is collectl is able to deal with times below 1 second, it can be fed into a plotting tool directly and collectl monitors processes more extensively.

51. iostat


iostat is also part of the sysstat package. This command is used for monitoring system input/output. The reports themselves can be used to change system configurations to better balance input/output load between hard drives in your machine.

52. free


This is a built-in command that displays the total amount of free and used physical memory on your machine. It also displays the buffers used by the kernel at that given moment.

53. /Proc file system


The proc file system gives you a peek into kernel statistics. From these statistics you can get detailed information about the different hardware devices on your machine. Take a look at the full list of the proc file statistics

54. GKrellM

GKrellm is a gui application that monitor the status of your hardware such CPU, main memory, hard disks, network interfaces and many other things. It can also monitor and launch a mail reader of your choice.

55. Gnome system monitor


Gnome system monitor is a basic system monitoring tool that has features looking at process dependencies from a tree view, kill or renice processes and graphs of all server metrics.

Log monitoring tools

56. GoAccess


GoAccess is a real-time web log analyzer which analyzes the access log from either apache, nginx or amazon cloudfront. It’s also possible to output the data into HTML, JSON or CSV. It will give you general statistics, top visitors, 404s, geolocation and many other things.

57. Logwatch

Logwatch is a log analysis system. It parses through your system’s logs and creates a report analyzing the areas that you specify. It can give you daily reports with short digests of the activities taking place on your machine.

58. Swatch


Much like Logwatch Swatch also monitors your logs, but instead of giving reports it watches for regular expression and notifies you via mail or the console when there is a match. It could be used for intruder detection for example.

59. MultiTail


MultiTail helps you monitor logfiles in multiple windows. You can merge two or more of these logfiles into one. It will also use colors to display the logfiles for easier reading with the help of regular expressions.

System tools

60. acct or psacct

acct or psacct (depending on if you use apt-get or yum) allows you to monitor all the commands a users executes inside the system including CPU and memory time. Once installed you get that summary with the command ‘sa’.

61. whowatch

Similar to acct this tool monitors users on your system and allows you to see in real time what commands and processes they are using. It gives you a tree structure of all the processes and so you can see exactly what’s happening.

62. strace


strace is used to diagnose, debug and monitor interactions between processes. The most common thing to do is making strace print a list of system calls made by the program which is useful if the program does not behave as expected.

63. DTrace


DTrace is the big brother of strace. It dynamically patches live running instructions with instrumentation code. This allows you to do in-depth performance analysis and troubleshooting. However, it’s not for the weak of heart as there is a 1200 book written on the topic.

64. webmin


Webmin is a web-based system administration tool. It removes the need to manually edit unix configuration files and lets you manage the system remotely if need be. It has a couple of monitoring modules that you can attach to it.

65. stat


Stat is a built-in tool for displaying status information of files and file systems. It will give you information such as when the file was modified, accessed or changed.

66. ifconfig


ifconfig is a built-in tool used to configure the network interfaces. Behind the scenes network monitor tools use ifconfig to set it into promiscuous mode to capture all packets. You can do it yourself with ifconfig eth0 promiscand return to normal mode with ifconfig eth0 -promisc.

67. ulimit


ulimit is a built-in tool that monitors system resources and keeps a limit so any of the monitored resources don’t go overboard. For instance making a fork bomb where a properly configured ulimit is in place would be totally fine.

68. cpulimit

CPUlimit is a small tool that monitors and then limits the CPU usage of a process. It’s particularly useful to make batch jobs not eat up too many CPU cycles.

69. lshw


lshw is a small built-in tool extract detailed information about the hardware configuration of the machine. It can output everything from CPU version and speed to mainboard configuration.

70. w

W is a built-in command that displays information about the users currently using the machine and their processes.

71. lsof


lsof is a built-in tool that gives you a list of all open files and network connections. From there you can narrow it down to files opened by processes, based on the process name, by a specific user or perhaps kill all processes that belongs to a specific user.

Infrastructure monitoring tools

72. Server Density


Our linux monitoring tool! It has a web interface that allows you to set alerts and view graphs for all system and network metrics. You can also set up monitoring of websites whether they are up or down. Server Density allows you to set permissions for users and you can extend your monitoring with our plugin infrastructure or api. The service already supports Nagios plugins.

73. OpenNMS


OpenNMS has four main functional areas: event management and notifications; discovery and provisioning; service monitoring and data collection. It’s designed to be customizable to work in a variety of network environments.

74. SysUsage


SysUsage monitors your system continuously via Sar and other system commands. It also allows notifications to alarm you once a threshold is reached. SysUsage itself can be run from a centralized place where all the collected statistics are also being stored. It has a web interface where you can view all the stats.

75. brainypdm


brainypdm is a data management and monitoring tool that has the capability to gather data from nagios or another generic source to make graphs. It’s cross-platform, has custom graphs and is web based.

76. PCP


PCP has the capability of collating metrics from multiple hosts and does so efficiently. It also has a plugin framework so you can make it collect specific metrics that is important to you. You can access graph data through either a web interface or a GUI. Good for monitoring large systems.

77. KDE system guard


This tool is both a system monitor and task manager. You can view server metrics from several machines through the worksheet and if a process needs to be killed or if you need to start a process it can be done within KDE system guard.

78. Munin


Munin is both a network and a system monitoring tool which offers alerts for when metrics go beyond a given threshold. It uses RRDtool to create the graphs and it has web interface to display these graphs. Its emphasis is on plug and play capabilities with a number of plugins available.

79. Nagios


Nagios is system and network monitoring tool that helps you monitor monitor your many servers. It has support for alerting for when things go wrong. It also has many plugins written for the platform.

80. Zenoss


Zenoss provides a web interface that allows you to monitor all system and network metrics. Moreover it discovers network resources and changes in network configurations. It has alerts for you to take action on and it supports the Nagios plugins.

81. Cacti


(And one for luck!) Cacti is network graphing solution that uses the RRDtool data storage. It allows a user to poll services at predetermined intervals and graph the result. Cacti can be extended to monitor a source of your choice through shell scripts.

82. Zabbix

Zabbix Monitoring
Zabbix is an open source infrastructure monitoring solution. It can use most databases out there to store the monitoring statistics. The Core is written in C and has a frontend in PHP. If you don’t like installing an agent, Zabbix might be an option for you.

Bonus section:

Thanks for your suggestions. It’s an oversight on our part that we’ll have to go back trough and renumber all the headings. In light of that, here’s a short section at the end for some of the Linux monitoring tools recommended by you:

83. collectd

Collectd is a Unix daemon that collects all your monitoring statistics. It uses a modular design and plugins to fill in any niche monitoring. This way collectd stays as lightweight and customizable as possible.

84. Observium

Observium is an auto-discovering network monitoring platform supporting a wide range of hardware platforms and operating systems. Observium focuses on providing a beautiful and powerful yet simple and intuitive interface to the health and status of your network.

85. Nload

It’s a command line tool that monitors network throughput. It’s neat because it visualizes the in and and outgoing traffic using two graphs and some additional useful data like total amount of transferred data. You can install it with

or

84. SmokePing

SmokePing keeps track of the network latencies of your network and it visualises them too. There are a wide range of latency measurement plugins developed for SmokePing. If a GUI is important to you it’s there is an ongoing development to make that happen.

85. MobaXterm

If you’re working in windows environment day in and day out. You may feel limited by the terminal Windows provides. MobaXterm comes to the rescue and allows you to use many of the terminal commands commonly found in Linux. Which will help you tremendously in your monitoring needs!

86. Shinken monitoring

Shinken is a monitoring framework which is a total rewrite of Nagios in python. It aims to enhance flexibility and managing a large environment. While still keeping all your nagios configuration and plugins.

 

10个惊艳的Swift单行代码

http://www.codeceo.com/article/10-swift-oneline-code.html

1 数组中的每个元素乘以2

特别简单,尤其是使用map解决的话。

(1...1024).map{$0 * 2}

2 数组中的元素求和

虽然这里使用reduce和加号运算符,借助了加号运算符是函数这样一个事实,但解决办法是显而易见的,我们可以看到 reduce更具创意的用法。

(1...1024).reduce(0,combine: +)

3 验证在字符串中是否存在指定单词

让我们使用 filter来验证tweet中是否包含选定的若干关键字中的一个:

let words = ["Swift","iOS","cocoa","OSX","tvOS"]
let tweet = "This is an example tweet larking about Swift"

let valid = !words.filter({tweet.containsString($0)}).isEmpty
valid //true

更新:@oisdk提出一些更好的选择:

words.contains(tweet.containsString)

方式更简洁,还有这一个:

tweet.characters
  .split(" ")
  .lazy
  .map(String.init)
  .contains(Set(words).contains)

4 读取文件

像其他语言一样,通过简单的内置来读取文件到数组中是不可能,但我们可以结合使用 split 和 map创造一些不需要for循环的简短代码:

let path = NSBundle.mainBundle().pathForResource("test", ofType: "txt")

let lines = try? String(contentsOfFile: path!).characters.split{$0 == "\n"}.map(String.init)
if let lines=lines {
    lines[0] // O! for a Muse of fire, that would ascend
    lines[1] // The brightest heaven of invention!
    lines[2] // A kingdom for a stage, princes to act
    lines[3] // And monarchs to behold the swelling scene.
}

map和字符串构造函数的最后一步把我们的数组字符转换为字符串。

5 祝你生日快乐!

这将显示生日快乐歌到控制台,通过map以及范围和三元运算符的简单使用。

let name = "uraimo"
(1...4).forEach{print("Happy Birthday " + (($0 == 3) ? "dear \(name)":"to You"))}

6 过滤数组中的数字

在这种情况下,我们需要使用提供的过滤函数分区一个序列。许多语言除了拥有常用的map、flatMap、reduce、filter等,还有正好能做这件事的 partitionBy 函数,Swift如你所知没有类似的东西(NSPredicate提供的可以过滤的NSArray函数在这里不是我们所需要的)。

因此,我们可以用 partitionBy 函数扩展 SequenceType 来解决这个问题,我们将使用 partitionBy 函数来分区整型数组:

extension SequenceType{
    typealias Element = Self.Generator.Element

    func partitionBy(fu: (Element)-&gt;Bool)-&gt;([Element],[Element]){
        var first=[Element]()
        var second=[Element]()
        for el in self {
            if fu(el) {
                first.append(el)
            }else{
                second.append(el)
            }
        }
        return (first,second)
    }
}

let part = [82, 58, 76, 49, 88, 90].partitionBy{$0 &lt; 60}
part // ([58, 49], [82, 76, 88, 90])

不是真正的单行代码。那么,我们是否可以使用过滤器来改善它?

extension SequenceType{

    func anotherPartitionBy(fu: (Self.Generator.Element)-&gt;Bool)-&gt;([Self.Generator.Element],[Self.Generator.Element]){
        return (self.filter(fu),self.filter({!fu($0)}))
    }
}

let part2 = [82, 58, 76, 49, 88, 90].anotherPartitionBy{$0 &lt; 60}
part2 // ([58, 49], [82, 76, 88, 90])

稍微好了一点,但它遍历了序列两次,并且试图把它变成单行代码删除闭包功能将会导致太多重复的东西(过滤函数和数组会在两个地方使用)。

我们是否使用单个数据流建立一些能够将初始序列转换为分区元组的东西?是的,我们可以用 reduce

var part3 = [82, 58, 76, 49, 88, 90].reduce( ([],[]), combine: {
	(a:([Int],[Int]),n:Int) -&gt; ([Int],[Int]) in
	(n&lt;60) ? (a.0+[n],a.1) : (a.0,a.1+[n]) 
})
part3 // ([58, 49], [82, 76, 88, 90])

我们在这里构建了包含两个分区的结果元组,一次一个元素,使用过滤函数测试初始序列中的每个元素,并根据过滤结果追加该元素到第一或第二分区数组中。

最后得到真正的单行代码,但要注意这样一个事实,即分区数组通过追加被构建,实际上会使其比前两个实施方式要慢。

7 获取并解析XML Web服务

上面的有些语言不依赖外部库,并默认提供多个选项来处理XML(例如Scala虽然笨拙但“本地”地支持XML解析成对象),但Foundation只提供了SAX解析器NSXMLParser,并且正如你可能已经猜到的那样,我们不打算使用它。

有几个替代的开源库,我们可以在这种情况下使用,其中一些用C或Objective-C编写,其他为纯Swift。

这次,我们打算使用纯Swift的AEXML:

let xmlDoc = try? AEXMLDocument(xmlData: NSData(contentsOfURL: NSURL(string:"https://www.ibiblio.org/xml/examples/shakespeare/hen_v.xml")!)!)

if let xmlDoc=xmlDoc {
    var prologue = xmlDoc.root.children[6]["PROLOGUE"]["SPEECH"]
    prologue.children[1].stringValue // Now all the youth of England are on fire,
    prologue.children[2].stringValue // And silken dalliance in the wardrobe lies:
    prologue.children[3].stringValue // Now thrive the armourers, and honour's thought
    prologue.children[4].stringValue // Reigns solely in the breast of every man:
    prologue.children[5].stringValue // They sell the pasture now to buy the horse,
}

8 在数组中查找最小(或最大)值

我们有各种方法来找到序列中的最小和最大值,其中有 minElement 和maxElement 函数:

//Find the minimum of an array of Ints
[10,-22,753,55,137,-1,-279,1034,77].sort().first
[10,-22,753,55,137,-1,-279,1034,77].reduce(Int.max, combine: min)
[10,-22,753,55,137,-1,-279,1034,77].minElement()

//Find the maximum of an array of Ints
[10,-22,753,55,137,-1,-279,1034,77].sort().last
[10,-22,753,55,137,-1,-279,1034,77].reduce(Int.min, combine: max)
[10,-22,753,55,137,-1,-279,1034,77].maxElement()

9 并行处理

某些语言允许用一种简单和透明的方式启用数组对功能,例如map和flatMap的并行处理,以加快顺序和独立操作的执行。

此功能Swift中还不可用,但可以使用GCD构建:http://moreindirection.blogspot.it/2015/07/gcd-and-parallel-collections-in-swift.html

10 埃拉托斯特尼筛法

埃拉托斯特尼筛法用于查找所有的素数直到给定的上限n。

从小于n的所有整数序列开始,算法删除所有整数的倍数,直到只剩下素数。并且为了加快执行速度,我们实际上并不需要检查每个整数的倍数,我们止步于n的平方根就可以了。

根据这一定义首次执行可能是这样的:

var n = 50
var primes = Set(2...n)

(2...Int(sqrt(Double(n)))).forEach{primes.subtractInPlace((2*$0).stride(through:n, by:$0))}
primes.sort()

我们使用外部范围来迭代我们要检查的整数,并且对于每一个整数我们使用 stride(through:Int by:Int)计算出数字的倍数的序列。那些序列然后从Set中减去,Set用所有从2到n的整数初始化。

但正如你所看到的,为了实际移除倍数,我们使用外部可变Set,导致了附带后果。

为了消除附带后果,正如我们通常应该做的那样,我们会先计算所有序列,用倍数的单一数组来flatMap它们,并从初始Set中删除这些整数。

var sameprimes = Set(2...n)

sameprimes.subtractInPlace((2...Int(sqrt(Double(n))))
						   .flatMap{ (2*$0).stride(through:n, by:$0)})
sameprimes.sort()

方式更清洁,使用flatMap的一个很好的例子以生成扁平化的嵌套数组。

11其他:通过解构元组交换

最后一点,并非每个人都知道的是,和其他有tuple类型的语言一样,元组可以用来执行紧凑的变量交换:

var a=1,b=2

(a,b) = (b,a)
a //2
b //1

好了,正如所料,Swift和其他语言一样富有表现力。

OVER(PARTITION BY)函数介绍

原文:http://www.cnblogs.com/lanzi/archive/2010/10/26/1861338.html
开窗函数
Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行。

开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
1:over后的写法:
over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数
over(partition by deptno)按照部门分区

 

   over(partition by deptno order by salary)

 

2:开窗的窗口范围:
over(order by salary range between 5 preceding and 5 following):窗口范围为当前行数据幅度减5加5后的范围内的。

举例:

 

–sum(s)over(order by s range between 2 preceding and 2 following) 表示加2或2的范围内的求和

select name,class,s, sum(s)over(order by s range between 2 preceding and 2 following) mm from t2
adf        3        45        45  –45加2减2即43到47,但是s在这个范围内只有45
asdf       3        55        55
cfe        2        74        74
3dd        3        78        158 –78在76到80范围内有78,80,求和得158
fda        1        80        158
gds        2        92        92
ffd        1        95        190
dss        1        95        190
ddd        3        99        198
gf         3        99        198

 

 

 

over(order by salary rows between 5 preceding and 5 following):窗口范围为当前行前后各移动5行。

举例:

 

–sum(s)over(order by s rows between 2 preceding and 2 following)表示在上下两行之间的范围内
select name,class,s, sum(s)over(order by s rows between 2 preceding and 2 following) mm from t2
adf        3        45        174  (45+55+74=174)
asdf       3        55        252   (45+55+74+78=252)
cfe        2        74        332    (74+55+45+78+80=332)
3dd        3        78        379    (78+74+55+80+92=379)
fda        1        80        419
gds        2        92        440
ffd        1        95        461
dss        1        95        480
ddd        3        99        388
gf         3        99        293

 

 

over(order by salary range between unbounded preceding and unbounded following)或者

over(order by salary rows between unbounded preceding and unbounded following):窗口不做限制

 

3、与over函数结合的几个函数介绍

row_number()over()、rank()over()和dense_rank()over()函数的使用

下面以班级成绩表t2来说明其应用

t2表信息如下:
cfe        2        74
dss        1        95
ffd        1        95
fda        1        80
gds        2        92
gf         3        99
ddd        3        99
adf        3        45
asdf       3        55
3dd        3        78

select * from
(
select name,class,s,rank()over(partition by class order by s desc) mm from t2
)
where mm=1;
得到的结果是:
dss        1        95        1
ffd        1        95        1
gds        2        92        1
gf         3        99        1
ddd        3        99        1

注意:
1.在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,row_number()只返回一个结果;
select * from
(
select name,class,s,row_number()over(partition by class order by s desc) mm from t2
)
where mm=1;
1        95        1  –95有两名但是只显示一个
2        92        1
3        99        1 –99有两名但也只显示一个

2.rank()和dense_rank()可以将所有的都查找出来:
如上可以看到采用rank可以将并列第一名的都查找出来;
rank()和dense_rank()区别:
–rank()是跳跃排序,有两个第二名时接下来就是第四名;
select name,class,s,rank()over(partition by class order by s desc) mm from t2
dss        1        95        1
ffd        1        95        1
fda        1        80        3 –直接就跳到了第三
gds        2        92        1
cfe        2        74        2
gf         3        99        1
ddd        3        99        1
3dd        3        78        3
asdf       3        55        4
adf        3        45        5
–dense_rank()l是连续排序,有两个第二名时仍然跟着第三名
select name,class,s,dense_rank()over(partition by class order by s desc) mm from t2
dss        1        95        1
ffd        1        95        1
fda        1        80        2 –连续排序(仍为2)
gds        2        92        1
cfe        2        74        2
gf         3        99        1
ddd        3        99        1
3dd        3        78        2
asdf       3        55        3
adf        3        45        4

–sum()over()的使用
select name,class,s, sum(s)over(partition by class order by s desc) mm from t2 –根据班级进行分数求和
dss        1        95        190  –由于两个95都是第一名,所以累加时是两个第一名的相加
ffd        1        95        190
fda        1        80        270  –第一名加上第二名的
gds        2        92        92
cfe        2        74        166
gf         3        99        198
ddd        3        99        198
3dd        3        78        276
asdf       3        55        331
adf        3        45        376

first_value() over()和last_value() over()的使用   


–找出这三条电路每条电路的第一条记录类型和最后一条记录类型

SELECT opr_id,res_type,
first_value(res_type) over(PARTITION BY opr_id ORDER BY res_type) low,
last_value(res_type) over(PARTITION BY opr_id ORDER BY res_type rows BETWEEN unbounded preceding AND unbounded following) high
FROM rm_circuit_route
WHERE opr_id IN (‘000100190000000000021311′,’000100190000000000021355′,’000100190000000000021339’)
ORDER BY opr_id;

注:rows BETWEEN unbounded preceding AND unbounded following 的使用

–取last_value时不使用rows BETWEEN unbounded preceding AND unbounded following的结果

 

SELECT opr_id,res_type,
first_value(res_type) over(PARTITION BY opr_id ORDER BY res_type) low,
last_value(res_type) over(PARTITION BY opr_id ORDER BY res_type) high
FROM rm_circuit_route
WHERE opr_id IN (‘000100190000000000021311′,’000100190000000000021355′,’000100190000000000021339’)
ORDER BY opr_id;

如下图可以看到,如果不使用

rows BETWEEN unbounded preceding AND unbounded following,取出的last_value由于与res_type进行进行排列,因此取出的电路的最后一行记录的类型就不是按照电路的范围提取了,而是以res_type为范围进行提取了。

 

 

 

 

 

在first_value和last_value中ignore nulls的使用

数据如下:

 

取出该电路的第一条记录,加上ignore nulls后,如果第一条是判断的那个字段是空的,则默认取下一条,结果如下所示:

–lag() over()函数用法(取出前n行数据)
lag(expresstion,<offset>,<default>)
with a as
(select 1 id,’a’ name from dual
union
select 2 id,’b’ name from dual
union
select 3 id,’c’ name from dual
union
select 4 id,’d’ name from dual
union
select 5 id,’e’ name from dual
)
select id,name,lag(id,1,”)over(order by name) from a;

–lead() over()函数用法(取出后N行数据)

lead(expresstion,<offset>,<default>)
with a as
(select 1 id,’a’ name from dual
union
select 2 id,’b’ name from dual
union
select 3 id,’c’ name from dual
union
select 4 id,’d’ name from dual
union
select 5 id,’e’ name from dual
)
select id,name,lead(id,1,”)over(order by name) from a;

–ratio_to_report(a)函数用法 Ratio_to_report() 括号中就是分子,over() 括号中就是分母
with a as (select 1 a from dual
union all
select 1 a from dual
union  all
select 1 a from dual
union all
select 2 a from dual
union all
select 3 a from dual
union all
select 4 a from dual
union all
select 4 a from dual
union all
select 5 a from dual
)
select a, ratio_to_report(a)over(partition by a) b from a
order by a;

with a as (select 1 a from dual
union all
select 1 a from dual
union  all
select 1 a from dual
union all
select 2 a from dual
union all
select 3 a from dual
union all
select 4 a from dual
union all
select 4 a from dual
union all
select 5 a from dual
)
select a, ratio_to_report(a)over() b from a –分母缺省就是整个占比
order by a;

with a as (select 1 a from dual
union all
select 1 a from dual
union  all
select 1 a from dual
union all
select 2 a from dual
union all
select 3 a from dual
union all
select 4 a from dual
union all
select 4 a from dual
union all
select 5 a from dual
)
select a, ratio_to_report(a)over() b from a
group by a order by a;–分组后的占比

 

percent_rank用法
计算方法:所在组排名序号-1除以该组所有的行数-1,如下所示自己计算的pr1与通过percent_rank函数得到的值是一样的:
SELECT a.deptno,
a.ename,
a.sal,
a.r,
b.n,
(a.r-1)/(n-1) pr1,
percent_rank() over(PARTITION BY a.deptno ORDER BY a.sal) pr2
FROM (SELECT deptno,
ename,
sal,
rank() over(PARTITION BY deptno ORDER BY sal) r –计算出在组中的排名序号
FROM emp
ORDER BY deptno, sal) a,
(SELECT deptno, COUNT(1) n FROM emp GROUP BY deptno) b –按部门计算每个部门的所有成员数
WHERE a.deptno = b.deptno;

cume_dist函数
计算方法:所在组排名序号除以该组所有的行数,但是如果存在并列情况,则需加上并列的个数-1,
如下所示自己计算的pr1与通过percent_rank函数得到的值是一样的:
SELECT a.deptno,
a.ename,
a.sal,
a.r,
b.n,
c.rn,
(a.r + c.rn – 1) / n pr1,
cume_dist() over(PARTITION BY a.deptno ORDER BY a.sal) pr2
FROM (SELECT deptno,
ename,
sal,
rank() over(PARTITION BY deptno ORDER BY sal) r
FROM emp
ORDER BY deptno, sal) a,
(SELECT deptno, COUNT(1) n FROM emp GROUP BY deptno) b,
(SELECT deptno, r, COUNT(1) rn,sal
FROM (SELECT deptno,sal,
rank() over(PARTITION BY deptno ORDER BY sal) r
FROM emp)
GROUP BY deptno, r,sal
ORDER BY deptno) c –c表就是为了得到每个部门员工工资的一样的个数
WHERE a.deptno = b.deptno
AND a.deptno = c.deptno(+)
AND a.sal = c.sal;

percentile_cont函数
含义:输入一个百分比(该百分比就是按照percent_rank函数计算的值),返回该百分比位置的平均值
如下,输入百分比为0.7,因为0.7介于0.6和0.8之间,因此返回的结果就是0.6对应的sal的1500加上0.8对应的sal的1600平均
SELECT ename,
sal,
deptno,
percentile_cont(0.7) within GROUP(ORDER BY sal) over(PARTITION BY deptno) “Percentile_Cont”,
percent_rank() over(PARTITION BY deptno ORDER BY sal) “Percent_Rank”
FROM emp
WHERE deptno IN (30, 60);

若输入的百分比为0.6,则直接0.6对应的sal值,即1500
SELECT ename,
sal,
deptno,
percentile_cont(0.6) within GROUP(ORDER BY sal) over(PARTITION BY deptno) “Percentile_Cont”,
percent_rank() over(PARTITION BY deptno ORDER BY sal) “Percent_Rank”
FROM emp
WHERE deptno IN (30, 60);

PERCENTILE_DISC函数
功能描述:返回一个与输入的分布百分比值相对应的数据值,分布百分比的计算方法见函数CUME_DIST,如果没有正好对应的数据值,就取大于该分布值的下一个值。
注意:本函数与PERCENTILE_CONT的区别在找不到对应的分布值时返回的替代值的计算方法不同

SAMPLE:下例中0.7的分布值在部门30中没有对应的Cume_Dist值,所以就取下一个分布值0.83333333所对应的SALARY来替代

SELECT ename,
sal,
deptno,
percentile_disc(0.7) within GROUP(ORDER BY sal) over(PARTITION BY deptno) “Percentile_Disc”,
cume_dist() over(PARTITION BY deptno ORDER BY sal) “Cume_Dist”
FROM emp
WHERE deptno IN (30, 60);

 

 

Linux命令总结

来源:http://blog.csdn.net/qwe6112071/article/details/50806734

1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件

<code>-a  列出包括.a开头的隐藏文件的所有文件
-A  通-a,但不列出"."和".."
-l  列出文件的详细信息
-c  根据ctime排序显示
-t  根据文件修改时间排序
---color[=WHEN] 用色彩辨别文件类型 WHEN 可以是'never'、'always'或'auto'其中之一
    白色:表示普通文件
    蓝色:表示目录
    绿色:表示可执行文件
    红色:表示压缩文件
    浅蓝色:链接文件
    红色闪烁:表示链接的文件有问题
    黄色:表示设备文件
    灰色:表示其它文件   
</code>

2.mv [选项] 源文件或目录 目录或多个源文件 | 移动或重命名文件

<code>-b  覆盖前做备份
-f  如存在不询问而强制覆盖
-i  如存在则询问是否覆盖
-u  较新才覆盖
-t  将多个源文件移动到统一目录下,目录参数在前,文件参数在后
eg:
    mv a /tmp/ 将文件a移动到 /tmp目录下
    mv a b 将a命名为b
    mv /home/zenghao test1.txt test2.txt test3.txt
</code>

3.cp [选项] 源文件或目录 目录或多个源文件 | 将源文件复制至目标文件,或将多个源文件复制至目标目录。

<code>-r -R 递归复制该目录及其子目录内容
-p  连同档案属性一起复制过去
-f  不询问而强制复制
-s  生成快捷方式
-a  将档案的所有特性都一起复制
</code>

4.scp [参数] [原路径] [目标路径] | 在Linux服务器之间复制文件和目录

<code>-v  详细显示输出的具体情况
-r  递归复制整个目录
(1) 复制文件:  
命令格式:  
scp local_file remote_username@remote_ip:remote_folder  
或者  
scp local_file remote_username@remote_ip:remote_file  
或者  
scp local_file remote_ip:remote_folder  
或者  
scp local_file remote_ip:remote_file  
第1,2个指定了用户名,命令执行后需要输入用户密码,第1个仅指定了远程的目录,文件名字不变,第2个指定了文件名  
第3,4个没有指定用户名,命令执行后需要输入用户名和密码,第3个仅指定了远程的目录,文件名字不变,第4个指定了文件名   
(2) 复制目录:  
命令格式:  
scp -r local_folder remote_username@remote_ip:remote_folder  
或者  
scp -r local_folder remote_ip:remote_folder  
第1个指定了用户名,命令执行后需要输入用户密码;  
第2个没有指定用户名,命令执行后需要输入用户名和密码;
eg:
    从 本地 复制到 远程
    scp /home/daisy/full.tar.gz root@172.19.2.75:/home/root 
    从 远程 复制到 本地
    scp root@/172.19.2.75:/home/root/full.tar.gz /home/daisy/full.tar.gz
</code>

5.rm [选项] 文件 | 删除文件

<code>-r  删除文件夹
-f  删除不提示
-i  删除提示
-v  详细显示进行步骤
</code>

6.touch [选项] 文件 | 创建空文件或更新文件时间

<code>-a  只修改存取时间
-m  值修改变动时间
-r  eg:touch -r a b ,使b的时间和a相同
-t  指定特定的时间 eg:touch -t 201211142234.50 log.log 
    -t time [[CC]YY]MMDDhhmm[.SS],C:年前两位
</code>

7.pwd 查看当前所在路径

8.cd 改变当前目录

<code>- :返回上层目录
.. :返回上层目录
回车  :返回主目录
/   :根目录
</code>

9.mkdir [选项] 目录… | 创建新目录

<code>-p  递归创建目录,若父目录不存在则依次创建
-m  自定义创建目录的权限  eg:mkdir -m 777 hehe
-v  显示创建目录的详细信息
</code>

10.rmdir 删除空目录

<code>-v  显示执行过程
-p  若自父母删除后父目录为空则一并删除
</code>

11.rm [选项] 文件… | 一个或多个文件或目录

<code>-f  忽略不存在的文件,不给出提示
-i  交互式删除
-r  将列出的目录及其子目录递归删除
-v  列出详细信息
</code>

12.echo:显示内容

<code>-n  输出后不换行
-e  遇到转义字符特殊处理  
    eg:
        echo "he\nhe"   显示he\nhe
        ehco -e "he\nhe"    显示he(换行了)he
</code>

13.cat [选项] [文件]..| 一次显示整个文件或从键盘创建一个文件或将几个文件合并成一个文件

<code>-n  编号文件内容再输出
-E  在结束行提示$
</code>

14.tac | 反向显示

15.more | 按页查看文章内容,从前向后读取文件,因此在启动时就加载整个文件

<code>+n  从第n行开始显示
-n  每次查看n行数据
+/String    搜寻String字符串位置,从其前两行开始查看
-c  清屏再显示
-p  换页时清屏
</code>

16.less | 可前后移动地逐屏查看文章内容,在查看前不会加载整个文件

<code>-m  显示类似于more命令的百分比
-N  显示行号
/   字符串:向下搜索“字符串”的功能
?   字符串:向上搜索“字符串”的功能
n   重复前一个搜索(与 / 或 ? 有关)
N   反向重复前一个搜索(与 / 或 ? 有关)
b   向后翻一页
d   向后翻半页
</code>

17.nl [选项]… [文件]… | 将输出内容自动加上行号
-b
-b a 不论是否有空行,都列出行号(类似 cat -n)
-b t 空行则不列行号(默认)
-n 有ln rn rz三个参数,分别为再最左方显示,最右方显示不加0,最右方显示加0

18.head [参数]… [文件]… | 显示档案开头,默认开头10行

<code>-v  显示文件名
-c number   显示前number个字符,若number为负数,则显示除最后number个字符的所有内容
-number/n (+)number     显示前number行内容,
-n number   若number为负数,则显示除最后number行数据的所有内容
</code>

19.tail [必要参数] [选择参数] [文件] | 显示文件结尾内容

<code>-v  显示详细的处理信息
-q  不显示处理信息
-num/-n (-)num      显示最后num行内容
-n +num 从第num行开始显示后面的数据
-c  显示最后c个字符
-f  循环读取
</code>

20.vi 编辑文件

<code>:w filename 将文章以指定的文件名保存起来  
:wq 保存并退出
:q! 不保存而强制退出
命令行模式功能键
1)插入模式
    按「i」切换进入插入模式「insert mode」,按"i"进入插入模式后是从光标当前位置开始输入文件;
    按「a」进入插入模式后,是从目前光标所在位置的下一个位置开始输入文字;
    按「o」进入插入模式后,是插入新的一行,从行首开始输入文字。

2)从插入模式切换为命令行模式
  按「ESC」键。
3)移动光标
  vi可以直接用键盘上的光标来上下左右移动,但正规的vi是用小写英文字母「h」、「j」、「k」、「l」,分别控制光标左、下、上、右移一格。
  按「ctrl」+「b」:屏幕往"后"移动一页。
  按「ctrl」+「f」:屏幕往"前"移动一页。
  按「ctrl」+「u」:屏幕往"后"移动半页。
  按「ctrl」+「d」:屏幕往"前"移动半页。
  按数字「0」:移到文章的开头。
  按「G」:移动到文章的最后。
  按「$」:移动到光标所在行的"行尾"。
  按「^」:移动到光标所在行的"行首"
  按「w」:光标跳到下个字的开头
  按「e」:光标跳到下个字的字尾
  按「b」:光标回到上个字的开头
  按「#l」:光标移到该行的第#个位置,如:5l,56l。

4)删除文字
  「x」:每按一次,删除光标所在位置的"后面"一个字符。
  「#x」:例如,「6x」表示删除光标所在位置的"后面"6个字符。
  「X」:大写的X,每按一次,删除光标所在位置的"前面"一个字符。
  「#X」:例如,「20X」表示删除光标所在位置的"前面"20个字符。
  「dd」:删除光标所在行。
  「#dd」:从光标所在行开始删除#行

5)复制
  「yw」:将光标所在之处到字尾的字符复制到缓冲区中。
  「#yw」:复制#个字到缓冲区
  「yy」:复制光标所在行到缓冲区。
  「#yy」:例如,「6yy」表示拷贝从光标所在的该行"往下数"6行文字。
  「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与"y"有关的复制命令都必须与"p"配合才能完成复制与粘贴功能。

6)替换
  「r」:替换光标所在处的字符。
  「R」:替换光标所到之处的字符,直到按下「ESC」键为止。

7)回复上一次操作
  「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次"u"可以执行多次回复。

8)更改
  「cw」:更改光标所在处的字到字尾处
  「c#w」:例如,「c3w」表示更改3个字

9)跳至指定的行
  「ctrl」+「g」列出光标所在行的行号。
  「#G」:例如,「15G」,表示移动光标至文章的第15行行首。
</code>

21.which 可执行文件名称 | 查看可执行文件的位置,在PATH变量指定的路径中查看系统命令是否存在及其位置

22.whereis [-bmsu] [BMS 目录名 -f ] 文件名| 定位可执行文件、源代码文件、帮助文件在文件系统中的位置

<code>-b   定位可执行文件。
-m   定位帮助文件。
-s   定位源代码文件。
-u   搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件。
-B   指定搜索可执行文件的路径。
-M   指定搜索帮助文件的路径。
-S   指定搜索源代码文件的路径。
</code>

23.locate | 通过搜寻数据库快速搜寻档案

<code>-r  使用正规运算式做寻找的条件
</code>

24.find find [PATH] [option] [action] | 在文件树种查找文件,并作出相应的处理

<code>选项与参数:
1. 与时间有关的选项:共有 -atime, -ctime 与 -mtime 和-amin,-cmin与-mmin,以 -mtime 说明
    -mtime n :n 为数字,意义为在 n 天之前的『一天之内』被更动过内容的档案;
    -mtime +n :列出在 n 天之前(不含 n 天本身)被更动过内容的档案档名;
    -mtime -n :列出在 n 天之内(含 n 天本身)被更动过内容的档案档名。
    -newer file :file 为一个存在的档案,列出比 file 还要新的档案档名

2. 与使用者或组名有关的参数:
    -uid n :n 为数字,这个数字是用户的账号 ID,亦即 UID
    -gid n :n 为数字,这个数字是组名的 ID,亦即 GID
    -user name :name 为使用者账号名称!例如 dmtsai
    -group name:name 为组名,例如 users ;
    -nouser :寻找档案的拥有者不存在 /etc/passwd 的人!
    -nogroup :寻找档案的拥有群组不存在于 /etc/group 的档案!

3. 与档案权限及名称有关的参数:
    -name filename:搜寻文件名为 filename 的档案(可使用通配符)
    -size [+-]SIZE:搜寻比 SIZE 还要大(+)或小(-)的档案。这个 SIZE 的规格有:
        c: 代表 byte
        k: 代表 1024bytes。所以,要找比 50KB还要大的档案,就是『 -size +50k 』
    -type TYPE :搜寻档案的类型为 TYPE 的,类型主要有:
        一般正规档案 (f)
        装置档案 (b, c)
        目录 (d)
        连结档 (l)
        socket (s)
        FIFO (p)
    -perm mode :搜寻档案权限『刚好等于』 mode的档案,这个mode为类似chmod的属性值,举例来说,-rwsr-xr-x 的属性为4755!
    -perm -mode :搜寻档案权限『必须要全部囊括 mode 的权限』的档案,举例来说,
        我们要搜寻-rwxr--r-- 亦即 0744 的档案,使用-perm -0744,当一个档案的权限为 -rwsr-xr-x ,亦即 4755 时,也会被列出来,因为 -rwsr-xr-x 的属性已经囊括了 -rwxr--r-- 的属性了。
    -perm +mode :搜寻档案权限『包含任一 mode 的权限』的档案,举例来
        说,我们搜寻-rwxr-xr-x ,亦即 -perm +755 时,但一个文件属性为 -rw-------也会被列出来,因为他有 -rw.... 的属性存在!
4. 额外可进行的动作:
    -exec command :command 为其他指令,-exec 后面可再接额外的指令来处理搜寻到的结果。
    -print :将结果打印到屏幕上,这个动作是预设动作!
    eg:
        find / -perm +7000 -exec ls -l {} \; ,额外指令以-exec开头,以\;结尾{}代替前面找到的内容
    | xargs 
        -i  默认的前面输出用{}代替 
        eg:
            find . -name "*.log" | xargs -i mv {} test4
</code>

25.grep ‘正则表达式’ 文件名 | 用正则表达式搜索文本,并把匹配的行打印出来

<code>-c  只输出匹配行的计数。
-I  不区分大小写(只适用于单字符)。
-l  只显示文件名
-v  显示不包含匹配文本的所有行。
-n  显示匹配行数据及其行号
</code>

25.file | 判断文件类型

26.gzip [-cdtv#] 檔名 | 压缩、解压缩,源文件都不再存在

<code>-d  进行解压缩
-c  将压缩的数据输出到屏幕上
-v  :显示原档案/压缩文件案的压缩比等信息
-#  :压缩等级,-1最快,但压缩比最差,=9最慢,但压缩比最好
</code>

27.gunzip | 解压缩

28.bzip2 | 压缩、解压缩

<code>-d  :解压
-z  :压缩
-k  :保留源文件
-c :将压缩的过程产生的数据输出到屏幕上!
-v :可以显示出原档案/压缩文件案的压缩比等信息;
-# :与 gzip 同样的,都是在计算压缩比的参数, -9 最佳, -1 最快!
</code>

29.bzcat 读取数据而无需解压

30.tar [主选项+辅选项] 文件或者目录 | 多个目录或档案打包、压缩成一个大档案

<code>主选项:
    -c  建立打包档案,可搭配 -v 来察看过程中被打包的档名(filename)
    -t  察看打包档案的内容含有哪些档名,重点在察看『档名』就是了;
    -x  解打包或解压缩的功能,可以搭配 -C (大写) 在特定目录解开
辅选项:
    -j  透过 bzip2 的支持进行压缩/解压缩:此时档名最好为 *.tar.bz2
    -z  透过 gzip 的支持进行压缩/解压缩:此时档名最好为 *.tar.gz
    -v  在压缩/解压缩的过程中,将正在处理的文件名显示出来!
    -f filename -f 后面要立刻接要被处理的档名!
    -C 目录   这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。
    --exclude FILE:在压缩打包过程中忽略某文件 eg: tar --exclude /home/zenghao -zcvf myfile.tar.gz /home/* /etc
    -p  保留备份数据的原本权限与属性,常用于备份(-c)重要的配置文件
    -P(大写)  保留绝对路径,亦即允许备份数据中含有根目录存在之意;
eg:
    压 缩:tar -jcvf filename.tar.bz2 要被压缩的档案或目录名称
    查 询:tar -jtvf filename.tar.bz2
    解压缩:tar -jxvf filename.tar.bz2 -C 欲解压缩的目录
</code>

31.exit 退出当前shell
32.logout 退出登录shell
33.shutdown -h now

34.users 显示当前登录系统地用户

35.who 登录在本机的用户与来源

<code>-H或--heading  显示各栏位的标题信息列。
</code>

36.w 登录在本机的用户及其运行的程序

<code>-s  使用简洁格式列表,不显示用户登入时间,终端机阶段作业和程序所耗费的CPU时间。
-h  不显示各栏位的标题信息列。
</code>

37.write 给当前联机的用户发消息

38.wall 给所有登录再本机的用户发消息

39.last 查看用户的登陆日志

40.lastlog 查看每个用户最后的登陆时间

41.finger [选项] [使用者] [用户@主机] | 查看用户信息

<code>-s 显示用户的注册名、实际姓名、终端名称、写状态、停滞时间、登录时间等信息
-l 除了用-s选项显示的信息外,还显示用户主目录、登录shell、邮件状态等信息,以及用户主目录下的.plan、.project和.forward文件的内容。
-p 除了不显示.plan文件和.project文件以外,与-l选项相同
</code>

42.hostname 查看主机名

43.alias ii = “ls -l” | 添加别名

44.unalias ii | 清除别名

45.useradd [-u UID] [-g 初始群组] [-G 次要群组] [-c 说明栏] [-d 家目录绝对路径] [-s shell] 使用者账号名 | 新增用户

<code>-M  不建立用户家目录!(系统账号默认值)
-m  建立用户家目录!(一般账号默认值)
-r  建立一个系统的账号,这个账号的 UID 会有限制 
-e  账号失效日期,格式为『YYYY-MM-DD』
-D  查看useradd的各项默认值
</code>

46.passwd | 修改密码

<code>-l  使密码失效
-u  与-l相对,用户解锁
-S  列出登陆用户passwd文件内的相关参数
-n  后面接天数,shadow 的第 4 字段,多久不可修改密码天数
-x  后面接天数,shadow 的第 5 字段,多久内必须要更动密码
-w  后面接天数,shadow 的第 6 字段,密码过期前的警告天数
-i  后面接『日期』,shadow 的第 7 字段,密码失效日期
使用管道刘设置密码:echo "zeng" | passwd --stdin zenghao
</code>

47.userdel 删除用户

<code>-r  用户文件一并删除
</code>

48.chage [-ldEImMW] 账号名 | 修改用户密码的相关属性

<code>-l  列出该账号的详细密码参数;
-d  后面接日期,修改 shadow 第三字段(最近一次更改密码的日期),格式YYYY-MM-DD
-E  后面接日期,修改 shadow 第八字段(账号失效日),格式 YYYY-MM-DD
-I  后面接天数,修改 shadow 第七字段(密码失效日期)
-m  后面接天数,修改 shadow 第四字段(密码最短保留天数)
-M  后面接天数,修改 shadow 第五字段(密码多久需要进行变更)
-W  后面接天数,修改 shadow 第六字段(密码过期前警告日期)
</code>

49.usermod [-cdegGlsuLU] username | 修改用户的相关属性

<code>-c  后面接账号的说明,即 /etc/passwd 第五栏的说明栏,可以加入一些账号的说明。
-d  后面接账号的家目录,即修改 /etc/passwd 的第六栏;
-e  后面接日期,格式是 YYYY-MM-DD 也就是在 /etc/shadow 内的第八个字段数据啦!
-f  后面接天数为 shadow 的第七字段。
-g  后面接初始群组,修改 /etc/passwd 的第四个字段,亦即是GID的字段!
-G  后面接次要群组,修改这个使用者能够支持的群组
-l  后面接账号名称。亦即是修改账号名称, /etc/passwd 的第一栏!
-s  后面接 Shell 的实际档案,例如 /bin/bash 或 /bin/csh 等等。
-u  后面接 UID 数字啦!即 /etc/passwd 第三栏的资料;
-L  冻结密码
-U  解冻密码
</code>

50.id [username] | 查看用户相关的id信息,还可以用来判断用户是否存在

51.groups 查看登陆用户支持的群组, 第一个输出的群组为有效群组

52.newgrp 切换有效群组

53.groupadd [-g gid] 组名 | 添加组

<code>-g  设定添加组的特定组id
</code>

54.groupmod [-g gid] [-n group_name] 群组名 | 修改组信息

<code>-g  修改既有的 GID 数字
-n  修改既有的组名
</code>

55.groupdel [groupname] | 删除群组

56.gpasswd | 群组管理员功能

<code>root管理员动作:
    -gpasswd groupname 设定密码
    -gpasswd [-A user1,...] [-M user3,...] groupname
        -A  将 groupname 的主控权交由后面的使用者管理(该群组的管理员)
        -M  将某些账号加入这个群组当中
    -gpasswd [-r] groupname
        -r  将 groupname 的密码移除
群组管理员动作:
    - gpasswd [-ad] user groupname 
        -a  将某位使用者加入到 groupname 这个群组当中
        -d  将某位使用者移除出 groupname 这个群组当中
</code>

57.chfn修改个人信息

58.mount [-t vfstype] [-o options] device dir

<code>-ro 采用只读方式挂接设备
-rw 采用读写方式挂接设备
eg:mount /home/mydisk.iso /tmp/mnt 通过mnt访问mydisk内的内容
</code>

59.umount 取消挂载

60.cut

<code>-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f  :与-d一起使用,指定显示哪个区域。
</code>

61.sort

<code>-n   依照数值的大小排序。
-o&lt;输出文件&gt;   将排序后的结果存入指定的文件。
-r   以相反的顺序来排序。
-t&lt;分隔字符&gt;   指定排序时所用的栏位分隔字符。
-k  选择以哪个区间进行排序。
</code>

62.wc 统计指定文件中的字节数、字数、行数, 并将统计结果显示输出

<code>-l filename 报告行数
-c filename 报告字节数
-m filename 报告字符数
-w filename 报告单词数
</code>

63.uniq 去除文件中相邻的重复行

清空/新建文件,将内容重定向输入进去
&> 正确、错误都重定向过去

后面追加

64.set 显示环境变量和普通变量
65.env 显示环境变量
66.export 把普通变量变成环境变量
67.unset 删除一个环境变量

aaa(){} 定义函数

68.read

<code>-p  接提示字符
-t  接等待的秒数
</code>

69.declare、typeset

<code>-i 声明为整数
-a 声明为数组
-f 声明为函数
-r 声明为只读
</code>

70.ulimit 限制使用者的某些系统资源

<code>-f  此 shell 可以建立的最大档案容量 (一般可能设定为 2GB)单位为 Kbytes eg: ulimit -f 1024 限制使用者仅能建立 1MBytes 以下的容量的档案
</code>

71.df [选项] [文件] | 显示指定磁盘文件的可用空间,如果没有文件名被指定,则所有当前被挂载的文件系统的可用空间将被显示

<code>-a  显示全部文件系统
-h  文件大小友好显示
-l  只显示本地文件系统
-i  显示inode信息
-T  显示文件系统类型
</code>

72.du [选项] [文件] | 显示每个文件和目录的磁盘使用空间

<code>-h  方便阅读的方式
-s  只显示总和的大小
</code>

73.ln [参数] [源文件或目录] [目标文件或目录] | 某一个文件在另外一个位置建立一个同步的链接

<code>-s  建立软连接   
-v  显示详细的处理过程
</code>

74.diff [参数] [文件1或目录1] [文件2或目录2] | 比较单个文件或者目录内容

<code>-b  不检查空格字符的不同。
-B  不检查空白行。
-i  不检查大小写
-q  仅显示差异而不显示详细信息
eg: diff a b &gt; parch.log 比较两个文件的不同并产生补丁
</code>

75.date [参数]… [+格式] | 显示或设定系统的日期与时间

<code>%H 小时(以00-23来表示)。 
%M 分钟(以00-59来表示)。 
%P AM或PM。
%D 日期(含年月日)
%U 该年中的周数。
date -s “2015-10-17 01:01:01″ //时间设定
date +%Y%m%d         //显示前天年月日
date +%Y%m%d --date="+1 day/month/year"  //显示前一天/月/年的日期
date +%Y%m%d --date="-1 day/month/year"  //显示后一天/月/年的日期
date -d '2 weeks' 2周后的日期
</code>

76.cal [参数] 月份] [年份] | 查看日历

<code>-1  显示当月的月历
-3  显示前、当、后一个月的日历
-m  显示星期一为一个星期的第一天
-s  (默认)星期天为第一天
-j  显示当月是一年中的第几天的日历
-y  显示当前年份的日历
</code>

77.ps | 列出当前进程的快照

<code>a   显示所有的进程
-a  显示同一终端下的所有程序
e   显示环境变量
f   显示进程间的关系
-H  显示树状结构
r   显示当前终端的程序
T   显示当前终端的所有程序
-au 显示更详细的信息
-aux    显示所有包含其他使用者的行程 
-u  指定用户的所有进程
</code>

78.top [参数] | 显示当前系统正在执行的进程的相关信息,包括进程ID、内存占用率、CPU占用率等

79.kill [参数] [进程号] | 杀死进程

80.free [参数] | 显示Linux系统中空闲的、已用的物理内存及swap内存,及被内核使用的buffer

81.vmstat | 对操作系统的虚拟内存、进程、CPU活动进行监控

82.iostat [参数] [时间t] [次数n](每隔t时间刷新一次,最多刷新n次)| 对系统的磁盘操作活动进行监视,汇报磁盘活动统计情况,同时也会汇报出CPU使用情况

<code>-p[磁盘] 显示磁盘和分区的情况
</code>

83.watch [参数] [命令] |重复执行某一命令以观察变化

<code>-n  时隔多少秒刷新
-d  高亮显示动态变化
</code>

84.at [参数] [时间] | 在一个指定的时间执行一个指定任务,只能执行一次

<code>HH:MM[am|pm] + number [minutes|hours|days|weeks] 强制在某年某月某日的某时刻进行该项任务
atq 查看系统未执行的任务
atrm n 删除编号为n的任务
at -c n 显示编号为n的任务的内容
</code>

85.crontab | 定时任务调度

<code>file    载入crontab
-e  编辑某个用户的crontab文件内容
-l  显示某个用户的crontab文件内容
-r  删除某个用户的crontab文件
</code>

86.ifconfig [网络设备] [参数] | 查看和配置网络设备

87.route | 显示和操作IP路由表

88.ping [参数] [主机名或IP地址] | 测试与目标主机的连通性

<code>-q  只显示最后的结果
</code>

89.netstat | 显示与IP、TCP、UDP和ICMP协议相关的统计数据

90.telnet [参数] [主机] | 用于远程登录,采用明文传送报文,安全性不好

91.rcp [参数] [源文件] [目标文件] | 远程文件拷贝

<code>-r  递归复制
-p  保留源文件的属性
usage: rcp –r remote_hostname:remote_dir local_dir
</code>

92.wget [参数] [URL地址] | 直接从网络上下载文件

<code>-o FILE 把记录写到FILE文件中    eg : wget -O a.txt URL
wget --limit-rate=300k URL  限速下载
</code>

93.awk

<code>-F 分隔符  以分隔符分隔内容
{}  要执行的脚本内容 eg:cat /etc/passwd |awk  -F ':'  '{print $1"\t"$7}'
</code>

94.sed 对数据行进行替换、删除、新增、选取等操作

<code>a   新增,在新的下一行出现
c   取代,取代 n1,n2 之间的行 eg: sed '1,2c Hi' ab
d   删除
i   插入,在新的上一行出现
</code>

95.paste 合并文件,需确保合并的两文件行数相同

<code>-d  指定不同于空格或tab键的域分隔符
-s  按行合并,单独一个文件为一行
</code>

96.su [参数] user | 切换登陆

<code>-l  切换时连同环境变量、工作目录一起改变
-c command  执行command变回原来的使用者
</code>

97.sudo | 以特定用户的权限执行特定命令

<code>-l  列出当前用户可执行的命令
-u username#uid 以指定用户执行命令
</code>
2

HTTP header详解

HTTP(HyperText Transfer Protocol)即超文本传输协议,所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应。HTTP header 大体分为Request和Response两部分。

Requests部分

Header 解释 示例
Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html
Accept-Charset 浏览器可以接受的字符编码集。 Accept-Charset: iso-8859-5
DontTrackMeHere 指定浏览器可以支持的web服务器返回内容压缩编码类型。 DontTrackMeHere: compress, gzip
Accept-Language 浏览器可接受的语言 Accept-Language: en,zh
Accept-Ranges 可以请求网页实体的一个或者多个子范围字段 Accept-Ranges: bytes
Authorization HTTP授权的授权证书 Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Cache-Control 指定请求和响应遵循的缓存机制 Cache-Control: no-cache
Connection 表示是否需要持久连接。(HTTP 1.1默认进行持久连接) Connection: close
Cookie HTTP请求发送时,会把保存在该请求域名下的所有cookie值一起发送给web服务器。 Cookie: $Version=1; Skin=new;
Content-Length 请求的内容长度 Content-Length: 348
Content-Type 请求的与实体对应的MIME信息 Content-Type: application/x-www-form-urlencoded
Date 请求发送的日期和时间 Date: Tue, 15 Nov 2010 08:12:31 GMT
Expect 请求的特定的服务器行为 Expect: 100-continue
From 发出请求的用户的Email From: raykaeso@leixuesong.cn
Host 指定请求的服务器的域名和端口号 Host: www.leixuesong.cn
If-Match 只有请求内容与实体相匹配才有效 If-Match: “737060cd8c284d8af7ad3082f209582d”
If-Modified-Since 如果请求的部分在指定时间之后被修改则请求成功,未被修改则返回304代码 If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT
If-None-Match 如果内容未改变返回304代码,参数为服务器先前发送的Etag,与服务器回应的Etag比较判断是否改变 If-None-Match: “737060cd8c284d8af7ad3082f209582d”
If-Range 如果实体未改变,服务器发送客户端丢失的部分,否则发送整个实体。参数也为Etag If-Range: “737060cd8c284d8af7ad3082f209582d”
If-Unmodified-Since 只在实体在指定时间之后未被修改才请求成功 If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT
Max-Forwards 限制信息通过代理和网关传送的时间 Max-Forwards: 10
Pragma 用来包含实现特定的指令 Pragma: no-cache
Proxy-Authorization 连接到代理的授权证书 Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Range 只请求实体的一部分,指定范围 Range: bytes=500-999
Referer 先前网页的地址,当前请求网页紧随其后,即来路 Referer: www.leixuesong.cn
TE 客户端愿意接受的传输编码,并通知服务器接受接受尾加头信息 TE: trailers,deflate;q=0.5
Upgrade 向服务器指定某种传输协议以便服务器进行转换(如果支持) Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
User-Agent User-Agent的内容包含发出请求的用户信息 User-Agent: Mozilla/5.0 (Linux; X11)
Via 通知中间网关或代理服务器地址,通信协议 Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning 关于消息实体的警告信息 Warn: 199 Miscellaneous warning

Responses 部分

Header 解释 示例
Accept-Ranges 表明服务器是否支持指定范围请求及哪种类型的分段请求 Accept-Ranges: bytes
Age 从原始服务器到代理缓存形成的估算时间(以秒计,非负) Age: 12
Allow 对某网络资源的有效的请求行为,不允许则返回405 Allow: GET, HEAD
Cache-Control 告诉所有的缓存机制是否可以缓存及哪种类型 Cache-Control: no-cache
Content-Encoding web服务器支持的返回内容压缩编码类型。 Content-Encoding: gzip
Content-Language 响应体的语言 Content-Language: en,zh
Content-Length 响应体的长度 Content-Length: 348
Content-Location 请求资源可替代的备用的另一地址 Content-Location: /index.htm
Content-MD5 返回资源的MD5校验值 Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
Content-Range 在整个返回体中本部分的字节位置 Content-Range: bytes 21010-47021/47022
Content-Type 返回内容的MIME类型 Content-Type: text/html; charset=utf-8
Date 原始服务器消息发出的时间 Date: Tue, 15 Nov 2010 08:12:31 GMT
ETag 请求变量的实体标签的当前值 ETag: “737060cd8c284d8af7ad3082f209582d”
Expires 响应过期的日期和时间 Expires: Thu, 01 Dec 2010 16:00:00 GMT
Last-Modified 请求资源的最后修改时间 Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT
Location 用来重定向接收方到非请求URL的位置来完成请求或标识新的资源 Location: http://www.leixuesong.cn/724
Pragma 包括实现特定的指令,它可应用到响应链上的任何接收方 Pragma: no-cache
Proxy-Authenticate 它指出认证方案和可应用到代理的该URL上的参数 Proxy-Authenticate: Basic
refresh 应用于重定向或一个新的资源被创造,在5秒之后重定向(由网景提出,被大部分浏览器支持) Refresh: 5; url=http://www.leixuesong.cn/724
Retry-After 如果实体暂时不可取,通知客户端在指定时间之后再次尝试 Retry-After: 120
Server web服务器软件名称 Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
Set-Cookie 设置Http Cookie Set-Cookie: UserID=raykaeso; Max-Age=3600; Version=1
Trailer 指出头域在分块传输编码的尾部存在 Trailer: Max-Forwards
Transfer-Encoding 文件传输编码 Transfer-Encoding:chunked
Vary 告诉下游代理是使用缓存响应还是从原始服务器请求 Vary: *
Via 告知代理客户端响应是通过哪里发送的 Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning 警告实体可能存在的问题 Warning: 199 Miscellaneous warning
WWW-Authenticate 表明客户端请求实体应该使用的授权方案 WWW-Authenticate: Basic
X-Cache 表示你的 http request 是由 proxy server 返回的 X-Cache:HIT TCP_MEM_HIT dirn:-2:-2

 

http://www.leixuesong.cn/724

JavaScript 10分钟入门

以下为译文,原文地址:http://www.codeproject.com/Articles/1006192/JavaScript-Summary


简介

JavaScript是一门面向对象的动态语言,他一般用来处理以下任务:

  1. 修饰网页
    • 生成HTML和CSS
    • 生成动态HTML内容
    • 生成一些特效
  2. 提供用户交互接口
    • 生成用户交互组件
    • 验证用户输入
    • 自动填充表单
  3. 能够读取本地或者远程数据的前端应用程序,例如http://web-engineering.info/JsFrontendApp-Book
  4. 通过Nodejs实现像JAVA,C#,C++一样的服务端程序
  5. 实现分布式WEB程序,包括前端和服务端

当前浏览器所支持的JavaScript的版本被称为“ECMAScript的5.1”,或简单的“ES5”,但接下来的两个版本,称为“ES6”和“ES7”(或“ES2015”和“ES2016”,新版以本年命名),有很多的附加功能和改进的语法,是非常值得期待的(并已部分被当前的浏览器和后端JS的环境支持)。

此篇博文,引自《Building Front-End Web Apps with Plain JavaScript》一书。

JavaScript类型和常量

JS有3个值类型:string,number和boolean,我们可以用一个变量v保存不同类型的值用来和typeof(v)比较, typeof(v)===”number”。

JS有5个引用类型:Object, Array, Function, Date 和 RegExp。数组,函数,日期和正则表达式是特殊类型的对象,但在概念上,日期和正则表达式是值类型,被包装成对象形式体现。

变量,数组,函数的参数和返回值都可以不声明,它们通常不会被JavaScript引擎检查,会被自动进行类型转换。

变量值可能为:

  1. 数据,如string,number,boolean
  2. 对象的引用:如普通对象,数组,函数,日期,正则表达式
  3. 特殊值null,其通常用作用于初始化的对象变量的默认值
  4. 特殊值undefined,已经声明但没有初始化的初始值

string是Unicode字符序列。字符串常量会被单引号或双引号包裹着,如“Hello world!”,“A3F0’,或者空字符串””。两个字符串表达式可以用+操作符连接,并可通过全等于比较:

<code class="hljs lisp"> if (<span class="hljs-name">firstName</span> + lastName === <span class="hljs-string">"James Bond"</span>) </code>

字符串的字符数量可以通过length属性获得:

<code class="hljs cpp">console.<span class="hljs-built_in">log</span>( <span class="hljs-string">"Hello world!"</span>.length);  <span class="hljs-comment">// 12</span></code>

所有的数字值都是在64位浮点数字。整数和浮点数之间没有明确的类型区别。如果一个数字常量不是数字,可以将其值设置为NaN(“not a number”),它可以用isNaN方法来判断。

不幸的是,直到ES6才有Number.isInteger方法,用于测试一个数是不是一个整数。因此在还不支持它的浏览器中,为确保一个数字值是一个整数,或者一个数字的字符串被转换为一个整数,就必须使用parseInt函数。类似地,包含小数的字符串可用与parseFloat方法转换。将一个数子n转换成字符串,最好的方法是使用String(n)。

就像Java,我们也有两个预先定义好的布尔型值,true与false,以及布尔运算符符号: ! (非),&&(与),||(或)。当非布尔型值与布尔型值比较时,非布尔型值会被隐式转换。空字符串,数字0,以及undefined和null,会被转换为false,其他所有值会转换为true。

通常我们需要使用全等符号符号(===和!==)而不是==和!=。否则,数字2是等于的字符串“2”的, (2 == “2”) is true

VAR= [] 和var a = new Array() 都可以定义一个空数组。(二胡:推荐前者)

VAR O ={} 和 var o = new Obejct() 都可以定义个空对象(二胡:还是推荐前者)。注意,一个空对象{}不是真的空的,因为它包含的Object.prototype继承属性。所以,一个真正的空对象必须以Null为原型, var o = Object.create(null)。

表1 类型测试和转换

变量作用域

在JavaScript的当前版本ES5,有两种范围变量:全局作用域和函数作用域,没有块作用域。因此,应该避免声明在块内声明变量。

<code class="hljs matlab"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span> {</span>
  <span class="hljs-keyword">for</span> (var <span class="hljs-built_in">i</span>=<span class="hljs-number">0</span>; <span class="hljs-built_in">i</span> &lt; <span class="hljs-number">10</span>; <span class="hljs-built_in">i</span>++) {
    ...  // do something with i
  }
}</code>

我们应该这样写

<code class="hljs matlab"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span><span class="hljs-params">()</span> {</span>
  var <span class="hljs-built_in">i</span>=<span class="hljs-number">0</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-built_in">i</span>=<span class="hljs-number">0</span>; <span class="hljs-built_in">i</span> &lt; <span class="hljs-number">10</span>; <span class="hljs-built_in">i</span>++) {
    ...  // do something with i
  }
}</code>

所有变量应在函数的开始声明。只有在JavaScript的下一个版本ES6中,我们可以用let关键词声明一个块级变量。

严格模式

从ES5开始,我们可以使用严格模式,获得更多的运行时错误检查。例如,在严格模式下,所有变量都必须进行声明。给未声明的变量赋值抛出异常。

我们可以通过键入下面的语句作为一个JavaScript文件或script元素中的第一行开启严格模式:’use strict’;

通常建议您使用严格模式,除非你的代码依赖于与严格的模式不兼容的库。

不同类型的对象

JS对象与传统的OO/UML对象不同。它们可以不通过类实例化而来。它们有属性、方法、键值对三种扩展。

JS对象可以直接通过JSON产生,而不用实例化一个类。

<code class="hljs javascript"><span class="hljs-keyword">var</span> person1 = { lastName:<span class="hljs-string">"Smith"</span>, firstName:<span class="hljs-string">"Tom"</span>};
<span class="hljs-keyword">var</span> o1 = <span class="hljs-built_in">Object</span>.create( <span class="hljs-literal">null</span>);  <span class="hljs-comment">// an empty object with no slots</span></code>

对象属性可以以两种方式获得:

  1. 使用点符号(如在C ++/ Java的):

    person1.lastName = “Smith”

  2. 使用MAP方式

    person1[“lastName”] = “Smith”

JS对象有不同的使用方式。这里有五个例子:

  1. 记录,例如,

    var myRecord = {firstName:”Tom”, lastName:”Smith”, age:26}

  2. MAP(也称为“关联数组”,“词典”或其他语言的“哈希表”)

    var numeral2number = {“one”:”1″, “two”:”2″, “three”:”3″}

  3. 非类型化对象

    var person1 = { lastName: "Smith", firstName: "Tom", getFullName: function () { return this.firstName +" "+ this.lastName; } };

  4. 命名空间

    var myApp = { model:{}, view:{}, ctrl:{} };

可以由一个全局变量形式来定义,它的名称代表一个命名空间前缀。例如,上面的对象变量提供了基于模型 – 视图 – 控制器(MVC)架构模式,我们有相应的MVC应用程序的三个部分。

  1. 正常的类

数组

可以用一个JavaScript数组文本进行初始化变量:
var a = [1,2,3];

因为它们是数组列表,JS数组可动态增长:我们可以使用比数组的长度更大的索引。例如,上面的数组变量初始化后,数组长度为3,但我们仍然可以操作第5个元素 a[4] = 7;

我们可以通过数组的length属性得到数组长度:

<code class="hljs coffeescript">`<span class="javascript"><span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i &lt; a.length; i++) { <span class="hljs-built_in">console</span>.log(a[i]);} <span class="hljs-comment">//1 2 3 undefined 7 </span></span>` </code>

我们可以通过 Array.isArray(a) 来检测一个变量是不是数组。

通过push方法给数组追加元素:a.push( newElement);

通过splice方法,删除指定位置的元素:a.splice( i, 1);

通过indexOf查找数组,返回位置或者-1:if (a.indexOf(v) > -1) …

通过for或者forEach(性能弱)遍历数组:

<code class="hljs matlab">var <span class="hljs-built_in">i</span>=<span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-built_in">i</span>=<span class="hljs-number">0</span>; <span class="hljs-built_in">i</span> &lt; a.<span class="hljs-built_in">length</span>; <span class="hljs-built_in">i</span>++) {
  console.log( a[i]);
}
a.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(elem)</span> {</span>
  console.<span class="hljs-built_in">log</span>( elem);
}) </code>

通过slice复制数组:var clone = a.slice(0);

Maps

map(也称为“散列映射”或“关联数组’)提供了从键及其相关值的映射。一个JS map的键是可以包含空格的字符串:

<code class="hljs swift"><span class="hljs-keyword">var</span> myTranslation = { 
<span class="hljs-string">"my house"</span>: <span class="hljs-string">"mein Haus"</span>, 
<span class="hljs-string">"my boat"</span>: <span class="hljs-string">"mein Boot"</span>, 
<span class="hljs-string">"my horse"</span>: <span class="hljs-string">"mein Pferd"</span>
}</code>

通过Object.keys(m)可以获得map中所有的键:

<code class="hljs perl">var i=<span class="hljs-number">0</span>, key=<span class="hljs-string">""</span>, <span class="hljs-keyword">keys</span>=[];
<span class="hljs-keyword">keys</span> = Object.<span class="hljs-keyword">keys</span>( myTranslation);
<span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i &lt; <span class="hljs-keyword">keys</span>.<span class="hljs-keyword">length</span>; i++) {
  key = <span class="hljs-keyword">keys</span>[i];
  alert(<span class="hljs-string">'The translation of '</span>+ key +<span class="hljs-string">' is '</span>+ myTranslation[key]);
}</code>

通过直接给不存在的键赋值来新增元素:

<code class="hljs prolog">myTranslation[<span class="hljs-string">"my car"</span>] = <span class="hljs-string">"mein Auto"</span>;</code>

通过delete删除元素:

<code class="hljs sql"><span class="hljs-keyword">delete</span> myTranslation[<span class="hljs-string">"my boat"</span>];</code>

通过in搜索map:

<code class="hljs coffeescript">`<span class="javascript"><span class="hljs-keyword">if</span> (<span class="hljs-string">"my bike"</span> <span class="hljs-keyword">in</span> myTranslation)  ...</span>`</code>

通过for或者forEach(性能弱)和Object.keys()遍历map:

<code class="hljs perl">var i=<span class="hljs-number">0</span>, key=<span class="hljs-string">""</span>, <span class="hljs-keyword">keys</span>=[];
<span class="hljs-keyword">keys</span> = Object.<span class="hljs-keyword">keys</span>( <span class="hljs-keyword">m</span>);
<span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i &lt; <span class="hljs-keyword">keys</span>.<span class="hljs-keyword">length</span>; i++) {
  key = <span class="hljs-keyword">keys</span>[i];
  console.<span class="hljs-keyword">log</span>( <span class="hljs-keyword">m</span>[key]);
}
Object.<span class="hljs-keyword">keys</span>( <span class="hljs-keyword">m</span>).forEach( function (key) {
  console.<span class="hljs-keyword">log</span>( <span class="hljs-keyword">m</span>[key]);
}) </code>

通过 JSON.stringify 将map序列化为JSON字符串,再JSON.parse将其反序列化为MAP对象 来实现复制:

<code class="hljs javascript"><span class="hljs-keyword">var</span> clone = <span class="hljs-built_in">JSON</span>.parse( <span class="hljs-built_in">JSON</span>.stringify( m)) </code>

请注意,如果map上只包含简单数据类型或(可能嵌套)数组/map,这种方法效果很好。在其他情况下,如果map包含Date对象,我们必须写我们自己的clone方法。

Functions

JS函数是特殊的JS的对象,它具有一个可选的名字属性和一个长度属性(参数的数目)。我们可以这样知道一个变量是不是一个函数:

<code class="hljs javascript"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span>( v) === <span class="hljs-string">"function"</span>) {...}</code>

JS函数可以保存在变量里、被当作参数传给其他函数,也可以被其他函数作为返回值返回。JS可以被看成一个函数式语言,函数在里面可以说是一等公民。

正常的定义函数方法是用一个函数表达式给一个变量赋值:

<code class="hljs javascript"><span class="hljs-keyword">var</span> myFunction = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">theNameOfMyFunction</span> () </span>{...}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">theNameOfMyFunction</span> () </span>{...}</code>

其中函数名(theNameOfMyFunction)是可选的。如果省略它,其就是一个匿名函数。函数可以通过引用其的变量调用。在上述情况下,这意味着该函数通过myFunction()被调用,而不是通过theNameOfMyFunction()调用。

JS函数,可以嵌套内部函数。闭包机制允许在函数外部访问函数内部变量,并且创建闭包的函数会记住它们。

当执行一个函数时,我们可以通过使用内置的arguments参数,它类似一个参数数组,我们可以遍历它们,但由于它不是常规数组,forEach无法遍历它。arguments参数包含所有传递给函数的参数。我们可以这样定义一个不带参数的函数,并用任意数量的参数调用它,就像这样:

<code class="hljs javascript"><span class="hljs-keyword">var</span> sum = <span class="hljs-function"><span class="hljs-keyword">function</span> () </span>{
  <span class="hljs-keyword">var</span> result = <span class="hljs-number">0</span>, i=<span class="hljs-number">0</span>;
  <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
    result = result + <span class="hljs-built_in">arguments</span>[i];
  }
  <span class="hljs-keyword">return</span> result;
};
<span class="hljs-built_in">console</span>.log( sum(<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">5</span>,<span class="hljs-number">8</span>));  <span class="hljs-comment">// 20</span></code>

prototype原型链可以访问函数中的每一个元素,如Array.prototype.forEach(其中Array代表原型链中的数组的构造函数)。

<code class="hljs javascript"><span class="hljs-keyword">var</span> numbers = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>];  <span class="hljs-comment">// create an instance of Array</span>
numbers.forEach( <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{
  <span class="hljs-built_in">console</span>.log( n);
});</code>

我们还可以通过原型链中的prototype.call方法来处理:

<code class="hljs javascript"><span class="hljs-keyword">var</span> sum = <span class="hljs-function"><span class="hljs-keyword">function</span> () </span>{
  <span class="hljs-keyword">var</span> result = <span class="hljs-number">0</span>;
  <span class="hljs-built_in">Array</span>.prototype.forEach.call( <span class="hljs-built_in">arguments</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{
    result = result + n;
  });
  <span class="hljs-keyword">return</span> result;
};</code>

Function.prototype.apply是Function.prototype.call的一个变种,其只能接受一个参数数组。

立即调用的JS函数表达式优于使用纯命名对象,它可以获得一个命名空间对象,并可以控制其变量和方法哪些可以外部访问,哪些不是。这种机制也是JS模块概念的基础。在下面的例子中,我们定义了一个应用程序,它对外暴露了指定的元素和方法:

<code class="hljs javascript">myApp.model = <span class="hljs-function"><span class="hljs-keyword">function</span> () </span>{
  <span class="hljs-keyword">var</span> appName = <span class="hljs-string">"My app's name"</span>;
  <span class="hljs-keyword">var</span> someNonExposedVariable = ...;
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ModelClass1</span> () </span>{...}
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ModelClass2</span> () </span>{...}
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">someNonExposedMethod</span> (<span class="hljs-params">...</span>) </span>{...}
  <span class="hljs-keyword">return</span> {
    appName: appName,
    ModelClass1: ModelClass1,
    ModelClass2: ModelClass2
  }
}();  <span class="hljs-comment">// immediately invoked</span></code>

这种模式在WebPlatform.org被当作最佳实践提及:https://docs.webplatform.org/wiki/tutorials/javascript_best_practices

定义和使用类

类是在面向对象编程的基础概念。对象由类实例化而来。一个类定义了与它创建的对象的属性和方法。

目前在JavaScript中没有明确的类的概念。JavaScript中定义类有很多不同的模式被提出,并在不同的框架中被使用。用于定义类的两个最常用的方法是:

构造函数法,它通过原型链方法来实现继承,通过new创建新对象。这是Mozilla的JavaScript指南中推荐的经典方法。

工厂方法:使用预定义的Object.create方法创建类的新实例。在这种方法中,基于构造函数继承必须通过另一种机制来代替。

当构建一个应用程序时,我们可以使用这两种方法创建类,这取决于应用程序的需求 。mODELcLASSjs是一个比较成熟的库用来实现工厂方法,它有许多优点。(基于构造的方法有一定的性能优势)

ES6中构造函数法创建类

在ES6,用于定义基于构造函数的类的语法已推出(新的关键字类的构造函数,静态类和超类)。这种新的语法可以在三个步骤定义一个简单的类。

基类Person 定义了两个属性firstName 和lastName,以及实例方法toString和静态方法checkLastName:

<code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
  <span class="hljs-keyword">constructor</span>( first, last) {
    <span class="hljs-keyword">this</span>.firstName = first;
    <span class="hljs-keyword">this</span>.lastName = last;
  }
  toString() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.firstName + <span class="hljs-string">" "</span> +
        <span class="hljs-keyword">this</span>.lastName;
  }
  static checkLastName( ln) {
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span>(ln)!==<span class="hljs-string">"string"</span> || 
        ln.trim()===<span class="hljs-string">""</span>) {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Error: "</span> +
          <span class="hljs-string">"invalid last name!"</span>);
    }
  }
}</code>

类的静态属性如下定义:

<code class="hljs">Person.instances = {};</code>

一个子类定义的附加属性和可能会覆盖超类的方法:

<code class="hljs scala"> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Person</span> </span>{
  constructor( first, last, studNo) {
    <span class="hljs-keyword">super</span>.constructor( first, last);
    <span class="hljs-keyword">this</span>.studNo = studNo; 
  }
  <span class="hljs-comment">// method overrides superclass method</span>
  toString() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">super</span>.toString() + <span class="hljs-string">"("</span> +
        <span class="hljs-keyword">this</span>.studNo +<span class="hljs-string">")"</span>;
  }
}</code>

ES5中构造函数法创建类

在ES5,我们可以以构造函数的形式定义一个基于构造函数的类结构,下面是Mozilla的JavaScript指南中推荐的编码模式。此模式需要七个步骤来定义一个简单的类结构。由于这种复杂的模式可能很难记住,我们可能需要使用cLASSjs之类的库来帮助我们。

首先定义构造函数是隐式创建一个新的对象,并赋予它相应的值:

<code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params"> first, last</span>) </span>{
  <span class="hljs-keyword">this</span>.firstName = first; 
  <span class="hljs-keyword">this</span>.lastName = last; 
}</code>

这里的this指向新创建的对象。

在原型中定义实例方法:

<code class="hljs javascript">Person.prototype.toString = <span class="hljs-function"><span class="hljs-keyword">function</span> () </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.firstName + <span class="hljs-string">" "</span> + <span class="hljs-keyword">this</span>.lastName;
}</code>

可以在构造函数中定义静态方法,也可以用.直接定义:

<code class="hljs javascript">Person.checkLastName = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">ln</span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span>(ln)!==<span class="hljs-string">"string"</span> || ln.trim()===<span class="hljs-string">""</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Error: invalid last name!"</span>);
  }
}</code>

定义静态属性:

<code class="hljs">Person.instances = {};</code>

定义子类并增加属性:

<code class="hljs javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Student</span>(<span class="hljs-params"> first, last, studNo</span>) </span>{
  <span class="hljs-comment">// invoke superclass constructor</span>
  Person.call( <span class="hljs-keyword">this</span>, first, last);
  <span class="hljs-comment">// define and assign additional properties</span>
  <span class="hljs-keyword">this</span>.studNo = studNo;  
}</code>

通过Person.call( this, …) 来调用基类的构造函数。

将子类的原型链改为基类的原型链,以实现实例方法的继承(构造函数得改回来):

<code class="hljs delphi"><span class="hljs-comment">// Student inherits from Person</span>
Student.prototype = <span class="hljs-keyword">Object</span>.create( 
    Person.prototype);
<span class="hljs-comment">// adjust the subtype's constructor property</span>
Student.prototype.<span class="hljs-keyword">constructor</span> = Student;</code>

通过Object.create( Person.prototype) 我们基于 Person.prototype创建了一个新的对象原型。

定义覆盖基类方法的子类方法:

<code class="hljs javascript">Student.prototype.toString = <span class="hljs-function"><span class="hljs-keyword">function</span> () </span>{
  <span class="hljs-keyword">return</span> Person.prototype.toString.call( <span class="hljs-keyword">this</span>) +
      <span class="hljs-string">"("</span> + <span class="hljs-keyword">this</span>.studNo + <span class="hljs-string">")"</span>;
};</code>

最后通过new关键字来实例化一个类

<code class="hljs javascript"><span class="hljs-keyword">var</span> pers1 = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Tom"</span>,<span class="hljs-string">"Smith"</span>);</code>

JavaScript的prototype

prototype是函数的一个属性(每个函数都有一个prototype属性),这个属性是一个指针,指向一个对象。它是显示修改对象的原型的属性。

__proto__是一个对象拥有的内置属性(prototype是函数的内置属性。__proto__是对象的内置属性),是JS内部使用寻找原型链的属性。

每个对象都有个constructor属性,其指向的是创建当前对象的构造函数。

工厂模式创建类

在这种方法中,我们定义了一个JS对象Person,并在其内部定义了一个create方法用来调用Object.create来创建类。

<code class="hljs javascript"><span class="hljs-keyword">var</span> Person = {
  name: <span class="hljs-string">"Person"</span>,
  properties: {
    firstName: {range:<span class="hljs-string">"NonEmptyString"</span>, label:<span class="hljs-string">"First name"</span>, 
        writable: <span class="hljs-literal">true</span>, enumerable: <span class="hljs-literal">true</span>},
    lastName: {range:<span class="hljs-string">"NonEmptyString"</span>, label:<span class="hljs-string">"Last name"</span>, 
        writable: <span class="hljs-literal">true</span>, enumerable: <span class="hljs-literal">true</span>}
  },
  methods: {
    getFullName: <span class="hljs-function"><span class="hljs-keyword">function</span> () </span>{
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.firstName +<span class="hljs-string">" "</span>+ <span class="hljs-keyword">this</span>.lastName; 
    }
  },
  create: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">slots</span>) </span>{
    <span class="hljs-comment">// create object</span>
    <span class="hljs-keyword">var</span> obj = <span class="hljs-built_in">Object</span>.create( <span class="hljs-keyword">this</span>.methods, <span class="hljs-keyword">this</span>.properties);
    <span class="hljs-comment">// add special property for *direct type* of object</span>
    <span class="hljs-built_in">Object</span>.defineProperty( obj, <span class="hljs-string">"type"</span>, 
        {value: <span class="hljs-keyword">this</span>, writable: <span class="hljs-literal">false</span>, enumerable: <span class="hljs-literal">true</span>});
    <span class="hljs-comment">// initialize object</span>
    <span class="hljs-built_in">Object</span>.keys( slots).forEach( <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">prop</span>) </span>{
      <span class="hljs-keyword">if</span> (prop <span class="hljs-keyword">in</span> <span class="hljs-keyword">this</span>.properties) obj[prop] = slots[prop];
    })
    <span class="hljs-keyword">return</span> obj;
  }
};  </code>

这样,我们就有了一个Person的工厂类,通过调用create方法来实例化对象。

<code class="hljs groovy">var pers1 = Person.create( {<span class="hljs-string">firstName:</span><span class="hljs-string">"Tom"</span>, <span class="hljs-string">lastName:</span><span class="hljs-string">"Smith"</span>});</code>

履道素而无欲,时虽移而不变者,朴人也