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 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
tar xvfz freetds-stable.tgz
cd freetds-0.82
./configure --enable-msdblib --with-tdsver=8.0 --with-unixodbc=/usr
make install
make clean

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

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

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

If that worked, I recommend throwing a (coding) party. Next up is some configging. Open the FreeTDS config file.

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

Now open the ODBC configuration file:

And add the following MSSQL driver entry (FreeTDS) at the end:
Description = FreeTDS driver
Driver = /usr/local/lib/
FileUsage = 1
UsageCount = 1 

Then, finally, set up the DSN within ODBC in the odbc.ini file here
By adding this bit to the file:
Description = MS SQL Server
Driver = /usr/local/lib/
Server =
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

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/

…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

…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

..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><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

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 and run:

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

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

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 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

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>

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

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

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

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>

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>

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

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, 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.


Top 10  System Monitoring Tools

1. 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 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


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.



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


(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




  .split(" ")

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.


5 祝你生日快乐!


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) {
        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服务




let xmlDoc = try? AEXMLDocument(xmlData: NSData(contentsOfURL: NSURL(string:"")!)!)

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].reduce(Int.max, combine: min)

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

9 并行处理



10 埃拉托斯特尼筛法




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

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

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



var sameprimes = Set(2...n)

						   .flatMap{ (2*$0).stride(through:n, by:$0)})




var a=1,b=2

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




