2012年12月17日星期一

Object serialization

From: http://www.javacoffeebreak.com/articles/serialization/index.html

Object Persistence Made Easy

With object serialization, your Java applets and applications can save and load the state of objects to disk or over a network. In this article, we'll examine the benefits of object serialization, and how to implement it in your own programs. By David Reilly.
One of the most critical tasks that applications have to perform is to save and restore data. Whether it be a word processing application that saves documents to disk, a utility that remembers its configuration for next time, or a game that sets aside world domination for the night, the ability to store data and later retrieve it is a vital one. Without it, software would be little more effective that the typewriter - users would have to re-type the data to make further modifications once the application exits.

Take it from someone who's been there - creating save & restore functions is not a fun task.

Writing the code for saving data, however, can become boring repetitive work. First, the programmer must create a specification document for the proposed file structure. Next, the programmer must implement save and restore functions that convert object data to & from primitive data types, and test it with sample data. 
If the application later requires new data to be stored, the file specification must be modified, as well as the save and restore methods. Take it from someone who's been there - creating save & restore functions is not a fun task.
The solution to this is object serialization. Object serialization takes an object's state, and converts it to a stream of data for you. With object serialization, it's an easy task to take any object, and make it persistent, without writing custom code to save object member variables. The object can be restored at a later time, and even a later location. With persistence, we can move an object from one computer to another, and have it maintain its state. This very cool feature, in Java, also happens to be very easy to use.

Serializing objects

Java makes it easy to serialize objects. Any object whose class implements the java.io.Serializable interface can be made persistent with only a few lines of code. No extra methods need to be added to implement the interface, however - the purpose of the interface is to identify at run-time which classes can be safely serialized, and which cannot. You, as a programmer, need only add the implements keyword to your class declaration, to identify your classes as serializable.
public class UserData implements
       java.io.Serializable
Now, once a class is serializable, we can write the object to anyOutputStream, such as to disk or a socket connection. To achieve this, we must first create an instance of java.io.ObjectOutputStream, and pass the constructor an existing OutputStream instance.
// Write to disk with FileOutputStream
FileOutputStream f_out = new 
 FileOutputStream("myobject.data");

// Write object with ObjectOutputStream
ObjectOutputStream obj_out = new
 ObjectOutputStream (f_out);

// Write object out to disk
obj_out.writeObject ( myObject );
Note that any Java object that implements the serializable interface can be written to an output stream this way - including those that are part of the Java API. Furthermore, any objects that are referenced by a serialized object will also be stored. This means that arrays, vectors, lists, and collections of objects can be saved in the same fashion - without the need to manually save each one. This can lead to significant time and code savings.

Restoring objects from a serialized state

Reading objects back is almost as easy. The one catch is that at runtime, you can never be completely sure what type of data to expect. A data stream containing serialized objects may contain a mixture of different object classes, so you need to explicitly cast an object to a particular class. If you've never cast an object before, the procedure is relatively straightforward. First check the object's class, using the instanceofoperator. Then cast to the correct class.
// Read from disk using FileInputStream
FileInputStream f_in = new 
 FileInputStream("myobject.data");

// Read object using ObjectInputStream
ObjectInputStream obj_in = 
 new ObjectInputStream (f_in);

// Read an object
Object obj = obj_in.readObject();

if (obj instanceof Vector)
{
 // Cast object to a Vector
 Vector vec = (Vector) obj;

 // Do something with vector....
}

Further issues with serialization

As you can see, it's relatively easy to serialize an object. Whenever new fields are added to an object, they will be saved automatically, without requiring modification to your save and restore code. However, there are some cases where this behavior is not desirable. For example, a password member variable might not be safe to transmit to third parties over a network connection, and might need to be left blank. In this case, the transient keyword can be used. The transient field indicates that a particular member variable should not be saved. Though not used often, it's an important keyword to remember.
public class UserSession implements 
         java.io.Serializable
{
 String username;
 transient String password;
}

Summary

