Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19732144
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2009-05-31 16:31:47

§2.4  unix shell

     Alias

定义:alias nss netstat –lptn 使用:nssnss | grep 80

定义:alias achoo echo "|%l|" %l表示插入行。使用:achooachoo these are args

In [36]: alias achoo echo first: "|%s|", second: "|%s|"

 

In [37]: achoo foo bar

first: |foo|, second: |bar|

 

In [40]: achoo foo bar bam

first: |foo|, second: |bar| bam

 

这些别名的存储:

In [5]: store achoo

Alias stored: achoo (2, 'echo first: "|%s|", second: "|%s|"')

 

     执行shell

加叹号就可以了。

!netstat –lptn

In [1]: user = 'jmjones'

In [2]: process = 'bash'

In [3]: !ps aux | grep $user | grep $process

也可以这样:

In [4]: l = !ps aux | grep $user | grep $process

In [5]: l

 

这样结果存储在列表l中,更加整洁。

!!和!类似,不过不可以将结果存储在变量中,可以通过_ _[0-9]*访问历史命令。

 

     Rehash

shell命令创建别名。

__IP实际是interactive shell object

In [1]: __IP.alias_table

Out[1]:

{'achoo': (2, 'echo first: "|%s|", second: "|%s|"'),

 'cat': (0, 'cat'),

 'clear': (0, 'clear'),

 'cp': (0, 'cp -i'),

 'lc': (0, 'ls -F -o --color'),

 'ldir': (0, 'ls -F -o --color %l | grep /$'),

 'less': (0, 'less'),

 'lf': (0, 'ls -F -o --color %l | grep ^-'),

 'lk': (0, 'ls -F -o --color %l | grep ^l'),

 'll': (0, 'ls -lF'),

 'ls': (0, 'ls -F'),

 'lx': (0, 'ls -F -o --color %l | grep ^-..x'),

 'mkdir': (0, 'mkdir'),

 'mv': (0, 'mv -i'),

 'rm': (0, 'rm -i'),

 'rmdir': (0, 'rmdir')}

 

In [2]: type(__IP.alias_table)

Out[2]:

 

In [3]: len(__IP.alias_table)

Out[3]: 16

 

In [4]: rehash

 

In [5]: len(__IP.alias_table)

Out[5]: 3269

 

     Rehashx

把认为可以执行的加进去。两者的详细区别介绍参见教材。

     Cd

Python中使用os.chdir()os.getcwd()

Ipython中有cdcd -, cd -q /tmp, pwdcd -b t等。进入历史目录的的功能参见教材。比如:cd -6

 

     Bookmark

可以跨会话保存。

bookmark muzak /home/jmjones/local/Music

bookmark –l

bookmark -d ulb

bookmark –r 全部清空

 

     Dhist 目录历史

 

Dhist

cd -

dhist 5

dhist 3 7

 

     Pwd

     变量扩展

Pythonshell变量的互传。

In [16]: for i in range(10):

   ....:     !date > ${i}.txt

   ....:    

   ....:    

 

# ls

0.txt  3.txt  6.txt  9.txt                                                down   now

1.txt  4.txt  7.txt  code of

 

     字符串处理

Shellps aux | awk '{if ($1 == "jmjones") print $2}'的实现。

ps = !ps aux

查找:ps.grep('lighttpd')

排除查找:ps.grep('Mar07', prune=True)

 

In [1]: import os

In [2]: file_list = !ls

In [3]: file_list

Out[3]: SList (.p, .n, .l, .s, .grep(), .fields() available). Value:

0: ch01.xml

1: code

2: ipython.pdf

3: ipython.xml

 

In [4]: file_list.grep(os.path.isfile)

Out[4]: SList (.p, .n, .l, .s, .grep(), .fields() available). Value:

0: ch01.xml

1: ipython.pdf

2: ipython.xml

 

类似的有file_list.grep(os.path.isdir)

打印制定列:ps.grep('Mar07', prune=True).fields(0, 1, 8)

ps.fields(0, 1).grep('jmjones').fields(1)

 

ps.fields(0, 1).grep('jmjones').fields(1).s

Out[6]: '5385 5388 5423 5425 5429 5431 5437 5440 5444 5452 5454 5457 5458 5468

5470 5478 5480 5483 5489 5562 5568 5593 5595 5597 5598 5618 5621 5623 5628 5632

5640 5740 5742 5808 5838 12707 12913 14391 14785 19301 21340 23024 23025 23373

23374 23375'

.s属性还不太了解。估计是存储为一个字符串。

 

     sh Profile

 

# ipython -p sh

/usr/local/lib/python2.6/site-packages/IPython/Magic.py:38: DeprecationWarning: the sets module is deprecated

  from sets import Set

IPython 0.9.1   [on Py 2.6.2]

[~]|1> import os

[~]|2> os.environ['PATH']

   <2> '/opt/ActivePerl-5.10/site/bin:/opt/ActivePerl-5.10/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin

在后面添加PATH4> env PATH+=:/appended

在前面添加PATH 5> env PATH-=/prepended:

查看:os.environ['PATH']

查看修改:

4> env -p

<4> {'add': [('PATH', ':/appended')], 'pre': [('PATH', '/prepended:')], 'set': {}}

取消修改:

[~/tmp]|5> env -d PATH

Forgot 'PATH' (for next session)

 

查找文件:3> mglob rec:*py

3> mglob dir:*

阅读(4072) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~