over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数
over(partition by deptno)按照部门分区


   over(partition by deptno order by salary)


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):窗口不做限制





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

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有两名但也只显示一个

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
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

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行数据)
with a as
(select 1 id,’a’ name from dual
select 2 id,’b’ name from dual
select 3 id,’c’ name from dual
select 4 id,’d’ name from dual
select 5 id,’e’ name from dual
select id,name,lag(id,1,”)over(order by name) from a;

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

with a as
(select 1 id,’a’ name from dual
select 2 id,’b’ name from dual
select 3 id,’c’ name from dual
select 4 id,’d’ name from dual
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;–分组后的占比


SELECT a.deptno,
(a.r-1)/(n-1) pr1,
percent_rank() over(PARTITION BY a.deptno ORDER BY a.sal) pr2
FROM (SELECT deptno,
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;

SELECT a.deptno,
(a.r + c.rn – 1) / n pr1,
cume_dist() over(PARTITION BY a.deptno ORDER BY a.sal) pr2
FROM (SELECT deptno,
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;

SELECT ename,
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);

SELECT ename,
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);



SELECT ename,
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);




来源: [选项] [目录名 | 列出相关目录下的所有目录和文件

<code>-a  列出包括.a开头的隐藏文件的所有文件
-A  通-a,但不列出"."和".."
-l  列出文件的详细信息
-c  根据ctime排序显示
-t  根据文件修改时间排序
---color[=WHEN] 用色彩辨别文件类型 WHEN 可以是'never'、'always'或'auto'其中之一
</code> [选项] 源文件或目录 目录或多个源文件 | 移动或重命名文件

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

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

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

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  
(2) 复制目录:  
scp -r local_folder remote_username@remote_ip:remote_folder  
scp -r local_folder remote_ip:remote_folder  
    从 本地 复制到 远程
    scp /home/daisy/full.tar.gz root@ 
    从 远程 复制到 本地
    scp root@/ /home/daisy/full.tar.gz

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

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

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:年前两位

7.pwd 查看当前所在路径 改变当前目录

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

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

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

10.rmdir 删除空目录

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

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

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


<code>-n  输出后不换行
-e  遇到转义字符特殊处理  
        echo "he\nhe"   显示he\nhe
        ehco -e "he\nhe"    显示he(换行了)he
</code> [选项] [文件]..| 一次显示整个文件或从键盘创建一个文件或将几个文件合并成一个文件

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

14.tac | 反向显示

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

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

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

<code>-m  显示类似于more命令的百分比
-N  显示行号
/   字符串:向下搜索“字符串”的功能
?   字符串:向上搜索“字符串”的功能
n   重复前一个搜索(与 / 或 ? 有关)
N   反向重复前一个搜索(与 / 或 ? 有关)
b   向后翻一页
d   向后翻半页
</code> [选项]… [文件]… | 将输出内容自动加上行号
-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行数据的所有内容

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

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

<code>:w filename 将文章以指定的文件名保存起来  
:wq 保存并退出
:q! 不保存而强制退出
    按「i」切换进入插入模式「insert mode」,按"i"进入插入模式后是从光标当前位置开始输入文件;








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

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

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

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

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

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

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 :将结果打印到屏幕上,这个动作是预设动作!
        find / -perm +7000 -exec ls -l {} \; ,额外指令以-exec开头,以\;结尾{}代替前面找到的内容
    | xargs 
        -i  默认的前面输出用{}代替 
            find . -name "*.log" | xargs -i mv {} test4

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

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

25.file | 判断文件类型

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

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

27.gunzip | 解压缩

28.bzip2 | 压缩、解压缩

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

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

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

    -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(大写)  保留绝对路径,亦即允许备份数据中含有根目录存在之意;
    压 缩:tar -jcvf filename.tar.bz2 要被压缩的档案或目录名称
    查 询:tar -jtvf filename.tar.bz2
    解压缩:tar -jxvf filename.tar.bz2 -C 欲解压缩的目录

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

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

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

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

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

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

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

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

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

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

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

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

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的各项默认值

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

47.userdel 删除用户

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

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

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

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> [username] | 查看用户相关的id信息,还可以用来判断用户是否存在

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

52.newgrp 切换有效群组

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

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

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

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

55.groupdel [groupname] | 删除群组

56.gpasswd | 群组管理员功能

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


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

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

59.umount 取消挂载


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


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

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

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

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

&> 正确、错误都重定向过去


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

aaa(){} 定义函数

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


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

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

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

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

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

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

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

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

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

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

<code>-b  不检查空格字符的不同。
-B  不检查空白行。
-i  不检查大小写
-q  仅显示差异而不显示详细信息
eg: diff a b &gt; parch.log 比较两个文件的不同并产生补丁
</code> [参数]… [+格式] | 显示或设定系统的日期与时间

<code>%H 小时(以00-23来表示)。 
%M 分钟(以00-59来表示)。 
%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> [参数] 月份] [年份] | 查看日历

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

<code>a   显示所有的进程
-a  显示同一终端下的所有程序
e   显示环境变量
f   显示进程间的关系
-H  显示树状结构
r   显示当前终端的程序
T   显示当前终端的所有程序
-au 显示更详细的信息
-aux    显示所有包含其他使用者的行程 
-u  指定用户的所有进程
</code> [参数] | 显示当前系统正在执行的进程的相关信息,包括进程ID、内存占用率、CPU占用率等

79.kill [参数] [进程号] | 杀死进程 [参数] | 显示Linux系统中空闲的、已用的物理内存及swap内存,及被内核使用的buffer

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

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

<code>-p[磁盘] 显示磁盘和分区的情况
</code> [参数] [命令] |重复执行某一命令以观察变化

<code>-n  时隔多少秒刷新
-d  高亮显示动态变化
</code> [参数] [时间] | 在一个指定的时间执行一个指定任务,只能执行一次

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

85.crontab | 定时任务调度

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

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

87.route | 显示和操作IP路由表 [参数] [主机名或IP地址] | 测试与目标主机的连通性

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

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

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

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

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

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

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


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

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

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

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

<code>-d  指定不同于空格或tab键的域分隔符
-s  按行合并,单独一个文件为一行
</code> [参数] user | 切换登陆

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

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

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

HTTP header详解

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


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:
Host 指定请求的服务器的域名和端口号 Host:
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:
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 (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:
Pragma 包括实现特定的指令,它可应用到响应链上的任何接收方 Pragma: no-cache
Proxy-Authenticate 它指出认证方案和可应用到代理的该URL上的参数 Proxy-Authenticate: Basic
refresh 应用于重定向或一个新的资源被创造,在5秒之后重定向(由网景提出,被大部分浏览器支持) Refresh: 5; url=
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 (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

JavaScript 10分钟入门




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


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


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

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



  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>


<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方法来判断。


就像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 类型测试和转换



<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 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




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





<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”


  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. 正常的类


var a = [1,2,3];

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


<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) …


<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);


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


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


<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>


<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>




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



<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>




<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>


<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 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> <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 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>







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



基类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 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 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 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 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 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> <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;  

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


<code class="hljs delphi"><span class="hljs-comment">// Student inherits from Person</span>
Student.prototype = <span class="hljs-keyword">Object</span>.create( 
<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> <span class="hljs-keyword">this</span>) +
      <span class="hljs-string">"("</span> + <span class="hljs-keyword">this</span>.studNo + <span class="hljs-string">")"</span>;


<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>







<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>


<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>

Linux Shell 文本处理工具集锦


find 文件查找

  • 查找txt和pdf文件
    <code><span class="pln">  find </span><span class="pun">.</span><span class="pln"> \( </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="pln">o </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.pdf"</span><span class="pln"> \) </span><span class="pun">-</span><span class="kwd">print</span></code>
  • 正则方式查找.txt和pdf
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">regex  </span><span class="str">".*\(\.txt|\.pdf\)$"</span></code>

    -iregex: 忽略大小写的正则

  • 否定参数

    <code><span class="pln">   find </span><span class="pun">.</span> <span class="pun">!</span> <span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="kwd">print</span></code>
  • 指定搜索深度

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">maxdepth </span><span class="lit">1</span> <span class="pun">-</span><span class="pln">type f  </span></code>


  • 按类型搜索:
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type d </span><span class="pun">-</span><span class="kwd">print</span>  <span class="com">//只列出所有目录</span></code>

    -type f 文件 / l 符号链接

  • 按时间搜索:
    -atime 访问时间 (单位是天,分钟单位则是-amin,以下类似)
    -mtime 修改时间 (内容被修改)
    -ctime 变化时间 (元数据或权限变化)

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">atime </span><span class="lit">7</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="kwd">print</span></code>
  • 按大小搜索:
    w字 k M G

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">size </span><span class="pun">+</span><span class="lit">2k</span></code>


    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">perm </span><span class="lit">644</span> <span class="pun">-</span><span class="kwd">print</span> <span class="com">//找具有可执行权限的所有文件</span></code>


    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">user weber </span><span class="pun">-</span><span class="kwd">print</span><span class="com">// 找用户weber所拥有的文件</span></code>


  • 删除:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.swp"</span> <span class="pun">-</span><span class="kwd">delete</span></code>
  • 执行动作(强大的exec)
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">user root </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> chown weber </span><span class="pun">{}</span><span class="pln"> \; </span><span class="com">//将当前目录下的所有权变更为weber</span></code>


    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">10</span> <span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="kwd">exec</span><span class="pln"> cp </span><span class="pun">{}</span><span class="pln"> OLD \;</span></code>
  • 结合多个命令
    tips: 如果需要后续执行多个命令,可以将多个命令写成一个脚本。然后 -exec 调用时执行脚本即可;

    <code>  <span class="pun">-</span><span class="kwd">exec</span> <span class="pun">./</span><span class="pln">commands</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">{}</span><span class="pln"> \;</span></code>


-print0 使用’\0’作为文件的定界符,这样就可以搜索包含空格的文件;

grep 文本搜索

grep match_patten file // 默认访问匹配行

  • 常用参数
    -o 只输出匹配的文本行 VS -v 只输出没有匹配的文本行
    -c 统计文件中包含文本的次数

    <code><span class="pln">  grep </span><span class="pun">-</span><span class="pln">c </span><span class="str">"text"</span><span class="pln"> filename</span></code>

    -n 打印匹配的行号
    -i 搜索时忽略大小写
    -l 只打印文件名

  • 在多级目录中对文本递归搜索(程序员搜代码的最爱):
    <code><span class="pln">  grep </span><span class="str">"class"</span> <span class="pun">.</span> <span class="pun">-</span><span class="pln">R </span><span class="pun">-</span><span class="pln">n</span></code>
  • 匹配多个模式
    <code><span class="pln">  grep </span><span class="pun">-</span><span class="pln">e </span><span class="str">"class"</span> <span class="pun">-</span><span class="pln">e </span><span class="str">"vitural"</span><span class="pln"> file</span></code>
  • grep输出以\0作为结尾符的文件名:(-z)
    <code><span class="pln">  grep </span><span class="str">"test"</span><span class="pln"> file</span><span class="pun">*</span> <span class="pun">-</span><span class="pln">lZ</span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="lit">0</span><span class="pln"> rm</span></code>

xargs 命令行参数转换

xargs 能够将输入数据转化为特定命令的命令行参数;这样,可以配合很多命令来组合使用。比如grep,比如find;

  • 将多行输出转化为单行输出
    cat file.txt| xargs
    \n 是多行文本间的定界符
  • 将单行转化为多行输出
    cat single.txt | xargs -n 3


-d 定义定界符 (默认为空格 多行的定界符为 \n)
-n 指定输出为多行
-I {} 指定替换字符串,这个字符串在xargs扩展时会被替换掉,用于待执行的命令需要多个参数时

<code><span class="pln">cat file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="pln">I </span><span class="pun">{}</span> <span class="pun">./</span><span class="pln">command</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-</span><span class="pln">p </span><span class="pun">{}</span> <span class="pun">-</span><span class="lit">1</span></code>


<code><span class="pln">find source_dir</span><span class="pun">/</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.cpp"</span> <span class="pun">-</span><span class="pln">print0 </span><span class="pun">|</span><span class="pln">xargs </span><span class="pun">-</span><span class="lit">0</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span></code>

sort 排序

-n 按数字进行排序 VS -d 按字典序进行排序
-r 逆序排序
-k N 指定按第N列排序

<code><span class="pln">sort </span><span class="pun">-</span><span class="pln">nrk </span><span class="lit">1</span><span class="pln"> data</span><span class="pun">.</span><span class="pln">txt
sort </span><span class="pun">-</span><span class="pln">bd data </span><span class="com">// 忽略像空格之类的前导空白字符</span></code>

uniq 消除重复行

  • 消除重复行
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span></code>
  • 统计各行在文件中出现的次数
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span><span class="pun">-</span><span class="pln">c</span></code>
  • 找出重复行
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span><span class="pun">-</span><span class="pln">d</span></code>

    可指定每行中需要比较的重复内容:-s 开始位置 -w 比较字符数


  • 通用用法
    <code><span class="pln">  echo </span><span class="lit">12345</span> <span class="pun">|</span><span class="pln"> tr </span><span class="str">'0-9'</span> <span class="str">'9876543210'</span> <span class="com">//加解密转换,替换对应字符</span><span class="pln">
      cat text</span><span class="pun">|</span><span class="pln"> tr </span><span class="str">'\t'</span> <span class="str">' '</span>  <span class="com">//制表符转空格</span></code>
  • tr删除字符
    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">d </span><span class="str">'0-9'</span> <span class="com">// 删除所有数字</span></code>

    -c 求补集

    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">c </span><span class="str">'0-9'</span> <span class="com">//获取文件中所有数字</span><span class="pln">
      cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">d </span><span class="pun">-</span><span class="pln">c </span><span class="str">'0-9 \n'</span>  <span class="com">//删除非数字数据</span></code>
  • tr压缩字符
    tr -s 压缩文本中出现的重复字符;最常用于压缩多余的空格

    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">s </span><span class="str">' '</span></code>
  • 字符类
    使用方法:tr [:class:] [:class:]

    <code><span class="pln">  eg</span><span class="pun">:</span><span class="pln"> tr </span><span class="str">'[:lower:]'</span> <span class="str">'[:upper:]'</span></code>

cut 按列切分文本

  • 截取文件的第2列和第4列:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">f2</span><span class="pun">,</span><span class="lit">4</span><span class="pln"> filename</span></code>
  • 去文件除第3列的所有列:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">f3 </span><span class="pun">--</span><span class="pln">complement filename</span></code>
  • -d 指定定界符:
    <code><span class="pln">  cat </span><span class="pun">-</span><span class="pln">f2 </span><span class="pun">-</span><span class="pln">d</span><span class="str">";"</span><span class="pln"> filename</span></code>
  • cut 取的范围
    N- 第N个字段到结尾
    -M 第1个字段为M
    N-M N到M个字段
  • cut 取的单位
    -b 以字节为单位
    -c 以字符为单位
    -f 以字段为单位(使用定界符)
  • eg:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">c1</span><span class="pun">-</span><span class="lit">5</span><span class="pln"> file </span><span class="com">//打印第一到5个字符</span><span class="pln">
      cut </span><span class="pun">-</span><span class="pln">c</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> file  </span><span class="com">//打印前2个字符</span></code>

paste 按列拼接文本


<code><span class="pln">cat file1
</span><span class="lit">1</span>
<span class="lit">2</span><span class="pln">

cat file2

paste file1 file2
</span><span class="lit">1</span><span class="pln"> colin
</span><span class="lit">2</span><span class="pln"> book</span></code>

paste file1 file2 -d “,”

wc 统计行和字符的工具

wc -l file // 统计行数
wc -w file // 统计单词数
wc -c file // 统计字符数

sed 文本替换利器

  • 首处替换
    <code><span class="pln">  seg </span><span class="str">'s/text/replace_text/'</span><span class="pln"> file   </span><span class="com">//替换每一行的第一处匹配的text</span></code>
  • 全局替换
    <code><span class="pln">   seg </span><span class="str">'s/text/replace_text/g'</span><span class="pln"> file</span></code>


    <code><span class="pln">  seg </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s/text/repalce_text/g'</span><span class="pln"> file</span></code>
  • 移除空白行:
    <code><span class="pln">  sed </span><span class="str">'/^$/d'</span><span class="pln"> file</span></code>
  • 变量转换

    <code><span class="pln">echo </span><span class="kwd">this</span> <span class="kwd">is</span><span class="pln"> en example </span><span class="pun">|</span><span class="pln"> seg </span><span class="str">'s/\w+/[&amp;]/g'</span><span class="pln">
    $</span><span class="pun">&gt;[</span><span class="kwd">this</span><span class="pun">]</span>  <span class="pun">[</span><span class="kwd">is</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">en</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">example</span><span class="pun">]</span></code>
  • 子串匹配标记
    第一个匹配的括号内容使用标记 \1 来引用

    <code><span class="pln">  sed </span><span class="str">'s/hello\([0-9]\)/\1/'</span></code>
  • 双引号求值

    <code><span class="pln">  sed </span><span class="str">'s/$var/HLLOE/'</span> </code>


    <code><span class="pln">eg</span><span class="pun">:</span><span class="pln">
    p</span><span class="pun">=</span><span class="pln">patten
    r</span><span class="pun">=</span><span class="pln">replaced
    echo </span><span class="str">"line con a patten"</span> <span class="pun">|</span><span class="pln"> sed </span><span class="str">"s/$p/$r/g"</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">line con a replaced</span></code>
  • 其它示例
    字符串插入字符:将文本中每行内容(PEKSHA) 转换为 PEK/SHA

    <code><span class="pln">  sed </span><span class="str">'s/^.\{3\}/&amp;\//g'</span><span class="pln"> file</span></code>

awk 数据流处理工具

  • awk脚本结构
    awk ‘ BEGIN{ statements } statements2 END{ statements } ‘
  • 工作方式
  • 使用不带参数的print时,会打印当前行;
    <code><span class="pln">  echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"line1\nline2"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'BEGIN{print "start"} {print } END{ print "End" }'</span> </code>
  • print 以逗号分割时,参数以空格定界;
    <code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' {var1 = "v1" ; var2 = "V2"; var3="v3"; \
    print var1, var2 , var3; }'</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">v1 V2 v3</span></code>
  • 使用-拼接符的方式(””作为拼接符);
    <code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' {var1 = "v1" ; var2 = "V2"; var3="v3"; \
    print var1"-"var2"-"var3; }'</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">v1</span><span class="pun">-</span><span class="pln">V2</span><span class="pun">-</span><span class="pln">v3</span></code>

特殊变量: NR NF $0 $1 $2


<code><span class="pln">echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"line1 f2 f3\n line2 \n line 3"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print NR":"$0"-"$1"-"$2}'</span></code>
  • 打印每一行的第二和第三个字段:
    <code><span class="pln">  awk </span><span class="str">'{print $2, $3}'</span><span class="pln"> file</span></code>
  • 统计文件的行数:
    <code><span class="pln">  awk </span><span class="str">' END {print NR}'</span><span class="pln"> file</span></code>
  • 累加每一行的第一个字段:
    <code><span class="pln">  echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"1\n 2\n 3\n 4\n"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'BEGIN{num = 0 ;
      print "begin";} {sum += $1;} END {print "=="; print sum }'</span></code>