Java's support for object serialization makes the implementation of persistent objects extremely easy. In contrast, the amount of code required to save and restore every field of an object is complex and repetitive work. While it is certainly possible to write your own serialization mechanism, the simplicity of that provided by Java would be hard to beat.
Serialization benefits programmers by
  • Reducing time taken to write code for save and restoration of object or application state
  • Eliminating complexity of save and restore operations, and avoiding the need for creating a new file format
  • Making it easier for objects to travel over a network connection.
With relatively little effort, you can apply serialization to a variety of tasks. Not only do applications benefit from serialization, but also applets. Rather than specifying a long list of parameters, or performing time consuming initialization and parsing, an applet can simple reload a configuration object whose member variables contain all the information needed to execute. It's not just useful for Java applications - even applets can make benefit, by loading their configuration details or parameters. With a little imagination, serialization may just have a place in your next project.

2012年12月16日星期日

Why We Created Julia

http://julialang.org/blog/2012/02/why-we-created-julia/

In short, because we are greedy.
We are power Matlab users. Some of us are Lisp hackers. Some are Pythonistas, others Rubyists, still others Perl hackers. There are those of us who used Mathematica before we could grow facial hair. There are those who still can’t grow facial hair. We’ve generated more R plots than any sane person should. C is our desert island programming language.
We love all of these languages; they are wonderful and powerful. For the work we do — scientific computing, machine learning, data mining, large-scale linear algebra, distributed and parallel computing — each one is perfect for some aspects of the work and terrible for others. Each one is a trade-off.
We are greedy: we want more.
We want a language that’s open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled.
(Did we mention it should be as fast as C?)
While we’re being demanding, we want something that provides the distributed power of Hadoop — without the kilobytes of boilerplate Java and XML; without being forced to sift through gigabytes of log files on hundreds of machines to find our bugs. We want the power without the layers of impenetrable complexity. We want to write simple scalar loops that compile down to tight machine code using just the registers on a single CPU. We want to write A*B and launch a thousand computations on a thousand machines, calculating a vast matrix product together.
We never want to mention types when we don’t feel like it. But when we need polymorphic functions, we want to use generic programming to write an algorithm just once and apply it to an infinite lattice of types; we want to use multiple dispatch to efficiently pick the best method for all of a function’s arguments, from dozens of method definitions, providing common functionality across drastically different types. Despite all this power, we want the language to be simple and clean.
All this doesn’t seem like too much to ask for, does it?
Even though we recognize that we are inexcusably greedy, we still want to have it all. About two and a half years ago, we set out to create the language of our greed. It’s not complete, but it’s time for a 1.0 release — the language we’ve created is calledJulia. It already delivers on 90% of our ungracious demands, and now it needs the ungracious demands of others to shape it further. So, if you are also a greedy, unreasonable, demanding programmer, we want you to give it a try.

2012年12月7日星期五

git command


【转载】浅复制与深复制中的传值与传址


这个标题念起来有点拗口,但却是理解数据结构的关键。标题中的4个术语,对应的英文分别是:shallow copy(注意,不是shadow copy)、deep copy、pass by value、pass by reference(或pass by address)。传址和传引用是一回事。
一门编程语言的核心是数据结构,粗略来讲,可以把数据结构分成不可变类型(immutable)和可变类型(mutable)。为什么这么分呢?这涉及到内存分配问题。对于不可变类型,只要分配有限的内存空间即可,而对于可变类型,理论上则要分配没有大小限制的空间。因此,这么分是出于合理利用系统资源的考虑。实际上,栈内存和堆内存分别用于保存不可变类型值和可变类型值。
什么是不可变类型?就是该值一旦赋予某个变量,就只属于某个变量,不能同属于其他变量。如:
1 var stringValue = "I'm immutable data structure, mean you can't modify me!";
2 var anotherStringValue = stringValue;
3 stringValue = "I have changed";
此时,anotherStringValue中保存的值会不会也变成“I have changed”?不会。因为
1 var anotherStringValue = stringValue;
照stringValue中保存的字符串的原样,复制一个字符串(相应地,在内存中分配一块新空间),并将该字符串赋给anotherStringValue。换句话说,这两个变量虽然保存的值相同,但它们的值并不在一块内存中。因此,修改任何一个变量,都不会影响另一个变量。即
1 stringValue = "I have changed";
只会影响stringValue的值。但是,确切来讲,stringValue = “I have changed”;并不是修改stringValue,而是创建了一个新字符串(相应地,在内存中分配一块新空间),然后让stringValue引用该字符串——更像是替换变量的值;原来的字符串呢?因为没有变量引用它,也就成为垃圾了(当然,垃圾所占用的内存会被回收)。
由此可见,赋值操作对于不变类型而言,传递的是内存中的值本身。那么,对于可变类型呢?当然,传递的是内存中值的引用(或者说地址),而且无论传递多少次,内存中始终都只有一份原始值——毕竟可变类型大小莫测,只保存一份原始值能最大限度节省内存空间。例如:
1 var objectValue = {1:1,'s':'string','innerObject':{'innerArray' : [1,2,3]}};
2 var anotherObjectValue = objectValue;
3 objectValue[1] = 100;
4 anotherObjectValue[1]; //100
不言自明,这里的anotherObjectValue通过赋值操作,从objectValue那里只获得了对原始对象( {1:1,’s':’string’,'innerObject’:{‘innerArray’ : [1,2,3]}})的引用,也就是该对象在内存中的地址,或者说“门牌号码”。因此,通过objectValue修改原始对象的第一个元素(objectValue[1] = 100;),结果同样会在anotherObjectValue[1]那里得到反映——因为这两个变量共享同一份原始值。
在JavaScript中,给函数传递参数是按照上述默认约定——即对不可变类型,传值;对可变类型,传址——进行的。如:
1 function example(str, obj){
2 ……
3 }
4 example(stringValue,objectValue);
调用example函数时,第一个参数传递的是实际的字符串值,第二参数传递的是对象的引用(内存地址)。
在PHP中,定义函数时可以指定相应参数是传值还是传址——通常是传值。其实,这也很容易理解:假如函数要求为某个可变类型参数传值,而不是传址,那么也就意味着内存中会因此多出一份该类型值的副本。相应地,在函数中修改这份新副本,不会影响函数外的原副本。因为新旧副本在内存中就不是同一个地址。
说到这,也就引出了浅复制和深复制的概念。事实上,浅复制和深复制的区别恰恰在于复制可变类型时,是传值还是传址。如果是像往常一样传址(传引用),那么就是浅复制。如果是传值,那么就是深复制。浅复制和深复制到底有什么区别呢?以下面的Python代码为例:
复制代码
1 >>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
2 >>> y = x.copy()
3 >>> y['username'] = 'mlh'
4 >>> y['machines'].remove('bar')
5 >>> y
6 {'username': 'mlh', 'machines': ['foo', 'baz']}
7 >>> x
8 {'username': 'admin', 'machines': ['foo', 'baz']}
复制代码
调用字典x的copy方法返回一个新字典并赋值给y,新字典中带有与原字典相同的键-值对。注意,copy方法采用浅复制创建的新字典,与原字典有区别也有联系。区别体现在,对于原字典中不可变的值,如数字、字符串、元组等,会在新字典中重新生成一份新副本;因此,修改(实际上是替换,或者说是重新赋值)这些键的值(y['username'] = ‘mlh’)不会影响原字典。联系体现在,对于原字典中可变的值,如列表、字典,不会在新字典中生成新副本,而只复制值的引用,即新字典中相应的键保存的是引用,当然,原字典中相应的键保存的也是引用,而且这两个引用都指向同一块内存地址。这就是所谓的浅复制。因此,如果修改的是可变类型的值(y['machines'].remove(‘bar’)),就意味着修改了新、旧字典共享的值(即本例中的列表['foo', 'bar', 'baz']),因此一定会影响引用该值的原字典。
深复制则不然。深复制是实实在在地把原字典中所有的值全都照原样子重新创建一遍,无论是不变类型值,还是可变类型值。执行深复制后,内存中会存在两份完全一样的数据段,但分别处于不同内存空间中,即地址不同。而且,分别由不同变量(原字典、新字典)引用。因此,经过深复制后修改一个字典,不会影响另一个字典。Python的copy模块中的deepcopy函数可以实现深复制:
复制代码
 1 >>> from copy import deepcopy
 2 >>> d = {}
 3 >>> d['names'] = ['Alfred', 'Bertrand']
 4 >>> c = d.copy()
 5 >>> dc = deepcopy(d)
 6 >>> d['names'].append('Clive')
 7 >>> c
 8 {'names': ['Alfred', 'Bertrand', 'Clive']}
 9 >>> dc