<code><span class="kwd">var</span><span class="pun">=</span><span class="lit">1000</span><span class="pln">
echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print vara}'</span><span class="pln"> vara</span><span class="pun">=</span><span class="pln">$var </span><span class="com">#  输入来自stdin</span><span class="pln">
awk </span><span class="str">'{print vara}'</span><span class="pln"> vara</span><span class="pun">=</span><span class="pln">$var file </span><span class="com"># 输入来自文件</span></code>


awk ‘NR < 5’ #行号小于5
awk ‘NR==1,NR==4 {print}’ file #行号等于1和4的打印出来
awk ‘/linux/’ #包含linux文本的行(可以用正则表达式来指定,超级强大)
awk ‘!/linux/’ #不包含linux文本的行


awk -F: ‘{print $NF}’ /etc/passwd



<code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{"grep root /etc/passwd" | getline cmdout; print cmdout }'</span> </code>


for(i=0;i<10;i++){print $i;}
for(i in array){print array[i];}


<code><span class="pln">seq </span><span class="lit">9</span><span class="pun">|</span><span class="pln"> \
awk </span><span class="str">'{lifo[NR] = $0; lno=NR} \
END{ for(;lno&gt;-1;lno--){print lifo[lno];}
} '</span></code>


  • head:
    <code><span class="pln">  awk </span><span class="str">'NR&lt;=10{print}'</span><span class="pln"> filename</span></code>
  • tail:
    <code><span class="pln">  awk </span><span class="str">'{buffer[NR%10] = $0;} END{for(i=0;i&lt;11;i++){ \
      print buffer[i %10]} } '</span><span class="pln"> filename</span></code>


  • awk方式实现:
    <code><span class="pln">  ls </span><span class="pun">-</span><span class="pln">lrt </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $6}'</span></code>
  • cut方式实现
    <code><span class="pln">  ls </span><span class="pun">-</span><span class="pln">lrt </span><span class="pun">|</span><span class="pln"> cut </span><span class="pun">-</span><span class="pln">f6</span></code>


  • 确定行号
    <code><span class="pln">  seq </span><span class="lit">100</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'NR==4,NR==6{print}'</span></code>
  • 确定文本
    打印处于start_pattern 和end_pattern之间的文本;

    <code><span class="pln">  awk </span><span class="str">'/start_pattern/, /end_pattern/'</span><span class="pln"> filename</span></code>


    <code><span class="pln">seq </span><span class="lit">100</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'/13/,/15/'</span><span class="pln">
    cat </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">passwd</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'/mai.*mail/,/news.*news/'</span></code>



<code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{"grep root /etc/passwd" | getline cmdout; print length(cmdout) }'</span> </code>

printf 类似c语言中的printf,对输出进行格式化

<code><span class="pln">seq </span><span class="lit">10</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'{printf "-&gt;%4s\n", $1}'</span></code>


1. 迭代文件中的每一行

  • while 循环法
    <code><span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span>
    <span class="kwd">do</span><span class="pln">
    echo $line</span><span class="pun">;</span>
    <span class="kwd">done</span> <span class="pun">&lt;</span><span class="pln"> file</span><span class="pun">.</span><span class="pln">txt
    </span><span class="pun">改成子</span><span class="pln">shell</span><span class="pun">:</span><span class="pln">
    cat file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span> <span class="pun">(</span><span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span><span class="kwd">do</span><span class="pln"> echo $line</span><span class="pun">;</span><span class="kwd">done</span><span class="pun">)</span></code>
  • awk法:
    cat file.txt| awk ‘{print}’


<code><span class="kwd">for</span><span class="pln"> word </span><span class="kwd">in</span><span class="pln"> $line</span><span class="pun">;</span>
<span class="kwd">do</span><span class="pln"> 
echo $word</span><span class="pun">;</span>
<span class="kwd">done</span></code>

3. 迭代每一个字符