10 {'names': ['Alfred', 'Bertrand']}
复制代码
显然,修改深复制得到的新值不会影响原值;而修改浅复制得到的“新”值,在某种程度上仍然会影响原值。

作者想表达的意思应该是在javascript中变量的复制和赋值,对于普通的变量而言,赋值仅仅是一个复制,而对于对象而言,赋值则是一个引用。
比如:var a=1;var b=a;
在这里,b其实是a的一个复制,所以b=1,正因为是复制,所以复制完后,b和a就没有任何关系了,当a重新赋值的时候,对于b则没有影响,同样,对于b再重新赋值,对a也没有影响 。
但是,如果a是一个对象,那就不一样了
例如var test ={a:1,b:2};var test1 =test;
在这样的情况下,test1就不再是test的复制了,而是直接取了test的地址,所以对于Test的值的改变,也影响到了test1,比如我test.a = 2,那么我test1.a的值也就自动变成了2
这点其实在PHP5里面已经也这样了,在PHP4的时候,对象的赋值也是一个复制,所以我们为了保证只有一个实例,往往都是采用:$a = &$b ;但是从5开始则不一样,对于对象而言,如果没有特别指定的操作,那么就相当于是对地址的一个引用。效果和上面的JS代码类似。
作者在最后举了一个PYTHON的例子来说明深复制,其实也就是为对象也做一个拷贝而不是采用引用,这个,当然在PHP里也有,clone就是实现的这个效果。


1. 浅拷贝是指源对象与拷贝对象共用一份实体,仅仅是引用的变量不同(名称不同)。对其中任何一个对象的改动都会影响另外一个对象。
利用python中的字典来简单说明下:
复制代码
 1 >>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
 2 >>> x
 3 {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
 4 >>> y = x.copy()
 5 >>> y
 6 {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
 7 >>> y['username'] = 'mlh'
 8 >>> y
 9 {'username': 'mlh', 'machines': ['foo', 'bar', 'baz']}
10 >>> x
11 {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
12 >>> y['machines'].remove('bar')
13 >>> y
14 {'username': 'mlh', 'machines': ['foo', 'baz']}
15 >>> x
16 {'username': 'admin', 'machines': ['foo', 'baz']}
复制代码
为什么可以改变username的值,并且未对原字典产生影响(迷惑中。。。),但是修改元组,则会反应在原字典上。
2. 深拷贝是指源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。
利用python中的字典来简单说明下:
复制代码
 1 >>> from copy import deepcopy
 2 >>> d = {}
 3 >>> d['names'] = ['Alfred', 'Bertrand']
 4 >>> d
 5 {'names': ['Alfred', 'Bertrand']}
 6 >>> c = d.copy()
 7 >>> c
 8 {'names': ['Alfred', 'Bertrand']}
 9 >>> dc = deepcopy(d)
10 >>> dc
11 {'names': ['Alfred', 'Bertrand']}
12 >>> d['names'].append('Clive')
13 >>> d
14 {'names': ['Alfred', 'Bertrand', 'Clive']}
15 >>> c
16 {'names': ['Alfred', 'Bertrand', 'Clive']}
17 >>> dc
18 {'names': ['Alfred', 'Bertrand']}
复制代码
这里为深复制,复制了一份键值相同的原字典的副本
3. 引用对象的浅拷贝原理,引用对象之间的赋值之所以执行的是浅拷贝动作,与引用对象的特性有关,一个引用对象一般来说由两个部分组成:
  (1). 一个具名的Handle,也就是我们所说的声明(如变量)
  (2). 一个内部(不具名)的对象,也就是具名Handle的内部对象。它在Manged Heap(托管堆)中分配,一般由新增引用对象的New方法是进行创建如果这个内部对象已被创建,那么具名的Handle就指向这个内部对象在Manged Heap中的地址,否则就是null(从某个方面来讲,如果这个具名的handle可以被赋值为null,说明这是一个引用对象,当然不是绝对)。两个引用对象如果进行赋值,它们仅仅是复制这个内部对象的地址,内部对象仍然是同一个,因此,源对象或拷贝对象的修改都会影响对方。这也就是浅拷贝。

http://docs.python.org/2/library/copy.html

2012年10月9日星期二

精准地道地表达观点——Introductory Words


如果在论文中你已经彻底厌倦了某人said, said, said,那我们可以考虑一下其他阐述观点的表达方式。但这并不意味着人名和动词的随机排列组合,语境和语气态度在这里非常重要。下面是一些在英国大学中广泛认可的表达方式以及适用语境,参考一下可以有效地避免过度重复以及语言不准确不地道的问题。

Ways of Presenting Ideas and Arguments

引入类型
Types of writing
适用表述
Ways you might present this in your essay
1
当作者对自己所陈述的内容非常肯定,坚定不移时
When the writer is very committed, even passionate, about the ideas he/she writes
X asserts that…
X strongly argues that…
X firmly believes that…
X is committed to the idea that…
2
当作者引述极为具体细致的论据来论证其观点时
When the writer presents detailed evidence in support of his/her ideas
The work of X shows that…
X concludes that…
X found that…
X discovered that…
X learned that…
3
当作者本身对其所引述或陈述的观点持谨慎保留的态度时
When the writer is quite cautious and restrained about the ideas he/she puts forward
Research by X indicates that…
X suggests that…
4
当作者仅凭个人经验表达观点时
When the writer puts forward ideas based on their personal experiences only
The work of X indicates that…
X believes that…
X feels that…
5
当作者引述在读者看来值得质疑的观点时
When the writer puts forward ideas you are rather suspicious or skeptical about
X claims that…
X believes that…
According to X…
6
当作者非常直率地陈述观点,并辅以相当显而易见且理由充分的论据支持时
When the writer appears to report something in a straight-forward way, backed up with apparently good reasons for the ideas presented
X states that…
X reports that…
X notes that…
X found that…
X observes that…
X concludes that…
As X points out, …
X has drawn attention to the fact that…
X argues that…

非常感谢Colin Neville教授,资料版权归Colin教授所有。
Special thanks go to Professor Colin Neville, copyright reserved. 

2012年6月21日星期四

Android Testing Harness

Our goal is to automatically generated action sequences which satisfied following two objectives:
1. The action sequence would guide the execution to cover most part of the program.
2. The actions in the sequence are all valid, i.e. program has corresponding reaction for the action.

Atif Memon has written several papers on GUI ripping of traditional desktop software, and he presented an event-flow graph model to guide the ripping process. While it comes to mobile applications, their approach would not be effective anymore. This is due to following reasons:
1. Android's gestures are far more than "clicking". If we just use clicking action, the whole ripping process may only lead to a low coverage rate, thus the whole ripping process becomes meaningless. On the other hand, if we  perform every action that a GUI component could possibly perform, first, the action sequence will be full of meaningless actions. Second, the ripping process would take a long time.
2. The GUI structure of Android interface would not remain unchanged. Because of the screens of mobile phone are all quite small, the app's developer often provide user their own right to arrange the UI layout based on their own preference. Thus, the event-flow graph get from the ripping process could be only part of the big picture. Once the layout becomes different than it be in the ripping process, the whole event flow model would not work.
3. The actions are no longer limited to the widget. The events that an Android program could respond to are far more than those actions could be fired on the widget. You can press "menu" and "back" button at any moment of the execution. You can swipe the whole screen so that it will goes to another screen. You can change the orientation of the phone so that the layout of the screen ,the elements show up, and the responding actions could all be changed. Atif's event flow model is based on the event can be carried out on a widget. The code coverage could be low if other events are not properly fired.

2012年3月21日星期三

Random Thought about Static Program Analysis

I've been doing research of analyzing Android application for a semester. Today, when I'm having bath, I guess I get some of the insight of the static program analysis research.
I think the program analysis research at the early stage is just use information get at compiling stage to
In the compiling process, compiler will translate the higher level language to the basic operation that the assembly language could recognize. When we do static analysis, we care about how some specific variable propagate in the whole execution process or which API call was trigger in what kind of context. The so called program behavior is just those kinds of things(Method invocation, variable propagation(including basic computation), reachability or dead code(The impact of certain code snippet to output of program) etc. To be complement) We can almost get any information through 3 structured representation: Control flow graph, Data flow graph, and Call graph.
In the current project I've been doing, we need to also handle the manifest file(A configuration for the components in Android application.) From the pure code perspective, we can regard this manifest file as the input of the program. Because we can get no information from it through the compiling procedure. But from the application perspective, as it's a static configuration file, it is part of the program. The information in it can use for static program analysis.
From this point, we can see that the static program analysis is actually a prediction of all the possible behavior the program could have in the runtime execution. So this behavior prediction can no longer get from the  the compiling information alone. Because the runtime behavior is not only rely on  the byte code of the program, it also rely on the configuration file of the program. That's where the inference rule comes in in our project.
Another thing worth noticing is that the interaction to the system itself. I still need to figure out how the current static analysis tool analyze the JDK code to get some insight of that.
When talking about access control, we are not only monitoring the program behavior of current program, we are more or less predicting what kind of operation it will execute when different possible program interact with current program interface.

2012年3月6日星期二

[zz]如何让alias永久生效

如何让alias永久生效?

alias(中文称为“别名”)允许使用更加简短的名称来重新定义 Linux 中的 Shell 命令,从而简化命令行的输入。如果经常与 CLI 打交道,那么使用 alias 不仅会节省时间,而且也能提高效率,真是一举两得的好事。

    * 基本用法: alias 的基本使用方法为:alias 新的命令='原命令 -选项/参数'。举例说明,alias l=‘ls -lsh' 将重新定义 ls 命令,现在只需输入 l 就可以列目录了。
    * 获知别名: 直接输入 alias 命令会列出当前系统中所有已经定义的命令别名。
    * 删除别名: 要删除一个别名,可以使用 unalias 命令,如 unalias l。

上面的是网上看到的,这个方法只是暂时的,如果重启一次就失效了,如果我们想永久生效一个命令怎么办呢?

打开 .bashrc (应该是ubuntu发行版的,其他发行版也许可以修改.bash_profile)可以看到

……

# some more ls aliases

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

……

方法1 :直接在我们的 环境变量文件中添加 alias xx='xxxxx'

方法2  : .bashrc 中有一句话

# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.

就是说可以另外新建一个文件用于专门存放自己的alias信息



$ cd

$ vi .bash_aliases

在文件中输入自己想设置的命令  alias rm='rm -i'  然后保存推出

$ source .bashrc   #让我们的环境生效

方法……----------------------请注意实际在centos5测试 发现应该是.bashrc

小结:

1.很多时候我们只注意了解决问题本身,但是往往忽略了其他的东西,在解决问题的过程中也是一个学习的机会,尤其不要忘了看软件源码之类的注释!突然发现 这个非常重要,就是因为看了注释,我才发现了这个方法,而我本来的目的是想对我自己的环境变量进行设置修改,在解决环境变量的问题同时我又顺带解决了这个 alias的问题,以后得牢记!!!

2.在查找网上的解决方法的时候,经常发现别人有的文件本机没有,例如.bashrc 和 .bash_profile  可能就是不同发行版上的,这个时候可以换个搜索方式,或者自己尝试……
分类: ubuntu

http://www.cnblogs.com/jjyoung/archive/2011/07/15/2107788.html


如果是当前的session
使用过的Linux命令之alias - 设置命令的别名,让 Linux 命令更简练

本文链接:http://codingstandards.iteye.com/blog/1145318   (转载请注明出处)


用途说明

设置命令的别名。在linux系统中如果命令太长又不符合用户的习惯,那么我们可以为它指定一个别名。虽然可以为命令建立“链接”解决长文件名的问题,但 对于带命令行参数的命令,链接就无能为力了。而指定别名则可以解决此类所有问题【1】。常用别名来简化ssh登录【见示例三】,使长命令变短,使常用的长 命令行变短,强制执行命令时询问等。


常用参数

格式:alias

格式:alias -p

显示当前设置的别名。



格式:alias name='command line'

设置别名。



格式:alias name

显示指定的别名设置。



格式:unalias name

取消指定的别名设置。


使用示例
示例一 解决RHEL5/CentOS5下vi不能语法高亮显示的问题

在/etc/profile的末尾添加vi的别名设置



alias vi="vim"


显示二 显示当前的别名设置

[root@jfht ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@jfht ~]# alias cp
alias cp='cp -i'
[root@jfht ~]#


示例三 ssh别名

ssh 别名 - alias server_name='ssh -v -l USERNAME IP ADDRESS',更改 server_name、USERNAME 及 IP 地址以适应你的需要。对于经常要使用 ssh 登录远程 shell 的同学来说,这是一个值得收藏的别名。



[root@jfht ~]# alias 180='ssh 192.168.1.180'
[root@jfht ~]# 180
ssh: connect to host 192.168.1.180 port 22: No route to host
[root@jfht ~]# alias 181='ssh 192.168.1.181'
[root@jfht ~]# 181
root@192.168.1.181's password:

[root@jfht ~]#


示例四 常见别名设置集锦

alias l="ls -l"

alias ll="ls -l"



alias lm='ls -al | more'


模拟DOS风格的命令

alias clr=clear
alias cls=clear
alias copy='cp -i'
alias del='rm -i'
alias delete='rm -i'
alias dir='ls -alg'
alias home='cd ~'
alias ls='ls -F'
alias md=mkdir
alias move='mv -i'
alias type=more

alias cd..='cd ..'



alias home='cd /home/dave/public_html'

alias list='ls -la'

alias attrib='chmod'
alias chdir='cd'
alias copy='cp'
alias cp='cp -i'
alias d='dir'
alias del='rm'
alias deltree='rm -r'
alias dir='/bin/ls $LS_OPTIONS --format=vertical'
alias edit='pico'
alias ff='whereis'
alias ls='/bin/ls $LS_OPTIONS'
alias mem='top'
alias move='mv'
alias mv='mv -i'
alias pico='pico -w -z'
alias rm='rm -i'
alias search='grep'
alias v='vdir'
alias vdir='/bin/ls $LS_OPTIONS --format=long'
alias which='type -path'
alias wtf='watch -n 1 w -hs'
alias wth='ps -uxa | more'


问题思考

1. 怎么取消指定别名?

2. 别名在shell脚本中有效吗?

3. 怎样列出所有别名?

4. 怎样取消所有别名?

5. 怎样执行ls命令本身,而不是别名?


相关资料

【1】linux系统下给命令指定别名alias命令用法

【2】10 个实用的 Bash alias

【3】鸟哥的私房菜 命令別名設定: alias, unalias

【4】Computer Hope Linux / Unix alias command

【5】alias command

【6】Linux宝库 alias——别名

【7】Linux安全网 linux alias命令参数及用法详解--linux定义命令别名alias
【8】jiacheo 在linux的alias中使用awk遇到的问题   http://codingstandards.iteye.com/blog/1145318




命          令:   alias

功能说明:设置指令的别名。

语  法:alias[别名]=[指令名称]

补充说明:用户可利用alias,自定指令的别名。若仅输入alias,则可列出目前所有的别名设置。 alias的效力仅及于该次登入的操作。若要每次登入是即自动设好别名,可在/etc/profile或自己的~/.bashrc中设定指令的别名。

    还有,如果你想给每一位用户都生效的别名,请把alias la='ls -al' 一行加在/etc/bashrc最后面,bashrc是环境变量的配置文件 /etc/bashrc和~/.bashrc 区别就在于一个是设置给全系统一个是设置给单用户使用.

参  数:若不加任何参数,则列出目前所有的别名设置。 资料来自 www.linuxso.com   Linux安全网

CentOS5.6自带的alias定义


[root@linuxso.com ~]#alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

有的系统里没有ll这个命令,原因就是没有定义ll='ls -l --color=tty'这个别名

利用alias可以把很长的命令变成任意我们喜欢的简短的

设置和修改alias命令别名格式很简单

alias ll='ls -l --color=tty'

如果想永久生效,就把这条写入到 /etc/bashrc里面

2012年2月29日星期三

[转载]Latex相关资源汇总

无法查看这则摘要。请 点击此处查看博文。