<code><span class="kwd">for</span><span class="pun">((</span><span class="pln">i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln">i</span><span class="pun">&lt;</span><span class="pln">$</span><span class="pun">{#</span><span class="pln">word</span><span class="pun">};</span><span class="pln">i</span><span class="pun">++))</span>
<span class="kwd">do</span><span class="pln">
echo $</span><span class="pun">{</span><span class="pln">word</span><span class="pun">:</span><span class="pln">i</span><span class="pun">:</span><span class="lit">1</span><span class="pun">);</span>
<span class="kwd">done</span></code>



.Net 高效开发之不可错过的实用工具

工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节,让开发效率翻倍。

  1. Visual Studio
    1. Visual Studio Productivity Power tool: VS 专业版的效率工具。
    2. Web Essentials: 提高开发效率,能够有效的帮助开发人员编写CSS, JavaScript, HTML 等代码。
    3. MSVSMON: 远程Debug 监控器 (msvsmon.exe) 是一种轻量级的应用程序,能够远程控制VS来调试程序。在远程调试期间,VS 在调试主机运行,MSVSMON 在远程机器中运行。
    4. WIX toolset: 可以将XML 源代码文件编译成Windows 安装包。
    5. Code digger: Code Digger 是VS 2012/2013 的扩展插件,能够帮助开发人员分析代码。
    6. CodeMaid: CodeMaid 是一款开源的VS2012/2013/2015 插件,提供代码分析,清理,简化代码的功能。
    7. OzCode: 非常强大的VS 调试工具。
    8. CodeRush: 是VS的提高代码重构和提升效率的VS插件。
    9. T4 Text Template:VS中T4 文本模板是生成代码文件最常用的模板文件,这种模板文件是通过编写文本块和控制逻辑来实现的。
    10. Indent Guides:  快速添加缩进行。
    11. PowerShell Tools:支持开发和调试PowerShell 脚本和VS2015代码块的工具包。
    12. Visual Studio Code: 免费的跨平台编辑器,可以编译和调试现代的Web和云应用。
  2. ASP.NET
    1. Fiddler: 能够捕获 http 请求/响应来模拟请求行为。
    2. AutoMapper: 自动生成对象到对象的映射代码,比如,能够生成从实体对象映射到域对象,而不是手动编写映射代码。Object to object mapping. Like, the tool can be used to map entity objects to domain objects instead of writing manual mapping code.
    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: Web.config 图形化编辑器
  3. WCF
    1. SOAP UI: API 测试工具,支持所有标准的协议和技术。
    2. WireShark:UNIX和Windows系统的网络协议分析器。用于捕获TCP 层的拥塞状况,还能帮你过滤无效信息。
    3. Svc TraceViewer: 提供文件追踪视图,是由WFO提供的。
    4. Svc Config Editor: 用于管理WCF相关配置的图形化界面工具。
  4. MSMQ
    1. QueueExplorer 3.4: 提供消息操作功能,如复制,删除,移动消息,保存和加载,强压测试,浏览编辑等
  5. LINQ
    1. LINQ Pad: LINQPad 是一个轻量级工具,用来测试Linq查询。 可以测试由不同语言写的.Net 语言脚本。
    2. LINQ Insight: LINQ Insight Express 可嵌入 Visual Studio 中,能够分析设计时的LINQ查询 。
  6. RegEx
    1. RegEx tester: 正则表达式插件。
    2. regexr: 在线正则表达式开发和测试工具。
    3. regexpal: 在线正则表达式开发和测试工具。
    4. Expresso: 桌面版的正则表达式工具。
    5. RegexMagic : 能够根据文本模式自动生成正则表达式的工具。
  7. Javascript/JQuery/AngularJS
    1. JSHint: JavaScript代码质量监控工具,定义了很多非常严格的规则。
    2. JSFiddle: 提供了浏览器内部的开发环境,能够测试HTML,CSS,Javascript/JQuery代码
    3. Protractor: 端到端的框架,能够测试Angular应用。
  8. SQL Server
    1. SQL Profiler: SQL 跟踪监控工具。
    2. ExpressProfiler: ExpressProfiler (aka SqlExpress Profiler) 是一个小型快速的SQL Server Profiler的替换工具,自带GUI界面。能够用于企业版和非企业版 的SQL Server。
    3. SQL Sentry Plan explorer: 提供了SQL 查询执行计划的很好的物理视图。
    4. SQL Complete: 为 SQL Server Management Studio and Visual Studio 提供非常智能的,优化SQL 格式的管理工具。
    5. NimbleText:文本操作和代码生成工具。
    6. Query Express: 轻量级的SQL 查询分析器。
    7. IO Meter: 提供IO 子系统的一些访问具体情况
    8. sqldecryptor: 可以解密SQL Server 中的加密对象,如存储过程,方法,触发器,视图。
    9. SpatialViewer: 可以预览和创建空间数据。
    10. ClearTrace: 导入跟踪和分析文件,并显示汇总信息。
    11. Internals Viewer for SQL Server: Internals Viewer 用来在SQL Server 的存储引擎中的查找工具,以及获取数据在物理层是如何分配,组织和存储的。
  9. NHibernate
    1. NHibernate Mapping Generator : 生成 NHibernate 映射文件,并从存在的数据库表映射到领域类。
  10. ​Tally
    1. Tally ERP 9
    2. Tally dll: .net 的动态链接库,能够将Tally Accounting 软件集成到应用程序中 ,通过代码对数据进行push或pull操作。
  11. 代码Review
    1. StyleCop: StyleCop 是静态代码分析工具,能够统一设置代码样式和规范。 可以在Visual Studio 中使用,也可以集成到 MSBuild 项目。
    2. FxCop: FxCop 是静态代码分析工具,能够通过分析.Net 程序集保证开发标准。
  12. 运行状况捕获
    1. WireShark: It is a network protocol analyzer for Unix and Windows. It can capture traffic at TCP level.
    2. HTTP Monitor: enables the developer to view all the HTTP traffic between your computer and the Internet. This includes the request data (such as HTTP headers and form GET and POST data) and the response data (including the HTTP headers and body).
  13. 诊断工具
    1. Glimpse:提供服务器端诊断数据。如 在ASP.NET MVC 项目,可以通过NuGet添加。
  14. 性能
    1. PerfMon: 使用 性能计数器监控系统性能。
  15. 代码转换器
    1. Telerik Code Converter: C# 到 VB 及 VB 到C# 代码转换器. I是一个在线编辑工具,可以选择 ‘Batch Converter’ ,并使用压缩包上传文件。
  16. 屏幕记录工具
    1. Wink: Using Wink, 可以轻松截图,并为截图添加描述等,也可以录制Demo。
  17. 文本编辑器
    1. Notepad++: 源码编辑器
    2. Notepad2: 轻量级功能丰富的文本编辑器
    3. sublimetext:富文本编辑器
  18. 文档工具
    1. GhostDoc: GhostDoc 是 Visual Studio 扩展项,能够自动生成 方法或属性的 文档注释,包括它们的类型,名称,其他上下文信息。
    2. helpndoc: helpndoc 用于创建帮助文档工具,能够根据文档源生成多种格式。
  19. 其他
    1. FileZilla: FileZilla 是开源的FTP 工具. 通过FileZilla 客户端可以将文件上传到FTP 服务器上。
    2. TreeTrim: TreeTrim 是调整代码的工具,能够删除一些无效的debug文件和临时文件等。
    3. BrowserStack: 支持跨浏览器测试的工具。
    4. BugShooting: 屏幕截图软件,能够铺货和附加工作项,bug,问题跟踪项等。
    5. Postman: REST 客户端,能够发送http请求,分析REST 应用程序发出的响应。
    6. Web developer checklist: checklist可用来管理开发计划
    7. PowerGUI: 能够快接收和使用PowerShell 来有效管理 Windows 开发环境。
    8. Beyond Compare: 提供文件对比功能。
    9. PostMan: REST Chrome 器扩展项
    10. Devart Codecompare: 文件区分工具,能够读取 C#, C++,VB 代码结构 。包括:文件夹对比工具,独立App 比较合并文件夹和文件,代码review 支持。