20091229

Oracle for Linux 8.1.6的一些命令(转寄)

发信人: plusy ( 哈哈), 信区: Database
标 题: Oracle for Linux 8.1.6的一些命令
发信站: BBS 水木清华站 (Mon Oct 21 00:12:03 2002), 站内信件

Oracle for Linux 8.1.6的一些命令
最近安装了一下,习惯windows的GUI,一时莫不找头脑,整理一下以便不走弯路

1. 启动服务类
agtctl 启动关闭Intellegent Agent,用于搜索节点
lsnrctl 启动关闭监听器
oemctrl 启动关闭OEM服务器
dbshut 关闭数据库
dbstart 启动数据库

2.配置类
dbassist Database Configuration Assistant,创建,切换,删除数据库实例
netca Net8 Configuration Assistant,配置网络服务等
omca 创建OEM的资料库

3.OEM的图形界面
oemapp dbastudio 启动Database studio
oemapp instance 启动Instance Manager
oemapp console 启动OEM Console
oemapp schema 启动Schema Manager
oemapp security 启动Security Manager
oemapp worksheet 启动Sqlplus Worksheet

4.其他
sqlldr 数据导入器
sqlplus 字符界面的客户端

--

※ 来源:・BBS 水木清华站 smth.edu.cn・[FROM: 210.34.2.236]

20091226

Host your own project with Trac

If you're in the initial phases of setting up a new software project,
one of the first things you should be thinking about is a project
collaboration site. A good project site enables you to do at least two
things:
Collect design documents and documentation in one place
Track and assign tasks/bugs/issues to developers
If used correctly, the project site can become a focal point for
everyone working on a particular project. Ideas, research and design
documents can all be collected in one place and collaborated over. At
the same time the site is a management tool enabling assignment and
tracking of tasks to a team of workers. This is surprisingly important
even for small teams: if your project is a two man thing, there is
still great benefit to knowing what the other person is working on and
being able to see his or her progress.
A simple solution for your project site is to pick different kinds of
software for different tasks. For example, you may choose to use
Eventum for bug and issue tracking, with a separate MediaWiki
installation set up for the documentation and design collaboration.
But wouldn't it be better to combine all of this functionality into a
single piece of software?
Trac is one such piece of software. It gives you issue tracking,
complete with SVN integration and wiki functionality, built into a
single application. An added bonus of having everything in a single
application is that you can make linked references to tickets,
milestones and wiki entries pretty much anywhere you want within the
application.

This changeset references ticket #3.
You also get a timeline which concisely summarizes what's happening
within the project, be it wiki edits or source code commits. This can
be a very popular feature for project developers – it gives everyone a
chance to see what's happening in the project, and also to get a
feeling for the 'aliveness' of the project.

Timeline showing both edits, source code commits and ticket updates.
Installing Trac
Here's Playing With Wire's accelerated setup guide for Trac.
Install the basic trac package using your preferred method (ports,
emerge, rpms etc).
Create a new folder for the trac website on your server.
cd /www/
mkdir mytrac
Use trac-admin to create the instance:
cd /www/mytrac
trac-admin `pwd` initenv
Answer the questions asked by trac-admin.
Once the questions have been answered, trac will give you some
instructions similiar to what's below:
Project environment for 'MyProject' created.

You may now configure the environment by editing the file:

/www/mytrac/conf/trac.ini

If you'd like to take this new project environment for a test drive,
try running the Trac standalone web server `tracd`:

tracd –port 8000 /www/mytrac

Then point your browser to http://localhost:8000/mytrac. There you can
also browse the documentation for your installed version of Trac,
including information on further setup (such as deploying Trac to a
real web server).

The latest documentation can also always be found on the project website:

http://trac.edgewall.org/
If you use SQLite, give +rw permissions to www for the database:
chown -R :www db
chmod -R g+rwX db
If you need to install new graphics, e.g. a new logo file you will
want to copy it into the actual htdocs folder:
/usr/local/share/trac/htdocs
Httpd Setup
How to configure your web server depends on both what server you're
running and what method you want to use for serving trac (cgi, fast
cgi or mod python). If you're going to run trac using CGI, you'll
basically want to link to the main trac cgi file, and also set up
serving of the supporting html documents. Here's a sample config file
for how it may look like with using Apache and CGI:
Alias /trac/chrome/common /usr/local/share/trac/htdocs
<Directory "/usr/local/share/trac/htdocs">
Order allow,deny
Allow from all
</Directory>

ScriptAlias /trac /usr/local/share/trac/cgi-bin/trac.cgi
<Location "/trac">
SetEnv TRAC_ENV "/www/mytrac"

AuthType Basic
AuthName "WireLoad Protected Area"
AuthUserFile /www/mytrac/.htpasswd
Require valid-user
</Location>
<Directory /usr/local/share/trac/cgi-bin>
Options -Indexes +ExecCGI
AllowOverride None
Allow from all

AuthType Basic
AuthName "WireLoad Protected Area"
AuthUserFile /www/mytrac/.htpasswd
Require valid-user
</Directory>
This is fairly straight forward. The most imporant part is,
ScriptAlias /trac /usr/local/share/trac/cgi-bin/trac.cgi
which sets up trac as a cgi script accessible by going to the /trac
address of the webhost.
For performance reasons, we don't want the CGI script to serve every
trac file. The following alias will override the /trac URL for the
theme related files:
Alias /trac/chrome/common /usr/local/share/trac/htdocs
This has to go before the ScriptAlias line.
User Accounts
Trac's login scheme is based on basic http authentication, which is
why we added a the AuthType sections in the config file above. In
fact, to log in to trac you simply authenticate with the web server
using a user name and password from the .htaccess file.
Every user you define in the .htaccess file (using htpasswd) will be
able to log in with some basic permissions. To configure the
permissions more precisely, use the trac-admin command. For instance,
to make the user with login 'aljungberg' an admin:
cd /www/mytrac
trac-admin `pwd` permission add aljungberg admin
trac-admin `pwd` permission add admin TRAC_ADMIN
This assigns the user 'aljungberg' to an admin group and gives the
admin group the TRAC_ADMIN permission set.
Notice that everyone who logs in gets the 'authenticated' group
permissions which are by default pretty useful. You can find what they
are by running this command:
trac-admin `pwd` permission list authenticated
It'll say something like:
User Action
——————————
authenticated BROWSER_VIEW
authenticated CHANGESET_VIEW
authenticated FILE_VIEW
authenticated LOG_VIEW
authenticated MILESTONE_VIEW
authenticated REPORT_SQL_VIEW
authenticated REPORT_VIEW
authenticated ROADMAP_VIEW
authenticated SEARCH_VIEW
authenticated TICKET_APPEND
authenticated TICKET_CHGPROP
authenticated TICKET_CREATE
authenticated TICKET_MODIFY
authenticated TICKET_VIEW
authenticated TIMELINE_VIEW
authenticated WIKI_CREATE
authenticated WIKI_MODIFY
authenticated WIKI_VIEW
Available actions:
BROWSER_VIEW, CHANGESET_VIEW, CONFIG_VIEW, FILE_VIEW, LOG_VIEW,
MILESTONE_ADMIN, MILESTONE_CREATE, MILESTONE_DELETE, MILESTONE_MODIFY,
MILESTONE_VIEW, REPORT_ADMIN, REPORT_CREATE, REPORT_DELETE,
REPORT_MODIFY, REPORT_SQL_VIEW, REPORT_VIEW, ROADMAP_ADMIN,
ROADMAP_VIEW, SEARCH_VIEW, TICKET_ADMIN, TICKET_APPEND,
TICKET_CHGPROP, TICKET_CREATE, TICKET_MODIFY, TICKET_VIEW,
TIMELINE_VIEW, TRAC_ADMIN, WIKI_ADMIN, WIKI_CREATE, WIKI_DELETE,
WIKI_MODIFY, WIKI_VIEW
To find out which permissions are available, check out the
TracPermissions documentation page.
Setting up the SVN hook
To allow SVN commits to close tickets using cool syntax like 'Fixes
#1′ in commit messages, an SVN hook has to be installed. Hook scripts
in SVN are described in the SVN documentation.
Enter a post-commit script in the hooks/ folder of your SVN repository:
REPOS="$1″
REV="$2″
LOG=`/usr/local/bin/svnlook log -r $REV $REPOS`
AUTHOR=`/usr/local/bin/svnlook author -r $REV $REPOS`

TRAC_ENV='/www/mytrac/'
TRAC_URL='http://mytrac.wireload.net/trac/'

/usr/local/bin/python /www/mytrac/trac-post-commit-hook \
-p "$TRAC_ENV" \
-r "$REV" \
-u "$AUTHOR" \
-m "$LOG" \
-s "$TRAC_URL"
You may have to download the actual script from the repository. Make
sure you get the right version. I initially accidentally got the
latest version since I grabbed it from the SVN, and it wasn't
compatible with trac 0.10.3 which I had installed.
Finally make sure the script can be run,
chmod a+rx post-commit
chmod a+x /www/mytrac/trac-post-commit-hook
The users who run the script must also be able to read and write to
the trac database. You can make sure this works by test submitting
some change set for analysis:
su -m wlaljungberg post-commit /home/mysvn/myproject/ 4
If the database isn't accessible you'll get an error message similar
to this one:
trac.core.TracError: The user root requires read _and_ write
permission to the database file /www/mytrac/db/trac.db and the
directory it is located in.
The hook is nice. Here's a description of what it does, quoted from
the actual script:
# It searches commit messages for text in the form of:
# command #1
# command #1, #2
# command #1 & #2
# command #1 and #2
#
# You can have more then one command in a message. The following commands
# are supported. There is more then one spelling for each command, to make
# this as user-friendly as possible.
#
# closes, fixes
# The specified issue numbers are closed with the contents of this
# commit message being added to it.
# references, refs, addresses, re
# The specified issue numbers are left in their current status, but
# the contents of this commit message are added to their notes.
# A fairly complicated example of what you can do is with a commit message
# of:
#
# Changed blah and foo to do this or that. Fixes #10 and #12, and refs #12.
#
# This will close #10 and #12, and add a note to #12.
If you run into any trouble, take a look at the excellent Trac
documentation. Good luck with your new project!


http://www.playingwithwire.com/2007/06/host-your-own-project-with-trac/

20091218

oracle在windows下安装时出现了错误:将Microsoft LoopBack Adapter配置为系统的主网络适配器。

oracle在windows下安装时出现了错误:将Microsoft LoopBack Adapter配置为系统的主网络适配器。


加一个Microsoft Loopback Adapter 的虚拟网卡,设置IP等信息就通过了,但安装完成后,WEB页就访问不了,也不只是咋回事。
幸好还可以用原来的本地连接上网,看来我把Oracle欺骗过去了


Microsoft LoopBack Adapter<==> Microsoft 环回适配器


要在 Windows XP 中手动安装 Microsoft 环回适配器,请按照下列步骤操作:


单击"开始",然后单击"控制面板"。
如果您在分类视图中,请单击左窗格中的"控制面板"下的"切换到经典视图"。
单击"添加硬件",然后单击"下一步"。
单击"是,我已经连接了此硬件",然后单击"下一步"。
在列表的底部,单击"添加新的硬件设备",然后单击"下一步"。
单击"安装我手动从列表选择的硬件",然后单击"下一步"。
单击"网络适配器",然后单击"下一步"。
在"制造商"框中,单击"Microsoft"。
在"网络适配器"框中,单击"Microsoft 环回适配器",然后单击"下一步"。
单击"完成"。
成功安装该适配器后,与其他任何适配器一样,您可以手动配置其选项。
如果 TCP/IP 属性配置为使用 DHCP,则该适配器最终将使用一个 autonet 地址
(169.254.x.x/16),因为该适配器并没有与任何物理媒体实际相连接。

注意:默认情况下,TCP/IP 属性配置为使用 DHCP。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/johnmcdavid/archive/2009/09/25/4433290.aspx

20090717

◇ 成为 linux 的主人(转寄)

成为Linux的主人(Linux Administrator)
CCCA 资工86 许景华
序言 :

在 Linux 系统管理中, /etc 下面的档案无疑的伴演了极端重要的角色, 在这篇文
章中, 将为各位介绍 /etc 下每个档案的用途, 及相关指令的运用, 希望能为大家在成
为 System Administrator 的路上有所助益.

特别要说明的是, 在这篇文章中, 有些档案仍然没有提到, 这是因为, 这些档案都
不是一二十行的描述就可以讲完的! 如 /rc.d 下的档案, inittab 这两个最好就要合
起来讲, 因为这些牵扯到 bootstrapping, 绝对不是三言两语就可以讲完的. 凡是有这
种特性的档案, 有机会的话将再为各位介绍......

本文 :

/etc/DIR_COLORS : 设定在用 ls 时 , 各种不同档案型态所用的颜色 , 但是不能用
pipe 再导向输出, 否则就没有各种颜色了. 由档案的内容可以很
轻易的了解, 如 default 的 directory 是蓝色, 具有可执行档权
限的是绿色, 而压缩档是红色...... 这些都可依个人喜好来加以
更改. 这个档案内容写得很清楚, 想必各位一眼也就可以看得出来
, 在颜色的组成方面, 我们常用 RGB 三原色的成份值来组合出各
种不同的颜色. 由 R,G,B 这个 bit 是 0 或是 1 三个值组合出颜
色这个道理虽浅显, 但假如我们更了解 R,G, B 的加权值分别是
1,2,4 的话, 那就再也不用翻书查颜色了...... 选择自己的调色
盘来取代预设的调色盘, 是经常会做的事.

/etc/HOSTNAME : 记录完整的 hostname 与 domain name , 这个档案在须要 hostname
的场合会用得到 , 如 /etc/rc.d/rc.M 等 ......

/etc/NETWORKING : YES , 没什麽用 , 改成 NO 会怎样呢 ? 会对网路的使用会造成影
响吗 ? 答案是否定的 .

/etc/X11 : link 到 /var/X11/lib/X11

/etc/at.deny : 在这个档案中可记载那些人不能使用at这个命令来做一些 later job
, 假如 at.deny 是空的, 而且 at.allow 也不存在的话. 那就是每个
人都可以用 at 这个命令.

/etc/at.allow : 相对於 at.deny , 这个档案记载那些人可以使用 at 这个指令关於
at 这个指令, 我想它的重要性是无庸置疑的, 各位应该要会这个指
令的用法, 会了这个指令, 可以在工作的执行上会更有弹性而关於
at.deny 与 at.allow, at 会先去找 at.allow, 假如存在而且有记
录一些人的话, 那就只有这些人能使用 at 这个指令, 但假如
at.allow 并不存在的话, 那 at 就会去找 at.deny, 没有在
at.deny 中的人都可以使用 at 这个指令了! 特别要说明的是, at
的执行时间也许会不如你所预期的, 比如说明明一个档你叫它在
7:02 执行, 但它却会等到 7:05 才执行, 这是因为 crond 是每五分
钟才去看看 at 的 queue 中是否有要执行的 job. 当然, 你也可以
改成每分钟都去 check, 但这样似乎没有很大的意义, 除非你对时间
的准确度要求的很严格, 否则应该没有必要去动才是. 各位可去看看
/var/spool/cron/crontabs/root 中就有详细记载, 每五分钟 run
一次 atrun, 所以 at 命令的执行可看成以每五分钟为一单位.

/var/spool/atjobs: 当你使用 at 来安排一件工作时, 系统会把
你目前的环境变数及所要用 at 执行的工作
抄一份到这个目录下的档案中 .

/var/spool/atspool : 使用 at 命令之後所得到的一些讯息 , 会被
记录在这里 . 可能是 at 所要执行的命令传
回的错误讯息 , 或传回执行成功的讯息 .

/etc/csh.cshrc :
/etc/csh.login :这两个档案应该很熟悉罗! 它就是各位常见到的.cshrc 及.login,
不过大家通常只注意到自己的 home directory 有这两个档, 但
/etc 下也有这两个档, 不过 /etc 下的这两个档绝大部份的情况下
都是由 system administrator 在 maintain, 一般 user 不应该有
权力去改. 顺便一提的是, 系统会先去找 /etc 下这两个档, 接著才
加上自己 home directory 中那两个档的设定. 当然, 有了.login
也有.logout,.logout 中可以写一些 message, 或执行一些命令, 当
你下达 logout 之後,.logout 就会被执行.

/etc/disktab : ( disk parameter table ) 假如你在刚开机有 pass 磁碟机的一些参数
给 kernel 的话 ( 通常是你的硬碟比较奇怪 , 需要 pass cylinders ,
heads , sectors 给 kernel 才抓的到 , 或者是 SCSI 的硬碟有时
有时也要用到一些参数 ) 那这些参数就会被记录在这里 .

/etc/exports : 这个档案记录著你要给别人 mount 的档案系统 , 这就是典型 NFS
系统所存在的档案 , TCP/IP for OS/2 若要使用 network file system
而要把自己的 filesystem 让别人 mount 的话 , 这个档案也是不可或缺
的. 比如说 , 我所在的 domain name 为 dorm10.nctu.edu.tw , 我要把
我 /usr 下的 filesystem 开放给十舍的人 mount , 那我在 exports 中
要这样写 :

/usr *.dorm10.nctu.edu.tw(ro)

接著 , 在 /etc/rc.d/rc.inet2 中 , 找到下面的段落 , 这些段落本来
每一列的前面都有 # , 现在 , 依照下面的内容 , 把这些 # 拿掉 .

......
......
......
# Start the SUN RPC Portmapper.
if [ -f ${NET}/rpc.portmap ]
then
echo -n " portmap"
${NET}/rpc.portmap
fi
......
......
......
# # Start the various SUN RPC servers.
if [ -f ${NET}/rpc.portmap ]
then
# if [ -f ${NET}/rpc.ugidd ]
# then
# echo -n " ugidd"
# ${NET}/rpc.ugidd -d
# fi
if [ -f ${NET}/rpc.mountd ]
then
echo -n " mountd"
${NET}/rpc.mountd
fi
if [ -f ${NET}/rpc.nfsd ]
then
echo -n " nfsd"
${NET}/rpc.nfsd
fi
# # Fire up the PC-NFS daemon(s).
if [ -f ${NET}/rpc.pcnfsd ]
then
echo -n " pcnfsd"
${NET}/rpc.pcnfsd ${LPSPOOL}
fi
# if [ -f ${NET}/rpc.bwnfsd ]
# then
# echo -n " bwnfsd"
# ${NET}/rpc.bwnfsd ${LPSPOOL}
# fi
fi

echo

# Done!

在 client 端可用如 :

mount -vt nfs rebel.dorm10.nctu.edu.tw:/usr /tmp

将 server 端的 /usr 挂到 client 端的 /tmp

/etc/fastboot : 这个档案是使用 shutdown -f 所产生的 , -f means "fastboot"
在重新 reboot 之後 , 系统会去检查这个档是否存在 , 以决定
是否要 run fsck .

/etc/fdprm : floopy disk parameter table . 一般情况下是不须要更动的 , 除非
你有很奇怪格式的软碟 , 或者你想做出不符合一般格式的磁碟片 .

/etc/nologin : 你也许会感到奇怪 , 我的 /etc 这个 directory 下并没有这个档啊?
没错 , 它平常是不存在的 , 通常会看到这个档的话 , 那表示系统大
概要 shutdown 了 , 因为系统要 shutdown 了 , 自然不希望有人又
login 进来 , 所以在我们执行 shutdown 时 , nologin 这个档会自动
的被 create , 里面放著 shutdown message . 实际上 , 当我们在
login 时 , 系统会去检查有没有这个档 , 假如有的话 , 那就会印出
这个档案中的 message , 然後不让你 login .
nologin 也有可能是为了某种理由被制造的 , 比如说系统在 maintain
暂时不希望有人 login . 无论如何 nologin 若存在 , 就不能 login

/etc/fstab : 记录开机要 mount 上来的 filesystem, 这个档案相当重要! 各位可以
在 /etc/rc.d/rc.S 中找到 /sbin/mount -avt nonfs 这一列, 当执行
到这一列时, mount 就依据 /etc/fstab 中的记载, 自动的将档案系统
mount 上来. 下面就是一个颇为典型的□例. 假如你一开机就想自动
mount 一些 filesystems, 而不要等到开机後再以手动 mount 的话那把
这些 filesystems 及相关资讯写在这个档, 是不错的选择. 在档案格式
方面, 每一列有六个栏位, 不过後面两个栏位常被省略所以我们只看到
有四个栏位. 其它的两栏分别为 dump-freq 及 pass-number.
dump-freq 预设值为 0; 而 pass-number 是 fsck 所会参考到的地方,
数字可为 0,1,2...... 1 表示这个 filesystem 将首先被 fsck check,
2 表其次, 馀类推......

# 要挂上的档案系统 挂在那里 档案系统格式 读写状况
/dev/hdb2 swap swap defaults
/dev/hdb1 / ext2 defaults
/dev/sbpcd /mnt/cdrom iso9660 ro
/dev/hda1 /mnt/dosc msdos rw
/dev/hda5 /mnt/dosd msdos rw
/dev/hda6 /mnt/dose msdos rw
none /proc proc defaults

/etc/ftpusers : 这个档案记录那些人不可以 ftp 签入系统 , 预设值有 root , uucp
news . 这些都是为了 security 方面的考量 , 你可以在这里加入
不可 ftp 进来的 user id .

/etc/gateways : 顾名思义 , 这个档案记录一些 gateways 的 information
这个档案的格式如下 :

<net | host> name1 gateway name2 metric value <passive | active | external>

当 routed 启动时, 它会去读 /etc/gateways 这个档. 若一个 gateway
并不做 routing information 交换的话, 那它就会被标示成 passive,
假如有做 routing information 的交换 ( 这台机器上有 run routed )
, 那就会标示成 active net 或 host 这个关键字是指出 route 是到网
路或是到一台特定的机器上. name1 就是目地网路或目地机器的名字.
这个名字可用 /etc/hosts 或是 /etc/networks 中的 symbolic host
name. name2 就是讯息将被送往之 gateway 的 name 或 IP address.

/etc/group : 如同 /etc/passwd 列出了系统中所有的使用者名字 , /etc/group
定义系统中所有的 group name 与相关讯息 .
格式为: group_name:passwd:GID:user_list

如 : author::200:jhhsu,emotion,passion

上面的例子表示 jhhsu,emotion,passion 都是属於 author 这个 group
通常 passwd 那栏是空白的 , 表示不用密码 . 或放一个 * 号
以下也是一个典型的例子 :

root::0:root,jhhsu
bin::1:root,bin,daemon
daemon::2:root,bin,daemon
sys::3:root,bin,adm
adm::4:root,adm,daemon
tty::5:
disk::6:root,adm
lp::7:lp
mem::8:
kmem::9:
wheel::10:root,jhhsu
shadow::11:root
mail::12:mail
news::13:news
uucp::14:uucp
man::15:man
users::100:games
bbs:*:99:bbs,bbsuser,bbsroot

当然啦 ! 这个档案只能为 superuser 所读写 , 一个 superuser 可使
用 groupadd , groupdel , groupmod 来管理 /etc/group 这个档 ,
要手动管理也是可以 . 下面是这三个指令的用法 :

groupadd [ -g gid [ -o ] ] group_name
groupdel group_name
groupmod [ -g gid [ -o ] ] [ -n new_group_name ] group_name

/etc/hosts : 这个档案记录著 IP address 至 hostname 的 mapping . 如我们想把
ccsun21.csie.nctu.edu.tw 取个别名叫 : oldstock
那我们就可以在这个档案里写如下的叙述 :

140.113.17.151 oldstock

如此一来 , 以後我们 telnet oldstock 就等於 telnet
140.113.17.151

在这个档案中 , 至少会有两列 , 一列为 loopback , 这是为了侦错目的
而设的 , 另一列为 local host , 就是你自己的机器 .

/etc/hosts.equiv : 在里面可以设定一些 remote machine , 而从这些 remote
machine 利用 rsh 或 rlogin 连回 local machine 的话
便不用输入密码
同样的东西也可以在 .rhosts 见到 . 比如说在 ccsun22.csie
.nctu.edu.tw 上你的 Home Directory 建一个 .rhosts 档 ,
里面的内容如下:

rebel.dorm10.nctu.edu.tw jhhsu

以後只要在 rebel.dorm10.nctu.edu.tw 上利用 rsh 或 rlogin 至
ccsun22.csie.nctu.edu.tw 的话 , 也不须要输入密码
rsh -l jhhsu ccsun22.csie.nctu.edu.tw <-- 不须密码
不过这东西最好不要乱设 , 以信得过的 hosts 为主 ,
才不会造成 security 上的困扰 .

/etc/hosts.deny : 设定那些 remote hosts 不可以使用 inetd
/etc/hosts.allow : 设定那些 remote hosts 可以使用 inetd
至於 inetd 有那些 service , 各位可以去看看 /etc/inetd.conf
从里面就可以很清楚的可以看得出来 .
假如你采取比较 close 作法的话 , 可以在 hosts.deny 中写

ALL:ALL

但是如此一来 , 只要不是从 localhost 来的 , 那大概都不必
玩了 , 所以 , 我们也要适时的在 hosts.allow 中加入可以
access 的 hosts , 如 :

ALL:140.113.17. <-- 只要从 140.113.17.X 都可以 access
ALL:140.113.4.
ALL:140.113.6.

/etc/hosts.lpd : 记录可 access printer 的 hosts . 假如一台 printer可被网路上
的许多台主机所共用, 那 printcap 这个档案必须得描述完整的网
路设定资讯. 在比较严密控制的情况下, 有两个条件必须符合首先,
local machine 必须在 remote machine 的 /etc/hosts.eqiuv 中,
或者, 在 remote machine 的 /etc/hosts.lpd 中被记载第二, 使
用者必须有 remote machine 的帐号.

/etc/inetd.pid : inetd process id

/etc/issue : 这个档案被记录著 login prompt 前所要 echo 的 message , 特别要注
意的是 /etc/rc.d/rc.S
下面的四行若没有 mark , 则每次开机 issue 及 motd 都会被改变
假如你要有自己的设定 , 下面一定都要 mark 起来如下 :
#echo > /etc/issue
#echo Welcome to Linux /bin/uname -a | /bin/cut -d\ -f3. >> /etc/issue
#echo >> /etc/issue
#echo "/bin/uname -a | /bin/cut -d\ -f1,3. (Posix)." > /etc/motd

/etc/klogd.pid : klogd process id

/etc/ld.so.conf : 记录一些 library 所在的目录

/etc/magic : 初看这个档案时 , 也许只能隐隐约约的看到 : 这个档案似乎是描述一些
档案的格式. 没错, 这个档案记载了许多档案格式的识别字串或方法那
这个档在那里会用的到呢? 各位有使用过 file 这个指令吗? file 这个
指令的命令格式为:

file [ -c ] [ -z ] [ -L ] [ -f namefile ] [ -m magicfile ] file

file 这个指令非常有趣, 它可以告诉你某一个档案的格式, 如它是一个
text 档, 或是一个 shell script 或 DOS 可执行档等...... 而 file
这个指令, 就是去参考 /etc/magic 这个资料库. 假如你知道某一档案
的识别字, 而原来 magic 这个资料库没有记录, 那你可以把识别方法加
到资料库中, 或乾脆建立自己的资料库算了.


/etc/motd : 这个档很简单 , 就是 message of the day 的意思 , 里面可以写一些
message , 而这些 message 会在 login shell 之前被显示出来 .
通常都是 system administrator 要告知 user 的一些讯息. 而关於每次
都会改变的问题, 在前面 /etc/issue时已提出过,记得 mark 起来就好.

/etc/mtab : 目前 mount 上来的 filesystems , 各位可使用 mount 这个指令来看看
这个档的变化 . 如我在 mount 3.5 inch 软碟A 前 , 我看到的 mtab
如下 :

/dev/hdb1 / ext2 rw 0 0
/dev/hda1 /mnt/dosc msdos rw 0 0
/dev/hda5 /mnt/dosd msdos rw 0 0
/dev/hda6 /mnt/dose msdos rw 0 0
none /proc proc rw 0 0

然而 , 当我以 mount -t msdos /dev/fd0H1440 /mnt/dosa 这个指令将
3.5 inch 软碟A 挂上来後 , mtab 变为像下面的情况 :

/dev/hdb1 / ext2 rw 0 0
/dev/hda1 /mnt/dosc msdos rw 0 0
/dev/hda5 /mnt/dosd msdos rw 0 0
/dev/hda6 /mnt/dose msdos rw 0 0
none /proc proc rw 0 0
/dev/fd0H1440 /mnt/dosa msdos rw 0 0 <-- 多了这列 !

所以 , 各位了解啦 ! /etc/mtab 就是记录目前 mount 的 filesystem


/etc/mtools : 这里面记录的 , 是给 /usr/bin/mtools 参考用的 parameter .
那 mtools 是什麽呢? 简而言之 , 就是一群操作 MSDOS 档案的命令
集合 , 可用的命令可多了 , 如下 :

mattrib - change MSDOS file attribute flags
mcd - change MSDOS directory
mcopy - copy MSDOS files to/from Unix
mdel - delete an MSDOS file
mdir - display an MSDOS directory
mformat - add an MSDOS filesystem to a low-level formatted diskette
mlabel - make an MSDOS volume label
mmd - make an MSDOS subdirectory
mrd - remove an MSDOS subdirectory
mread - low level read (copy) an MSDOS file to Unix
mren - rename an existing MSDOS file
mtype - display contents of an MSDOS file
mwrite - low level write (copy) a Unix file to MSDOS

这些都被 link 到 mtools

/etc/named.boot : 假如你要建立 name server 的话 , 那这个档就是你要修改的地方
详细的情况可以去看 named 的 manual 就知道了 .

/etc/named.pid : named process id

/etc/networks : 这个档与 hosts 有点像 , 都是在 boot 的时候会用到的东西 ,
在这里可以定义一个子网路与其 IP address 的资讯 .

/etc/nntpserver : 这个档案记录著 news server , 当我们使用 tin -r ( read news
remotely ) 时, 这个档案会被参考到, 或者若这个档案不存在的
话, 那环境变数 NNTPSERVER 所指定的 server 就会被当成要去取
得 news 的 server. 譬如说, 你要将 140.111.1.11 当成 news
server, 那 /etc/nntpserver 中就可写:

140.111.1.11

/etc/nologin : 你也许会感到奇怪 , 我的 /etc 这个 directory 下并没有这个档啊?
没错, 它平常是不存在的, 要是你看到这个档的话, 那表示系统大概
要 shutdown 了, 因为系统要 shutdown 了, 自然不希望有人又
login 进来, 所以在我们执行 shutdown 时, nologin 这个档会自动
的被 create, 里面放著 shutdown message. 实际上, 当我们在
login 时, 系统会去检查有没有这个档, 假如有的话, 那就会印出这
个档案中的 message, 然後不让你 login. nologin 也有可能是为了
某种理由被制造的, 比如说系统在 maintain 暂时不希望有人 login.
无论如何 nologin 若存在, 就不能 login.

/etc/passwd : 呵呵 , 这个档案可重要了 . 一个 system administrator 最初要学的
常常就是这个档案的内容! 这个档案记录著系统可以认得的 user, 当
然啦, 也包括一些非人的 login name, 但这是有特殊用途的. 档案内
容每列的格式如下:

Login Name:Encrypted Password:UID:GID:GCOS:Home Directory:Login Shell

(1)login name: 在 login name 方面 , 每个 login name 必须是唯一的 ,
而且不能超过 8 个字元, 一般说来, login name 虽然可以
大小写混合, 但平常的情况下都用小写. 顺便一提的是
/usr/lib/aliases 这个档案, 修改这个档案的内容可以使
mail 时用超过 8 个字元的档案. 如平常 mail
jhhsu@dorm10 .nctu.edu.tw 可变成 mail
Jin-Hwa-Sheu@dorm10.nctu.edu. tw.

(2)Encrypted Password: 这栏是编码过的密码 , 在新增一个 new user 时
应该在这栏填上 * , 更改 password 可用 passwd
这个指令 . passwd 这是一个 setuid 的指令 ,
关於 setuid , setgid , stickybit , 各位可去
参考 Unix Security 方面的书籍 .

(3)UID: user id , 每个"人"都要不一样 , 习惯上 , 100 以前被保留做为
特殊的 user id , 而 root 总是 0.

(4)GID: group id , 在比较早期的系统 , 一个 user 同时只能在一个 group
不过现在就没有这种限制了 .

(5)GCOS: 这栏基本上没有特殊格式限制 , 在这里可以写上你的 full name
家中电话, 住址等...... 爱写什麽就写什麽. 虽然你可以乱写一
通, 但若你用到逗号, 系统会把它当成区间. 如你写: Jin-Hwa
Sheu, NCTU, 80317, (02)1234123 这些都会被当成 Full Name,
Office, Home Phone. finger 这个指令会去读这地方的资讯. 用
chfn 可改变这栏.

(6)Home Directory: 这栏没什麽 , 就是记录 user 的 home directory 而已
利用 cd 或 cd~ 都可回到 home directory .

(7)Login Shell: 设定使用者所使用的 shell , 预设值是 /bin/bash . 当
然 , 你也可以自己改成 csh 或 tcsh , zsh 也是不错 ,
假如有 free 版的 korn Shell 也可拿来用用 . 用 chsh
可改变你的 login shell .

为了 security 方面的考量 , 各位可以安装 shadow , 将 passwd 这个档
完全的与一般 user 隔绝开来 , 连读也没有办法读 passwd 这个档 .

/etc/profile : profile 这个档是 bash 这个 shell 所用的 , profile 之於 bash
就好像 cshrc 之於 csh. 同样的, /etc 下的 profile 也是 SA 在维
护的, 主要是放著全域的设定 ( Global Setting ) 而每个 user 在
自己的 Home Directory 都可以有各人的 .profile

/etc/protocols : 假如你为了某些目地 , 而写了自己的 protocol , 那你就必须把
它列在这个档案中 , 这样 inetd 才会管理一些 daemons 去用它
这个档案每列的格式如下 , 当然 , # 之後的是注解 :

Protolcol Name Portocol Number Aliases

ip 0 IP # internet protocol, pseudo protocol number
icmp 1 ICMP # internet control message protocol
igmp 2 IGMP # internet group multicast protocol
ggp 3 GGP # gateway-gateway protocol
tcp 6 TCP # transmission control protocol
pup 12 PUP # PARC universal packet protocol
udp 17 UDP # user datagram protocol
idp 22 IDP # WhatsThis?
raw 255 RAW # RAW IP interface

/etc/psdatabase : 这个档案是被 psupdate 所用到 , psupdate 主要的功能就是更新
/etc/psdatabase 这个档 , 以符合目前的 kernel image system
map file . 一般的预设档是 /usr/src/linux/tools/zSystem .

/etc/resolv.conf : 这里面记载著你机器的 domain name 及 name server 的 IP
address , name server 可以自己加入 , 在交大可以用
140.113.1.1 或 140.113.17.5 , 这两台都是 name server .
name server 可不要随便就去掉 , 否则 , 就会发生如下的情况 :

<jhhsu>@Rebel[/etc]>telnet ccsun3.cc.nctu.edu.tw
ccsun3.cc.nctu.edu.tw: Host name lookup failure

因为没有了 name server , 所以 ccsun3.cc.nctu.edu.tw
没有办法被解译 , 故会有 Host name lookup failure
这段讯息 .

/etc/rpc :


/etc/securetty : 假如你要以 root login 的话 , 实际上是有限制的 , 这个档案就
列出了 root 可以 login 的 ttys , 假如你把每一列都 mark 起来
那就没有任何一个 terminal 可供 root login 了 , 所以呢 ,
就会出现以下的情况 :

Rebel login: root
root login refused on this terminal.

再一次的 , 我们看到了 unix security 方面的东西 , 仔细的在这
个档案列出可用的 terminal , 可使从 modem 连的使用者或经由网
路来的其它的使用者比较不容易取得 superuser 的权力 .

/etc/services :

/etc/shell : 这个档案再简单也不过了 , 里面就记录著可以用的 login shell . 还
有一点 , 那就是这个档的记录会在使用 chsh 时做为参考 , 唯有被列
在这里的 shell , 使用 chsh 时才会出现这些 shell 可选 .


/etc/sudoers : 在一个不是很复杂的系统中 , 也许一个 System Administrator 就可
以管理所有的事情 . 但一但系统提供的服务变多 , 系统管理的工作
也随之变得沉重 , 这时候 , sudo 就派上用场了 , sudo 可以让一个
普通的 user 变成 superuser , 这样一来 , 一个系统的某些工作 ,
就可以分配给这些 superusers 来做 . 以减轻负担 .
当然 , 并不是每个 user 都可以变成 superuser . 所以
/etc/sudoers 就是记录著那些人可以用 superuser 的身份来执行一
些工作 , 这个档案的格式如下 :

host alias section format:
Host_Alias HOSTALIAS = host-list

Host_Alias ::= a keyword.
HOSTALIAS ::= an upper-case alias name.
host-list ::= a comma separated list of hosts.

command alias section format:
Cmnd_Alias CMNDALIAS = cmnd-list

Cmnd_Alias ::= a keyword.
CMNDALIAS ::= an upper-case alias name.
cmnd-list ::= a comma separated list commands.

user specification format:
user access_group [: access_group] ...

access_group ::= host_type = [op]cmnd_type [,[op]cmnd_type] ...
host_type ::= a lower-case host name OR a host alias.
cmnd_type ::= an command OR a command alias.
op ::= the logical '!' NOT operator.

如下面是一个极为简单的例子 :

# Cmnd alias specification
Cmnd_Alias SHUTDOWN=/sbin/halt,/sbin/shutdown
# User specification
root ALL=ALL
jhhsu ALL=ALL
emotion ALL=ALL,!SHUTDOWN

在这个例子当中 , jhhsu 可以执行 root 所有的命令 , emotion 除了 halt
及 shutdown 不能执行以外 , 其它 root 能执行的命令他都能执行 .
这个档案有一个专属的 editor 叫 visudo 可来编辑 /etc/sudoer 这个档案
在编辑完这个档案之後 , jhhsu 就可以用 sudo.bin 这个命令转换成
superuser 来做原来只有 superuser 才能做的事 . 而关於使用 sudo.bin
有一个记录档 , 在 /var/adm/sudo.log , 里面记录著谁在什麽时候利用
sudo 下了那些指令 . 顺便一提的是 : 利用下面这个命令可抓出那些人转换
成 superuser 但却没有成功 ......

grep "FAILED SU" /var/adm/messages

/etc/utmp : 从 utmp 中可以知道现在有谁正在用系统 . 当使用者 logout 之後 ,
init ( 很重要的 process , 以後有时间将为各位介绍 ) 就去把 logout
的使用者从 /etc/utmp 中去掉 .
实际上 , utmp 每个 record 是像这样的一个结构 :

struct utmp {
short ut_type; /* type of login */
pid_t ut_pid; /* pid of process */
char ut_line[12]; /* devicename of tty -"/dev/" */
char ut_id[2]; /* init id or abbrev. ttyname */
time_t ut_time; /* logintime */
char ut_user[8]; /* username, not null-term */
char ut_host[16]; /* hostname for remote login */
long ut_addr; /* IP addr of remote host */
};

了解这个档案 , 进而去修改 , 可以做出许多有趣的事 ......:P

/etc/wtmp : 简而言之 , wtmp 是一个 login data base , 有许多的指令会用到这个
档 , 像 last , sessreg , who 等 ......
这个档案格式与 utmp 大致相同 , 只是多了 shutdown 及 reboot 这
个记录 , 指出系统的 shutdown 或 reboot , 及一个空的 user name
指出在相关 terminal 的 logout . 还有 , 不像 /etc/utmp , /
/etc/wtmp 的记录是慢慢的附加上去的 , 也就是说 , 这个档案会越变
越大 , 虽然变大的速度并不会太快 , 但一但到了某种程度大小的话
System Administrator 应该要考虑砍掉部份 . 下面是用 last 这个
指令所得到的部份内容 :

bbs tty1 Tue Nov 29 19:09 - 19:09 (00:00)
root tty1 Tue Nov 29 19:08 - 19:09 (00:00)
root tty1 Tue Nov 29 19:07 - 19:07 (00:00)
runlevel ~ Tue Nov 29 19:07
reboot ~ Tue Nov 29 19:07

/etc/zprofile : link 到 /etc/profile

20090716

A System Administrator's View of(转寄)

发信人: precilla (晴朗的IWannaBe!), 信区: Unix

From http://people.netscape.com/bjm/whyLDAP.html

A System Administrator's View of LDAP
By Bruce Markey
----------------------------------------------------------------------------
----
The benefits of LDAP (Lightweight Directory Access Protocol) to developers a
nd end users have been widely touted, but in fact it's system administrators
who may reap the greatest benefits from LDAP deployment. Here in Netscape's
IS group, we've recently been using LDAP directory services on our internal
network -- which shouldn't be surprising, since Netscape's server products
and Netscape Communicator use LDAP for sharing information. In this article
I'll give a system administrator's perspective on what you'll need in order
to reap the benefits of using LDAP: a good understanding of what LDAP can an
d cannot accomplish, some familiarity with LDAP basics, and ideas on how to
make the transition to LDAP.
Why LDAP?
At a monthly meeting of Unix system administrators in Silicon Valley, LDAP w
as correctly described as a system for distributing information such as list
s of users. A concerned system administrator was quick to ask, "With all the
user lists that I manage already, why do I want to have another one?" A fai
r question, I thought, and one that hints at why system administrators shoul
d pay close attention to LDAP's evolution.
LDAP has the potential to replace existing application-specific lists and co
nsolidate information. This means that changes made on an LDAP server will t
ake effect for every directory-enabled application that uses this informatio
n. Imagine adding a variety of information about a new user through a single
interface only once, and immediately the user has a Unix account, an NT acc
ount, a mail address and aliases, membership in departmental mailing lists,
access to a restricted Web server, and inclusion in job-specific restricted
newsgroups. The user is also instantly included in the company's phone list,
mail address book, and meeting calendar system. When a user leaves, access
can be disabled for all of these services with just a single operation.
That sounds wonderful, but at the same time far-fetched. How can we expect o
perating systems and applications from different vendors to agree on one sys
tem for looking up information, and why is LDAP perceived as the key to maki
ng this possible?
First, let's look at what LDAP is and isn't. Initially developed at the Univ
ersity of Michigan, LDAP is now an Internet standard for directory services
that run over TCP/IP. One or more LDAP servers contain the data that make up
the LDAP directory tree. An LDAP client connects to an LDAP server and subm
its a query to request information or submits information to be updated. If
access rights for the client are granted, the server responds with an answer
or possibly with a referral to another LDAP server where the client can hav
e the query serviced.
An LDAP server is not simply a form of database, but a specialized server fo
r directories. A directory can be distinguished from a general-purpose datab
ase by the usage pattern. A directory contains information that is often sea
rched but rarely modified. Host names or user names, for example, are assign
ed once and then looked up thousands of times. LDAP servers are tuned for th
is type of usage, whereas relational databases are much more geared toward m
aintaining data that's constantly changing.
Another difference is that relational databases store information in rows of
tables, whereas LDAP uses object-oriented hierarchies of entries. The Domai
n Name Service (DNS) for resolving host names to IP addresses also uses a hi
erarchy. Because an LDAP directory could hold host name information, it migh
t seem that LDAP could be a replacement for DNS. However, DNS is very specia
lized, and LDAP was not designed to address the same set of problems that DN
S has been groomed for. Still, DNS maintenance can benefit from an LDAP-base
d strategy.
Being designed for this usage pattern, current directory servers with a mill
ion or more entries can respond to hundreds of search requests per second fr
om a single server. Replication is also possible, which makes LDAP very scal
able.
Reducing load on the authoritative server is not the only reason for using r
eplica servers. Many Unix networks use Network Information Service (NIS), al
so known as YP, which uses slave servers on each subnet. As with YP, putting
replicas on subnets can avoid network traffic through routers and reduce la
tency. However, unlike YP, the LDAP synchronization scheme features incremen
tal updates that can be pushed immediately to the replicas rather than perio
dically transferring all of the data. For Netscape's internal mail hubs, we
use replicas on the local host. While this really isn't necessary, it does a
llow the Netscape Messaging Server to do lookups locally with very little ov
erhead to maintain synchronized data.
LDAP Basics
Before going any further with architecture issues, let's look at how LDAP or
ganizes information. LDAP introduces a lot of new terminology, but the only
terms you need to understand to get started are entry, object class, attribu
te, and distinguished name.
An LDAP server contains entries, and each entry's type is defined by an obje
ct class. An object class defines attributes, both required and optional, as
sociated with an entry of that class. Each entry is uniquely identified by a
distinguished name, or DN. The DNs are organized in a hierarchy; each one c
onsists of the name of an entry plus a path of names tracing the entry back
to the root of the tree.
For example, given that I work at a company in the United States, the top en
try in the hierarchy for my entry has the DN "c=US". This top entry is of th
e object class country. A country entry has one required attribute, c, with
the value ofUS in this case. At the next level down in the hierarchy, an ent
ry of the object class organization has the DN "o=Netscape Communications Co
rp., c=US". The organization entry requires the attribute o, which is Netsca
pe Communications Corp. in this case. Notice that the parts of the DN are se
parated by a comma.
What we have at this point would likely be the base DN for this server. A ba
se DN defines the top of the namespace that the server is responsible for, m
uch like a DNS zone. As we add entries further down the hierarchy, the DNs b
ecome longer. Remembering a long DN is not an issue for end users, because c
lient applications will be doing the searches and then displaying the attrib
utes the user needs to see. For most applications, the full DN does not need
to be exposed to the end user.
Now that the groundwork has been laid, let's look at an entry for an individ
ual. The entry's DN is
"cn=Bruce Markey, o=Netscape Communications Corp., c=US"
This entry is of the object class inetOrgPerson, which requires a cn attribu
te (for "common name"). It also requires an sn attribute for the surname; th
is is an example of a required attribute that is not the attribute included
in the DN. The inetOrgPerson class also defines about 50 other attributes th
at can be associated with a person, such as uid, title, manager, telephoneNu
mber, pager, mail (e-mail address), and other information you would likely w
ant to associate with a person in an organization that has access to the Int
ernet. It even has the attributes jpegPhoto and audio, which are possible be
cause attribute values can be declared to be encoded into different syntaxes
, including binary data.
In order for authoritative information to be maintained, access control need
s to be imposed for privileges to read, write, search, or compare. Access co
ntrol can be done on a subtree, entry, or attribute type and granted to indi
viduals, groups, or "self" (which allows an authenticated user to access his
or her own entry). This scheme provides a great deal of flexibility. For ex
ample, you may want to only allow people in a human resources group to chang
e the title or manager attributes, allow administrative assistants to change
office location and pager number information for just their department, and
allow individuals to modify their own home phone number, license plate, and
so on.
More Than a Protocol
Understanding how to maintain the data is not enough to be able to put LDAP
to use. There are four well-defined pieces of the overall system that simpli
fy implementing LDAP: the LDAP open standard, the API, the LDIF text format
for data, and the object class definitions.
LDAP -- RFCs 1777 and 1778 define the protocol that allows clients from diff
erent developers on any platform to talk to any type of LDAP server. Many ve
ndors have announced support for LDAP. Notably, Netscape, Microsoft, and Nov
ell already offer directory-enabled servers and clients. The University of M
ichigan, where LDAP evolved, has source code for their original slapd server
and other tools available for download.
API -- One of the important factors in the success of LDAP is that developer
s should be able to make use of information from an LDAP server without havi
ng to write and debug a lot of code. A well-defined application programming
interface (API) helps make this possible. You may be familiar with SOCKS and
"socksifying" an application; with LDAP, directory enabling is a very simil
ar process. For a program to make use of directory information, you simply i
nclude the API libraries in the source directory, modify the program's code
to call the API functions at the point where the information needs to be loo
ked up, and recompile. The most recent version of sendmail already includes
the API and has options to look up information through LDAP.
LDIF -- Another piece of the puzzle that I've found to be remarkably importa
nt is the LDIF file format. This ASCII text format is used for exporting and
importing data to and from LDAP servers. This not only makes it easy to mig
rate data from one server to another but also allows you to write scripts to
create LDIF files from other data sources. You can then verify and manipula
te the LDIF file before committing the data to the server. Because command-l
ine tools like ldapsearch return data in LDIF format, you can save some or a
ll of your data to a file, make global changes, and then import the new data
back into the server.
Object classes -- One other piece that's important to portability is object
class definitions. If a client needs some attributes that aren't in the well
-known object class definitions, a new object class can be created as an ext
ension of a similar object class. The client could then work with any LDAP s
erver, as long as the server has been given this new object class definition
.
Making the Transition to LDAP
Even with a solid understanding of LDAP concepts, switching to LDAP won't ha
ppen instantly. There are two distinct ways to put information to use: one i
s to use applications that are already directory-enabled, and the other is t
o gateway the information from LDAP into a format used by existing applicati
ons.
Here at Netscape, we first used a corporate-wide phone book that was availab
le through a web page. This was the beginning of creating an authoritative l
ist of employees on our central Directory Server. Netscape Communicator now
includes a window that can do native LDAP searches. Not only can that act as
the corporate phone book, but results can be selected to automatically addr
ess e-mail messages. Once the data is being stored and updated on an LDAP se
rver, other applications can take advantage of this resource.
Many LDAP-based software distributions include command-line tools for search
ing and modifying directory information. This makes it possible to use or ma
nipulate information with simple shell scripts. There are also tools availab
le for programming in Perl, Java, C, and so on.
Let's look at Unix login information as an example of some possible transiti
on strategies. Once attributes for users are stored in a directory server, a
useful thing that can be done is to sync up user names and passwords for mu
ltiple environments. We've implemented a system in-house where Unix and NT a
ccount passwords are updated when passwords are changed through our Director
y Server interface. This not only simplifies the change for users but can re
duce the chance of having infrequently used accounts with forgotten password
s.
One of the more interesting gateways to date is ypldap by Luke Howard of Xed
oc. This is an NIS (YP) server that uses LDAP instead of files to look up it
s information. It supports passwd and group maps along with the other common
ly used YP maps. This approach allows using existing YP-based applications w
ithout needing to run a script for converting the data.
Eventually, even tools like ypldap may become unnecessary when operating sys
tems include directory-enabled /bin/login. Most Unix versions have a configu
ration file that tells the OS where to look for password information. It sho
uld be relatively easy for vendors to add LDAP as one of the choices along w
ith NIS and the local /etc/passwd file.
I hope that at this point you're thinking about installing and testing a Dir
ectory Server on your network. As you start down the path to consolidating i
nformation with LDAP, be aware that LDAP is not an all-or-nothing propositio
n. You should start with one application as a pilot project. Once you have a
uthoritative information in your Directory Server for one application, you'l
l soon see the value of phasing in other applications.
----------------------------------------------------------------------------
----
LDAP Resources
LDAP: Programming Directory-Enabled Applications with Lightweight Directory
Access Protocol by Tim Howes and Mark Smith (Macmillan Technical Publishing,
1997) is an outstanding book focusing on the API.
RFC 1777, Lightweight Directory Access Protocol, and RFC 1778, The String Re
presentation of Standard Attribute Syntaxes, contain specific information ab
out the protocol.
The University of Michigan site is a good starting point on the web for info
rmation about LDAP.
PADL Software provides information about Luke Howard's ypldap.
Netscape's web site has several articles about LDAP and information about pr
oducts that incorporate LDAP, including:
The introductory View Source article Directories and LDAP: Universal Access
to Directory Information
The white paper An Internet Approach to Directories
A starting point for information about Netscape Directory Server, which feat
ures web-based administration and documentation
The ability to download and test drive the Directory Server
----------------------------------------------------------------------------
----
Bruce Markey is a Senior System Administrator in the Server Operations group
at Netscape. He began using UNIX 7th Edition in the early 1980s and has sin
ce supported more than 20 Unix variants. In 1990 he came to Silicon Valley,
where he worked as a system administrator for a series of successful startup
software companies. Since June 1995 Bruce has worked in a variety of server
product support roles for Netscape.
(7:98)
----------------------------------------------------------------------------
----
DevEdge Online FAQ
Developer Response Center
Join DevEdge Program Copyright ? 1998 Netscape Communications Corporation.

This site powered by: Netscape Enterprise Server and Netscape Publishing Sys
tem 1.6.

20090713

[软件]跨平台C++类库:fox (转寄)

发信人: yuhui (眼睛猫―工作猫―论文猫―发呆猫), 信区: Linux
标 题: [软件]跨平台C++类库:fox
发信站: BBS 水木清华站 (Thu Jun 22 22:38:15 2000)

闲逛发现的,在http://FOXGUI.sourceforge.net/

down下来看了看,因为有为VC做的workspace,很方便就试了一下,感觉不错,那个Text
Editor的例子比windows的notepad强多了(嘿嘿废话)

粗粗看了看这个还支持OpenGL和Mesa

跨平台的,感觉应该不错。有工夫要好好研究一下!

如果有人感兴趣,给个地址我上载一下。
^o^

The FOX GUI Library
===================
What Is FOX?
============
FOX is a C++ based Toolkit for developing Graphical User Interfaces easily a
nd
effectively. It offers a wide, and growing, collection of Controls, and pr
ovides
state of the art facilities such as drag and drop, selection, as well as Ope
nGL widgets
for 3D graphical manipulation. FOX also implements icons, images, and user-
convenience
features such as status line help, and tooltips. Tooltips may even be used
for 3D
objects!
Considerable importance has been placed on making FOX one of the fastest too
lkits
around, and to minimize memory use:- FOX uses a number of techniques to spee
d up drawing
and spatial layout of the GUI. Memory is conserved by allowing programmers
to create
and destroy GUI elements on the fly.
Even though FOX offers a large collection of Controls already, FOX leverages
C++ to
allow programmers to easily build additional Controls and GUI elements, simp
ly by taking
existing controls, and creating a derived class which simply adds or redefin
es the
desired behavior.
One of the prime design goals of FOX is the ease of programming; thus, most
controls
can be created using a single line of C++ code; most parameters have sensibl
e default
values, so that they may be omitted, and layout managers ensure that designe
rs of GUI's
do not have to worry about precise alignments.
Another nice feature of FOX which significantly reduces the number of lines
of code
which have to be written is FOX's ability to have widgets connect to each ot
her, and
passing certain commands between them; for example, a menu entry Hide Toolba
r can be
directly connected to the Toolbar, and cause it to hide.
Finally, FOX makes it easy to maintain the state of the GUI in an applicatio
n by having
the GUI elements automatically updating themselves by interrogating the appl
ication's
state. This feature eliminates the large amount of effort that may go into
sensitizing,
graying out, checking/unchecking etc. depending on the application state.
Where to get it?
================
You can FTP the complete FOX GUI toolkit from our FTP site:
ftp://ftp.cfdrc.com/pub/fox.tar.gz.
The tar-ball includes this on-line (HTML) documentation you see here.
Mailing List.
=============
To stay informed, we now have a mailing list for FOX. To subscribe, send ma
il to
fox-users-request@cfdrc.com with the word subscribe in the message body. Me
ssages to
the list can be sent to foxgui-users@lists.sourceforge.net.
FOX announcements will be made through the foxgui-announce@lists.sourceforge
.net list;
you can subscribe to this list by sending a message "subscribe" to the list
server at
foxgui-announce-request@lists.sourceforge.net.
Installation.
=============
Please refer to the file INSTALL. FOX should compile on a large number of U
NIX
systems. The current "configure" scripts are however still somewhat primiti
ve;
if you need to make specific alterations to the configure system, feel free
to
send them to me so that I may merge them into the main distribution.
The current FOX distribution compiles on Windows NT using the eXceed system;
we
expect however to get some core functionality to become available under the
native
Windows environment fairly soon.
Getting started.
=================
To get started programming with FOX, take a look at the tests directory, whi
ch contains
some example programs. The best one to look at is probably the "glviewer" a
pplication.
Note that you need OpenGL or Mesa on your machine in order to compile it.
Bugs.
=====
Please file bugs and questions to the list foxgui-users@lists.sourceforge.ne
t. Before
filing a bug, please take a moment to download the latest version of FOX, an
d make sure the
problem still persists:- FOX is being developed fairly rapidly, and it is po
ssible that
some of the problems you may have experienced have already been fixed.
License.
========
The FOX Library proper is licensed under GNU Library GPL; all the examples a
nd demo programs
are licensed under GPL.
List of Contributors.
=====================
List of contributors in alphabetical order.
Johnathan Bush. The Progress Bar Widget.
Freddy Golos. Freddy [Fyodor] was one of the first adopters of FOX, back
when everybody still had to take my word for it how nice it was;
Freddy made it clear there was real potential here.
Lyle Johnson. The Spinner Widget. Lyle is also largely responsible for the
Window NT port of FOX.

Guoqing Tian. The Dial Widget.
Charles W. Warren. The Shutter Widget. The concept of the Data Targets. Ch
arles
has also been my principal sounding board for exploring the
philosophical underpinnings of the FOX Library.
The true list should probably be much larger; if you feel I have inadvertant
ly omitted
you, please email me jvz@cfdrc.com and I'll amend the errors in my ways.
Yours,
Jeroen van der Zijp
jvz@cfdrc.com


--
忘掉她,忘掉她就可以不必再忍受,忘掉她就不再有痛苦。忘掉她,忘掉你
没有的东西,忘掉别人有的东西,忘掉你失去和以后不能得到的东西,忘掉
仇恨,忘掉屈辱,忘掉爱情,象犀牛忘掉草原,象水鸟忘掉湖泊,象地狱里
的人忘掉天堂,象截肢的人忘掉自己曾快步如飞,象落叶忘掉风,象公犀牛
忘掉母犀牛。忘掉是一般人能做的唯一的事。但是我决定不忘掉她。

※ 修改:・yuhui 於 Jun 22 22:39:02 修改本文・[FROM: 166.111.65.52]
※ 来源:・BBS 水木清华站 smth.org・[FROM: 166.111.65.52]

20090712

第十一章 任务的自动化 (转寄)

发信人: ruster (尘埃*星辰*领悟), 信区: Linux
标 题: 第十一章 任务的自动化
发信站: BBS 水木清华站 (Thu Dec 21 13:55:18 2000)

第11章 任务的自动化

本章要点:

本章介绍用来替代shell脚本的工具,如TCL和perl。

本章具体包括以下内容。

TCL/expect的使用

awk语言的基本知识

perl语言的基本知识

11.1 TCL和expect

TCL是一种类似shell脚本的语言,你可以使用它来完成许多操作。不过,我介绍它的
主要原因是expect是从它发展出来的。如果你想要写一个能够自动处理输入输出的脚本
(如向用户提问并且验证密码)又不想面对C或者Perl,那么expect是你的唯一选择。

11.1.1 TCL语言

要使用TCL,你必须先安装这个程序:

% rpm -q tcl

tcl-8.0.5-30

TCL语言可以用交互式或者脚本的方式执行,要使用交互式的TCL环境,只要输入

$ tclsh

%

出现的"%"符号是TCL的提示符,然后就可以使用TCL命令的。

如果你要使用脚本方式的TCL,首先把你的脚本写成一个文本文件,例如test.tcl,然
后执行

$ tclsh test.tcl

在tcl脚本中,每一行或者是一个命令行,或者是一个注释。注释行必须以#符号开头
,而命令行最好以分号结束,虽然不一定要这样做,但是这样做可以免去不少麻烦。

变量

在tcl中,有两种基本类型的变量,即标量和数组。标量就是一般的数字或者字符串变
量,可以用set语句定义同时赋值:

% set i 1

1

字符串应该用引号括起来:

% set str "test"

'test'

要输出一个标量的内容,使用put语句:

% puts $str

test

$用来说明str是一个变量。puts函数在标准输出显示变量的内容。

数组也可以用set语句定义,实际上,tcl中建立数组只是单个建立数组的元素。例如


% set arr(1) 0

0

% set arr(2) 1

1

这样就建立了一个两个元素的数组arr。在TCL中,不存在相当于数组边界这样的东西
,例如

% set arr(100) to

to

这时数组中实际只存在arr(1),arr(2)和arr(100),这是和C语言不同的地方。用arr
ay size命令可以返回数组的大小:

% array size arr

3

访问数组的方法和访问标两实际是一样的,例如:

% puts $arr(100)

to

可以用同样的方法创建多维数组。

要使用数组中的所有元素,需要使用一种特殊的便利方式。首先要启动startsearsh:

% array startsearch arr

s-1-arr

这里返回了一个搜索id,你可以把它传递给某个变量,因为以后还要使用它进行进一
步的搜索:

% set my_id [array startsearch arr]

s-1-arr

现在my_id的内容是s-1-arr,然后,就可以搜索arr的内容了:

% array nextelement arr $my_id

whi

这里的array nextelement返回的是什么?可能有点出乎你的意料,是arr数组的下标
,再执行一次array nextelement命令又会找出另外一个下标:

% array nextelement arr $my_id

4

这样遍历下去,可以找出arr数组的所有下标,而知道下标之后,就可以用$arr(4)之
类的方式访问arr的内容了。当遍历完成之后,array nextelement命令将简单地返回:

% array nextelement arr $my_id

%

这时就可以停止遍历过程了,如果你想确认遍历是否完成,可以使用array anymore命
令:

% array anymore arr $my_id

0

返回0说明遍历已经完成。

串处理

TCL中可以进行一般的串处理过程,这可以使用string命令和append命令,append命令
将某个字符串加到另外一个字符串的后面:

% set str1 "test "

test

% set str2 "cook it"

cook it

% append str1 $str2 " and other"

test cook it and other

string命令可以执行字符串的比较,删除和查询,其格式是 string [参数] string1
[string2]

参数可以是下面的命令之一:

compare 按照字典顺序对字符串进行比较,根据相对关系返回-1,0或者+1。

first 返回string2中第一次出现string1的位置,如果失败,返回-1。

last 返回string2中最后一次出现string1的位置,如果失败,返回-1

trim 从string1中删除开头和结尾的出现在string2中的字符

trimleft 从string1中删除开头的出现在string2中的字符。

trimright 从string1中删除结尾的出现在string2中的字符

下面几个用在string中的参数不需要string2变量:

length 返回tring1的长度

tolower 返回将string1全部小写化的串

toupper 返回将string1全部大写化的串

运算

TCL的运算方式比较别扭,它使用expr命令作为计算符号,其用法类似C语言的+=和/=
,例如,

% set j [expr $i/5]

1

注意TCL会自动选择整数或者浮点计算:

% set l [ expr $i /4.0]

1.25

% set l [ expr $i /4]

1

在TCL里面可以使用+ - * /和%作为基本运算符,另外通常还包括一些数学函数,如a
bs,sin,cos,exp和power(乘方)等等。

另外,还有一个起运算符作用的命令incr,它用来对变量加一:

% set i 1

1

% incr i

2

流程控制

tcl支持分支和循环。分支语句可以使用if和switch实现。if语句的和C语言类似,如

if { $ x < 0 } {

set y 10;

}

注意判断子句也需要使用花括号。

与C语言一样,tcl的if语句也可以使用else和elseif。

switch语句的用法有点类似这样:

switch $x {

0 { set y 10;}

10 { set y 100;}

20 { set y 400;}

}

与C的switch语句不同,每次只有符合分支值的子句才被执行。

循环命令主要由for,foreach和while构成,而且每一个都可以使用break和continue
子句。

for语句的格式有点类似这样:

for { set i 0} {$i < 10} { incr i} {puts $i}

将会输出从1到9的整数。

如果用while循环,这个句子可以写成

while {$i < 10 } {

puts $i;

incr i;

}

foreach是对于集合中的每一个元素执行一次命令,大致的命令格式是

foreach [变量] { 集合 } {

语句;

}

例如

% foreach j { 1 3 5} {

put $j;

}

1

3

5

函数

如同在一般的编程语言里面一样,在tcl里面也可以定义函数,这是通过proc命令实现
的:

proc my_proc {i}{

puts $i;

}

这样就定义了一个名字叫proc的函数,它只是在终端显示输入变元的内容。

要使用这个函数,简单地输入它的名字:

% my_proc { 5 }

5

如果变元的数目是0,只要使用空的变元列表,例如 proc my_proc {} {语句;}


尽管tcl还可以处理更复杂的过程,但是我们不再介绍了,例如文件的读写以及tk图形
语言,因为我们处理tcl的主要目标就是理解expect,对于更复杂的编程工作,我们建议
你使用perl。

11.1.2 expect

expect是建立在tcl基础上的一个工具,它用来让一些需要交互的任务自动化地完成。
我们首先从一个简单的例子开始,如同在这一节一开始就提到的,我们想设置一个自动
的文件下载程序。

我们看一看这样的一个例子脚本:

#! /usr/bin/expect

spawn ftp 202.199.248.11

expect "Name"

send "ftp\r"

expect "Password:"

send "nothing\r"

expect "apply"

send "cd /pub/UNIX/Linux/remoteX\r"

expect "successful."

send "bin\r"

expect "set to I"

send "get exceed5.zip\r"

expect "complete."

send "quit\r"

这个是什么意思?呵呵,就是个自动下载程序。第一行说明这个程序应该调用/usr/b
in/expect去执行,然后的就是expect命令。

察看expect的手册页面(man expect)可以得到一个很长的expect说明,可惜其中关于
expect的语法仍然介绍的不够。一般来说,expect主要用在需要自动执行人机交互的过
程中,例如fsck程序,这个程序会不断地提问"yes/no",像这样的命令就可以用expect
来完成。

spawn语句在expect脚本中用于启动一个新的进程,在我们的程序中,spawn ftp 202
.199.248.11就是去执行ftp程序,接下来,就是expect和send的指令对了。

每一对expect和send指令代表一个信息/回应。如果这样说不好理解的话,那么可以看
一看ftp的具体执行过程:

ftp 202.199.248.11

Connected to 202.199.248.11.

220 mail.asnc.edu.cn FTP server (BeroFTPD 1.3.3(3) Sun Feb 20 15:52:49 CST
2000.

Name (202.199.248.11:wanghy):

显然,一旦连接成功,服务器会返回一个Name(202.199.248.11:wanghy):的字符串来
要求客户给出用户名。expect语句简单地在返回信息中查询你给出的字符串,一旦成功
就执行下面的命令,现在,expect " Name"已经成功地找到了Name字符串,接下来可以
执行send命令了。

send命令比expect命令更简单,它简单地向标准输入提交你设定的字符串,现在设置
为send "ftp\r"表示等到登录信息之后就给出一个输入ftp回车,也就是标准的登录过
程。

下面的行与这些行完全一样,只是机械地等待服务器的回应,并且提交自己的输入。

要使用这个expect脚本,你只需要将它设置为可执行的属性,然后执行它,expect就
会执行你需要的服务。

由于expect是tcl的扩展,所以你在expect文件中可以象tcl脚本一样设置变量和程序
流程。

现在我们看一看我们还能够如何改进我们的expect脚本。ftp命令可能会失败,比如远
端的机器可能会无法提供服务,或者在启动ftp命令时本地机器发生问题。为了处理这一
类的问题,我们可以使用expect的timeout选项来设置超时的话expect脚本自动退出:

#! /usr/bin/expect

spawn ftp 202.199.248.11

expect {

timeout exit

Connect

}

………………

注意这里面使用的花括号。它的含义是使用一组并列表达式。使用并列表达式的主要
原因是这样:如果使用下面的指令对:

expect timeout

exit

那么由于expect脚本是顺序执行的,那么当程序执行到这个expect的时候就会阻塞,
所以程序会一直等待到timeout然后退出。并列表达式则是相当于switch的行为,只要列
出的几项内容有一项得到满足,expect命令就得到满足,于是程序可以正常执行。上面
的脚本表示,如果连接ftp的时候发生了超时,那么就退出,否则,一旦发现Connect应
答,说明服务器已经正常了,那么就可以继续运行了。

我们可以看看用tcl能够对我们的expect脚本提供什么帮助。我们可以设置让expect脚
本不断地连接远端服务器的服务,直到正常建立连接开始,为此,我们可以把建立连接
的命令放在一个循环里面,并且根据回应的不同自动选择重新输入命令还是继续执行:

spawn ftp

while {1} {

expect "ftp>"

send "o 202.199.248.11\r"

expect {

"Connected" break

"refused" { sleep 10} ;

}

}

这里使用了我们在tcl语言中讲到的while和break命令,熟悉C的读者应该很容易看出
它的行为:不断地等待ftp>提示符,在提示符下面发送连接远端服务器的命令,如果服
务器回应是refused(连接失败),就等待10秒钟,然后开始下一次循环;如果是Conne
cted,那么就跳出循环执行下面的命令。sleep是expect的一个标准命令,表示暂停若干
秒钟。

expect还支持许多更复杂的进程控制方式,如fork,disconnect等等,你可以从手册
页面中得到详细的信息。另外,各种tcl运算符和流程控制命令,包括tcl函数也可以使
用。

有些读者可能会问,如果expect执行的话是否控制台输入不能使用了,答案是否定的
。expect命令运行时,如果某个等待的信息没有得到,那么程序会阻塞在相应的expect
语句处,这时,你在键盘上输入的东西仍然可以正常地传递到程序中去,其实对于那些
expect处理的信息,原则上你输入的内容仍然有效,只是expect的反映太快,总是抢在
你的前面"输入"就是了。知道了这一点之后,你就可能写一个expect脚本,让expect
自动处理来自fscki的那些恶心的yes/no选项(我们介绍过,这些yes/no其实完全是多余
的,正常情况下你除了选择yes之外什么也干不了)。

缺省下,expect在标准输出(你的终端上)输出所有来自应用程序的回应信息,你可
以用下面的两个命令重定向这些信息:

log_file [文件名]

这个命令让expect在你设置的文件中记录输出信息。必须注意,这个选项并不影响控
制台输出信息,不过如果你通过crond设置expect脚本在半夜运行的话,你就确实可能需
要这个命令来记录各种信息了。例如:

log_file expect.log

log_user 0/1

这个选项设置是否显示输出信息,设置为1时是缺省值,为0 的话,expect将不产生任
何输出信息,或者说简单地过滤掉控制台输出。必须记住,如果你用log_user 0关闭了
控制台输出,那么你同时也就关闭了对记录文件的输出。

这一点很让人困扰,如果你确实想要记录expect的输出却不想让它在控制台上制造垃
圾的话,你可以简单地把expect的输出重定向到/dev/null:

./test.exp > /dev/null

你可以象下面这样使用一对fork和disconnect命令。expect的disconnect命令将使得
相应的进程到后台执行,输入和输出被重定向到/dev/null:

if [fork]!=0 exit

disconnect

fork命令会产生出一个子进程,而且它产生返回值,如果返回的是0,说明这是一个子
进程,如果不为0,那么是父进程。因此,执行了fork命令之后,父进程死亡而子进程被
disconnect命令放到后台执行。注意disconnect命令只能对子进程使用。

11.2 awk和文件的处理

UNIX里面充斥着各种记录文件和类似的东西。对文本文件的处理是系统管理员每天重
要的工作,例如从系统记录中查找重要的内容,或者对某种程序的输出进行统计等等。
我们将介绍常用的一个处理程序,即gawk。

11.2.1 grep和正则表达式

让我们首先从grep命令开始。这个命令大家应该很熟悉了,它用来在文件中查找一个
字符串。不过,实际上,grep的处理功能要强大和复杂的多。

grep 命令的语法是

grep [模式] [文件名]

如果没有给出文件名,就缺省使用标准输入。grep每次读取一行,并且和给出的模式
进行匹配,如果成功就把这一行会显,例如:(粗体的是我们输入的内容)

$ grep test

close

test my hand

test my hand


grep的"模式"也称为正则表达式,可以由各种基本的正则表达式元素构成。正则表
达式元素主要包括下面几种:

字符串 匹配任何字符串,例如grep test表示在标准输入中1

[...] 封闭集中匹配一个字符,如:[abcde]可以匹配a,b,c,d,e

[^...] 求补集中匹配一个字符,例如[^ABC]匹配

. 匹配任意字符

\s 空白符

\S 非空白符

\d 数字

\D 非数字

\w 字母或数字

\W 非字母和数字

* 匹配任何字符

上面的形式是grep中使用的基本正则表达式,另外,还可以使用egrep,egrep是grep
的一个扩展版本,支持下面这些扩展的正则字符串:

^ 匹配一行的开始

$ 匹配一行的结尾

( ) 确定正则表达式求值顺序,和正常演算中的括号意思差不多。

(...|...|...) 或,可选项之一进行匹配,例如:(abc|dev|ghi)可以匹配abc,dev,gh
i,而(ww|gg)do可以匹配wwdo或者ggdo。

+ 一次或多次模式

如:aba+匹配aba,abaa...不匹配ab

通常,我们有两种方法使用grep和egrep,一种是使用管道,例如我们应该熟悉的ps
ax |grep sendmail,另一种是直接在文件中搜索对应的字符串。

grep/egrep还可以在命令行使用开关,常用的开关包括:

-b 在行前加上块号

-c 统计匹配行的个数

-n 在行前加上行号

-w 将模式解释为字符串,所有正则表达式的控制命令失效

-x 精确匹配

-r 查询文件时包含子目录

举个例子来说,我们想在/var/log/httpd/access_log中查询所有不是来自本地(192
.168.0.1)的请求记录,可以执行:

grep �v "^192.168.0.1" /var/log/httpd/access_log

^用来让grep 只在行首匹配。

在grep查询的时候可以使用通配符代表多个文件,例如,grep start * -r将在当前目
录以及所有子目录的所有文件中查询start字符串。

11.2.2 gawk的使用方法

gawk是awk的一个实现,awk是一种用来处理报告等文本文件的脚本语言。不过,我们
介绍这个产品的主要目标是用它来处理各种程序的记账文件。对于复杂的脚本,还是用
Perl比较合适。

gawk 的主要功能是针对档案的每一行搜寻指定的 模式。,每当找到一个匹配的模式
,gawk就会去执行你设定的动作。按照这个方式, gawk 依此方式处理输入档案的每一
行直到输入档案结束。如果对于某个模式没有设置对应的动作,gawk将直接将这个行显
示出来。

为了使用gawk,你通常必须先写一个awk脚本,除非模式/动作非常简单,可以在一行
上完成。我们用一个例子来解释gawk的基本用法,首先产生一个目录列表文件:

ls �l /etc > list

现在list的内容有点像这样:

total 2164

drwxr-xr-x 3 root root 4096 Feb 15 22:55 CORBA

-rw-r--r-- 1 root root 2045 Sep 24 1999 DIR_COLORS

-rw-r--r-- 1 root root 17 Mar 25 19:59 HOSTNAME

…………

现在我们选择一个最简单的例子,简单地查找所有属性是drwxr-xr-x的目录文件:

gawk '/drwxr-xr-x/ {print $0}' list

将输出所有这样的目录。

这个例子看上去没有什么实际用处,因为用grep也可以做同样的动作,那么我们可以
看一看下面这个功能:

$ gawk '$1=="-rwxr-xr-x" {sum=sum+$5} END {print sum}' list

15041

这个是什么意思?对于所有属性是755的文件,让gawk对第五栏的数字求和。第五栏我
们可以看到就是文件的长度,因此这个命令将显示所有属性为755的文件的总共的长度。

$n是gawk中非常重要的概念,它用来表示文本串的分栏。缺省的情况下,gawk将输入
字符串(从文件中读入的每一行)按照分割的空格分成若干个字段,每个字段作为一个
变量,例如有一行

my name is 3th test

那么,在awk读入这一行之后,就产生了$1到$5变量,其中$1="my",$2="is",………
,最后$5="test"。另外还有一个特殊的变量$0,它表示整个输入行,也就是这个字符串
"my name is test"。另外还有一个特殊的变量NF,它表示当前行的字段的个数,在现在
的情况下,NF应该等于5。

在某些特殊的情况下,你可能需要改变分割符的定义,这可以通过对FS赋值来完成,
例如FS=","将分割符定义为都号而不是缺省的空格。

在一般情况下,gawk可以从命令文件中获得模式/动作,命令文件的格式很简单,就是
直接将应该写在命令行上的模式/动作对写在文件里面,每个对构成一行,模式可以有两
种,一种是模式匹配,也就是我们在前面解释的正则表达式,如果使用正则表达式,那
么需要用两个/把它们夹在一起,例如/[A-Z]/表示正则表达式[A-Z]。

另一种模式是比较指令,比较指令可以用比较操作符和逻辑运算符来构成,常用的比
较操作符有:

== 等于 <= 不大于 ~ 按照正则表达式匹配

< 小于 >= 不小于 !~ 按照正则表达式不匹配

> 大于 != 不等于

逻辑运算符有

&& 和 || 或 ! 非 ()括号


设定了模式后,就可以设置对应的动作了,在gawk中,动作必须用花括号括起来。ga
wk能完成的动作并不多,毕竟它是一种报告分析语言。一般情况下,只要熟悉print和p
rintf命令就足够了,print命令的格式非常简单:

print item1,item2,…………

输出时,每个项目输出一栏,中间用空格分开。一个print后面不跟着任何变量会导致
gawk显示当前的输入行($0)。如果要输出一个字符串,使用引号把它括起来,特别是
如果要输出一个空行,使用print ""。这里是一个例子,它将list文件的头两栏输出:

gawk '{print $1,$2}' list

由于输入的文本文件内容有多行,你在命令栏中设计的模式/动作会对每一行执行一次
。就是:

total 2164

drwxr-xr-x 3

-rw-r--r-- 1

-rw-r--r-- 1

-rw-r--r-1

…………………

如果你要精确地控制输出,也可以使用printf命令,这个命令的格式是:

printf format, item1, item2, ...

format参数就是C语言里面的格式控制符,例如%c,%d,%f等等。在 % 与格式控制
字母之间可加入 modifier,modifier 是用来进一步控制输出的格式。可能的 modifie
r 如下所示:

'-' 使用在 width 之前,指明是向左靠齐。如果'-'没有出现,则会在被指定的
宽度向右靠齐。例如:

printf "%-4S", "foo"会印出'foo '。

'width' 这一个数字指示相对应的栏位印出时的宽度。例如:

printf "%4s","foo" 会印出' foo'。

width 的值是一个最小宽度而非最大宽度。如果一个 item 的值需要的宽度
比 width 大,则不受 width 的影响。例如printf "%4s","foobar"将印出'foobar'。

'.prec' 此数字指定印出时的精确度。它指定小数点右边的位数。如果是要印出一个
字串,它指定此字串最多会被印出多少个字符。

作为一种脚本语言,gawk允许使用变量,定义变量非常简单,就是直接用等号对它赋
值。为了在gawk程序的开始处对变量赋值,gawk专门提供了BEGIN语句,这个语句将在所
有行被读入之前执行,而且只执行一次,通常用它来执行初始化命令,例如

BEGIN { sum=0;count=0;average=0.0;}

对于变量可以使用数学表达式进行运算,运算符包括常见的加减乘除算符,以及^(乘
方),%(取余)和著名的++,--。不过注意gawk在做除法的时候总是使用浮点除法,除了
取余算符%。

函数

另外,gawk包含下列函数:

数学函数

atan2(x,y) y/x的正切

cos(x) 余弦函数

sin(x) 正弦函数

int(x) 取整

log(x) 取自然对数

exp(x) 指数函数

rand(x) 生成一个0到1之间的随机数

srand() 初始化随机数发生器

systime() 返回从1970年1月1日0:00到当前时间的秒数

sqrt(x) 取x的平方根

字符串函数

index(string1,string2 )

它会在string1 里面,寻找string2 第一次出现的地方,返回值是字串string2出
现在字串string1 里面的位置。如果找不到,返回值为 0。

例如:

print index("peanut","an")

会印出 3。

length(string)

string字符串的长度

例如:

length("abcde")

是 5。

match(string,regexp)

match 函数会在字串 string 里面,寻找符合 regexp 的最长、最靠左边的子字
串。返回值是 regexp 在 string 的开始位置,即 index值。这个函数会设定内部变量
RSTART 等於 index,内部变量RLENGTH 等於符合的子串个数。如果不符合,则会设定
RSTART 为0、RLENGTH 为 -1。

sprintf(format,expression1,...)

跟C语言的sprintf差不多。

例如:

sprintf("pi = %.2f (approx.)',22/7)

传回的字串为"pi = 3.14 (approx.)"


sub(regexp, replacement,target)

在字串 target 里面,寻找符合 regexp 的最长、最靠左边的地方,并且以字串
replacement 代替最左边的 regexp。

例如:

str = "water, water, everywhere"

sub(/at/, "ith",str)

结果字串str会变成

"wither, water, everywhere"


gsub(regexp, replacement, target)

gsub 与前面的 sub 类似。在字串 target 里面,寻找符合 regexp 的所有地方
,以字串 replacement 代替所有的 regexp。

例如:

str="water, water, everywhere"

gsub(/at/, "ith",str)

结果字串str会变成

'wither, wither, everywhere"

substr(string, start, length)

传回字串 string 的子字串,这个子字串的长度为 length 个字符,从第 start
个位置开始。

例如:

substr("washington",5,3)

传回值为"ing"

如果 length 没有出现,则传回的子字串是从第 start 个位置开始至结束。

例如:

substr("washington",5)

传回值为"ington"

tolower(string)

将字串string的大写字母改为小写字母。

例如:

tolower("MiXeD cAsE 123")

传回值为"mixed case 123"


toupper(string)

将字串string的小写字母改为大写字母。

例如:

toupper("MiXeD cAsE 123")

传回值为"MIXED CASE 123"

其他函数

system(command)

此函式允许使用者执行作业系统的指令,执行完毕後将回到 gawk

程式。

例如:

BEGIN {system("ls")}

控制流

在gawk命令脚本中可以使用控制流,主要是if,for,while等语句,用法和C语言相当
类似:


if (condition) then-body [else else-body]

如果 condition 为真(true),则执行 then-body,否则执行 else-body。

举一个例子如下:

if (x % 2 == 0)

print "x is even"

else

print "x is odd"


while (condition)

body

while 语句测试 condition表达式。假如 condition 为真则执行 body 的语句。一次
执行完後,会再测试 condition,假如condition 为真,则 body 会再度被执行。这个
过程会一直被重复直到condition 不再是真。如果 condition 第一次测试就是伪(fals
e),则body 从没有被执行。

下面的例子会印出每个输入行的前三个栏位。

gawk '{ i=1

while (i <= 3) {

print $i

i++

}

}'

do

body

while (condition)

这个 do loop 执行 body 一次,然後只要 condition 是真则会重复执行 body。即使
开始时 condition 是伪,body 也会被执行一次。

下面的例子会印出每个输入记录十次。

gawk '{ i= 1

do {

print $0

i++

} while (i <= 10)

}'

for (initialization; condition; increment)

body

此叙述开始时会执行initialization,然後只要 condition是真,它

会重复执行body与做increment 。

下面的例子会印出每个输入记录的前三个栏位。

gawk '{ for (i=1; i<=3; i++)

print $i

}'

break 会跳出包含它的 for、while、do-while 循环的最内层。

下面的例子会找出任何整数的最小除数,它也会判断是否为质数。

gawk '# find smallest divisor of num

{ num=$1

for (div=2; div*div <=num; div++)

if (num % div == 0)

break

if (num % div == 0)

printf "Smallest divisor of %d is %d\n", num, div

else

printf "%d is prime\n", num }'

continue 使用于 for、while、do-while 循环内部,它会跳过循环体的剩余部分
,立刻进行下一次循环的执行。

下面的例子会印出 0 至 20 的全部数字,但是 5 并不会被印出。

gawk 'BEGIN {

for (x=0; x<=20; x++) {

if (x==5)

continue

printf ("%d",x)

}

print ""

}'

next 语句强迫 gawk 立刻停止处理目前的行而继续下一个输入行。

exit 语句会使得 gawk 程式停止执行而跳出。然而,如果 END 出现,它会去执
行 END 的 actions。


自定义函数

你可以定义自己的函数,其格式是

function name (parameter-list) {

body-of-function

}


name 是所定义的函数名字。 parameter-list 是函数的变量列表。变量间使用逗号分
开。

函数可以在程序的任何地方定义,不过习惯上总是定义在程序的开头部分。

下面这个例子,会将每个记录的第一个栏位之值的平方与第二个栏位之值的平方加
起来。

{print "sum =",SquareSum($1,$2)}


function SquareSum(x,y) {

sum=x*x+y*y

return sum

}

如果你熟悉任何编程语言,那么掌握awk都是很轻松的事情,如果你不喜欢它,那么你
可以参考我们下面介绍的perl。

11.3 Perl

Perl是从awk发展起来的,它由Larry Wall在1986年发明。它是一种功能强大的编程语
言,而且可以在许多平台上使用。实际上,你完全可以将Perl作为一种标准编程语言(
而不是脚本语言)来使用,笔者非常喜欢它,并且建议所有不想学习C语言的UNIX管理员
应该掌握Perl的基本编程技术。目前,常用的版本是perl 5,几乎所有的Linux发行版本
都会包含它,缺省时,linux的perl 5安装在/usr/bin下,命令是/usr/bin/perl.


11.3.1 基本语法


perl的语法介于C和basic之间,一个perl程序由若干行组成,使用的时候由perl解释
程序解释执行。每个完整的行都应该用分号结尾。

Perl的基本语法是这样的:

① 变量和运算符

在perl中,所有变量都不需要提前声明。一旦对某个变量赋值,就自动产生了这个变
量。perl的变量有普通变量,数组和关联数组三种。普通变量就是数值和字符串,要声
明一个普通变量,在变量名字前面加上$,例如

$string1="aaa";

$test=5;

$u=1.33;

同样,访问变量内容也需要使用$符号。


数组用@字符标志,如

@name1=("tom","marry","john");

$b=$name[0]; $b现在等于"tom"

$b=@name[0];跟上一句是一样的

$name[0,2]=["help","so"];现在@name等于["help","marry","so"]

@name[0,2]==@name[2,0];交换0,2元素

数组的大小不是固定的,你可以动态地添加数组元素,例如

$name[3]="app";增加一个元素

直接访问数组名字将得到数组中元素的个数,例如:

$count=@name;将name的元素个数存放到$count变量中。


关联数组是一种特殊的数组,每个元素都由一对元素构成。或者说,关联数组是一种
下标不是整数的数组,要声明一个关联数组,使用%符号,例如:

%arr=(1,"one",2,"two",3,"three",4,"four");

这时可以用前面的值(key)来索引后面的值:

$one=$arr{1};这时$one等于"one"

注意关联数组的访问方式,是使用$关联数组名字[索引号]。

你可以把关联数组看成数据库的一种实现。与一般的数组一样,其大小也可以动态调
节:

$arr{5}="five";增加一对数据。

可以将关联数组简单地变成普通数组,例如

@X=%arr;现在@X的内容是X[0]="1",X[1]="one",……………

perl的运算符与C语言以及我们介绍的gawk很相似,包括普通的+-*/%以及来自C语言的
逻辑运算符&&(和),||(或),等等,下面是一个列表:

+ - * / 四则运算,注意perl的除法是浮点除法

$a % $b a对b取余数,例如3%2的结果是1

$1 .. $2 区段运算符,这个算符取出$1和$2中间的所有值,例如1..9返回一个表
1,2,………9。通常用这个命令初始化一个数组,例如:@dec=1..9;@oth=(1..26,'A
'..'Z')等等。

= 赋值算符

> < >= <= == !=

这几个算符是数字之间的比较算符。

perl中没有专门的boolean型变量,而是象C语言一样认为所有不为零的量为真值,而
0或者空字符串为假。与C语言类似,Perl支持以下的逻辑运算符:

&& 与 || 或 ! 非

同样,perl也支持位运算:

& 与 || 或 ^ 异或

还有就是与C语言相似的运算符使用方式,如

$i+=$j; 等效于$i=$i+$j,同样还可以使用$i-=5;$i&=12。这样的算式

$i++; 等效于C语言的++,将i加一,++$i,$i--,--$i都是可以使用的。

除了上面的标准算式之外,perl支持字符串运算,首先是字符串之间的比较命令:

$str1 gt $str2 $str1大于$str2

$str1 lt $str2 $str1小于$str2

$str1 ge $str2 $str1不小于$str2

$str1 le $str2 $str1不大于$str2

$str1 eq $str2 $str1等于$str2

$str1 ne $str2 $str1不等于$str2

$str1 cmp $str2 根据$str1是大于,等于还是小于$str2,返回1,0或者-1。

上面的字符串比较都是使用字典顺序,即ASCII码的顺序。

另一个非常有用的运算符是点号运算符,这个运算符用于把两个字符串连接成一个,
例如:

$str1="string1";

$str2="string2";

$string3=$str1.$str2;这时$string3等于"string1string2"

②基本语句和函数:

#


这个符号代表注释的开始。


print


显示字符串,写文件,如

print "hello\n";或者print "the var is $i","\n";注意变量名会自动地被替换成变
量值,除非你用一个\符号明确地告诉perl:

print "\$i is a str";

print FILE "hello\n";向FILE对应的文件写,FILE是一个文件句柄;

split 分割字符串,格式split(/模式/,$string);

例如$string="i:am:perl";

@list=split(/:/,$string);

#这时@list=("i","am","perl")

($a,$b,$c)=split(/:/,$string);


delete $ARRAY(key)


这个函数用于在关联数组中删除一对记录。例如,%arr=(1,"one",2,"two",3,"
three"); delete $arr(2);执行上述操作之后,%arr的内容变为(1,"one",2,"two
")。


keys(%ARRAY)


取出关联数组%ARRAY中所有的索引key。这个操作将返回一个数组。例如,对于上面的
%arr,执行@test=key(%arr)的结果是@test成为(1,2)。


values(%ARRAY)


取出关联数组%ARRAY中所有的value,同样返回一个数组,例如对于%arr,@test=val
ues(%arr)的结果是@test变成("one","two")。


reverse(@array)


把@array反转排列。例如@test=(1,2,5,3,10),@other=reverse(@test)的结果是@ot
her变成(10,3,5,2,1)。


sort(@array)


排序,注意这个排序是按照字符串的排序,例如@other=sort(@other)的结果是(1,
10,2,3,5)。


chop($string)


删除字符串的最后一个字符,通常用于去掉输入字符串中的回车符。


lenth($string)


取字符串长度


substr($string,offset,length)


取字符串子串,即从$string的offset偏移量处截取length长度的字符串作为子串返回


index($string,$substring)


在$string中查找$substring,成功的话,返回$substring在$string中的偏移量,如
果不存在就返回-1。


push(@array,$string)


在@array末尾加入$string


pop(@array)


删除@array的末尾元素并返回这个元素


shift(@array)


删除@array的开头元素并且返回这个元素


join($string,@array)


在@array中间加入$string并返回结果


grep(/pattern/,@array)


在@array中用正则方式查找符合条件的元素


hex($string)


将16进制转化为十进制


rand


产生随机数,注意应该先执行srand初始化随机数种子。


localtime


返回时间数组


die LIST


显示字符串并且退出程序


pack("格式",LIST)


把一个LIST转换成指定的二进制格式,例如:$string=pack('C",65)这时$string等于
ASCII的65,即"A"


反引号


用反引号将某个字符串括起来的效果是使perl执行系统命令,这里使用的反引号是大
键盘最左边键,如`ls`执行ls命令。


③使用文件


在perl中使用文本文件非常简单,只要使用open和close打开和关闭文件:


open 打开文件

close 关闭文件


open函数的格式是open(Filehandle,$filename),这个操作将会打开$filename文件
,并且让Filehandle句柄指向打开的文件。如果失败,将返回false。缺省下,文件是以
只读的方式打开的。如果要打开名字为$filename文件用于输出,使用open(Filehandle
,">$filename")。想要在某个文件的后面追加内容,使用open(Filehandle,">>$filena
me")。当然,open(Filehandle,"<$filename")也是可以使用的,不过这就等于open(Fi
lehandle,"$filename")。

read 读文件,格式是read (Filehandle,$string,length),这函数从Filehandle指向
的文件中读取length个字符,存放到$string变量中。如果你要得到标准输入,使用STD
IN的句柄。

close (Filehandle)将关闭由open语句打开的文件。

除了用read语句的标准方法之外,还有一个经常用的方法:

$filecontent=<FILE>;从句柄FILE指向的文件中读取一行,内容存入$filecontent变
量。如果你要从控制台读取一个字符串,使用$input=<STDIN>;就可以了。

下面是一个例子:

$filename="test";

open (FILE,"$filename")||die "can not open file!;

while($line=<FILE>{

print "$line";

}

close(FILE);

这个程序实际就是cat命令的perl语言实现,open命令打开当前目录下面的test文件,
并且把句柄返回到FILE变量,注意这一行的用法,Perl的||(或)运算是短路求值的,如
果open成功,那么返回一个非0的数,因此这算式无论如何都会为真,所以会跳过||后面
的东西;否则,如果open失败,perl就要对后面的东西执行一下,于是退出这个程序。

打开成功之后,perl会得到这个文件的句柄,下面的句子就是反复读取文件的每一行
并且显示出来,当文件读到末尾的时候,$line=<FILE>将产生一个空字符串,于是whil
e循环结束。

与shell脚本语言类似,perl还有一些文件测试运算符


-t $file


如果$file这个文件可读,返回1,$file是文件名。


-w $file


如果$file可写,返回1


-x $file


如果$file可以执行,返回1


-e $file


如果$file存在,返回1


-o $file


如果用户是$file的拥有者,返回1


-s $file


返回$file文件的大小


-f $file


是否为正常文件


-T $file


是否文本


-B $file


是否二进制文件


-M $file


文件从更新到现在的日期数


④流程控制


perl支持与C语言很相似的流程控制语句:


if和if..else:

if语句的语法是

if(...){

clause;

}

与C语言不同,即使只有一行程序,if后面的花括号也不能省略,这一点也适用于后面
说的其他复合语句。

与C语言类似,也可以用else和elseif子句:

if(...){

clause1;}

else {

clause2;

}

或者

if (…){

...

}

elseif(…){

....

}

else{

...

}

另外,perl还支持unless语句:

unless(exp1){

clause1;

}

如果exp1不成立,就执行clause1子句。这个unless语句里面也可以使用else子句。实
际上,这就是一种否定形式的if……else语句。


while循环语句:

while的语法有两种,分别是将表达式放在循环首部和尾部,第一种形式是:

while(exp){

clause;

}

第二种形式是

do {

clause;

}

while(exp);

都是循环执行clause直到exp不成立,不过一个在循环头部判断exp表达式是否为真,
另一个是在循环尾部。


until循环


语法是until(exp){

clause;

}

反复执行clause直到exp成立。

for循环


for(初始化;继续条件;增量){


循环体;

}


这个for循环和C的一样,首先执行初始化语句,然后开始循环执行循环体,每次循环
都调用一次增量表达式,直到循环继续条件不再成立。例如

for ($1=0;$i<10;$i++){

print $i;

}

将显示出从0到9的所有整数。


foreach $variable(@array){


循环体;

}


这个类似于shell语言的foreach,它把@array的内容一条一条赋给$variable并执行里
面的语句。


跳出循环

有两个语句用来实现特殊控制:


last if 用在循环里,相当于break;

next if 相当于continue.


⑤文字处理运算


除了正常的字符串处理语句外,perl还支持一种文字处理运算方式,基本格式是$str
ing=~(文字处理模式)。这个文字处理模式是perl的主要优点之一,由于perl的文字处
理运算模式太强大了,这里只能介绍几个非常基本的形式。

首先解释一下pattern的概念.pattern一般是用两个/字符夹在一起的一些字符串, 用
来代表一些具有某些特点的字符串, 实际上,这个patternj基本上就是grep/egrep里面
的正则表达式,只是功能更丰富一点。常用的有:

任意字符串: 匹配该字符串

[0-9] 匹配所有数字字符

[a-z] 所有小写字母

[^0-9] 所有的非数字

[^a-z] 所有的非小写字母

[A-Z] 所有的大写字母

^ 字符串开头的字符

$ 字符串结尾的字符

\d 跟[0-9]一样

\D 非数字字符

\w 就相当于[a-zA-Z0-9]

\W 相当于[^a-zA-Z0-9]

\s 一个空白的字符

\S 非空白的字符

\d+ 一个相当于数字的字符串

\w+ 一个完全由数字或字符构成的字符串

\b 一个不由英文字母或者数字为边界的字符串

\B 一个由字母或数字为边界的字符串

a|b|c 符合a,b,c之一的字符串

/pattern/i i代表忽略大小写

[] 找寻符合[]内的字符,例如[abde]可以匹配a,b,d,e的任何一种

? {m} 正好是m个指定的字母

{m,n} 多于m少于n个指定的字符

\ 如果要引用一些在pattern中具有特殊意义的字符,使用\前缀


在perl的文本运算模式中,最常用的是=~或者!~运算符号和s,tr两个函数。=~表示匹配
,它用右边的模式来匹配左边的字符串,如果成功就得到一个true,!~正好相反,代表
不匹配,例如

$string="chmod711cgi";

if($string=~"/chmod/"){

print "Found chmod!\n";}

$string=~"/chmod/"这样的表达式询问是否/chmod/可以匹配$string,因为$string中
包含chmod,所以if语句会成功地执行。

tr是串转换函数,例如:

$string=~tr/a-z/A-Z/;

将字符串中的小写字母转换成大写.

s是串取代函数,例如:

$string=~s/a/A/;

把第一个a换成A,还可以加后缀g表示全程替换,例如:

$string=~s/1/A/g;

变换后,$string变成chmod7AAcgi.

注意tr和s的区别,tr的多替换一般是一对一的,或者说是字符的替换,而s是字符串的
替换,长度可以改变,例如如果$string的值是chmod711cgi,那么

$string=~s/1/ONE/g;

将变成chmod7ONEONEcgi.而

$string=~tr/1/ONE/;

将把$string变成chmod7oocgi。

还有一个很实用的功能是变数替换,例如:

$string="test24";

$string=~s/(\d+)/<$1>/;

这时,s首先搜索满足\d+(数字)的串,得到24,然后送入$1,接着再套上<>,结果是 "tes
t<24>".

⑥内置变量

除了用户定义的变量之外,perl还定义了一些内置变量,它们在perl中有特殊的意义
,用户不能修改它们的含义(有些可以赋值),下面是常用的内置变量:

@ARGV

这是个数组,它代表的是传递给perl程序的命令行开关,如@ARGV[0]是第一个参数,
@ARGV[1]是第二个等等。如果你的perl程序名字叫test,而你用test me other去调用它
,那么@ARGV[0]是"me",@ARGV[1]是other,以此类推。

$_和$1,$2,………

这几个参数用于文本处理模式中的临时变量。举个例子来说,串匹配模式中:

$string="chmod711cgi"

$string=~/(\w+)\s+(\d+)/;

看上去这第二行代码什么也不做,因为它仅仅是个匹配语句。不过,实际上,由于pe
rl会把临时变量放进$n变量,所以它会修改$_和$1,………变量。在这个匹配模式中,
首先的\w+匹配一组字符,成功,得到chmod字符串,于是perl将它保存到$1;然后,\s
+匹配一组空白符号,失败;最后,\d+匹配一组数字,成功,得到711,perl将它保存到
$2。

如果在匹配模式中没有指明对那个串变量使用匹配模式,就使用$_进行匹配。

⑦自定义函数

Perl程序中允许定义自己的子程序。例如,下面的语句定义了一个子过程:

sub my_proc {

print "this is my subrouting\n";

}

sub是子过程的说明,my_proc是过程名字,要调用这个过程,只要使用&符号,例如:

if($want==1){

&my_proc;

}




11.3.2 perl的使用

上面介绍了perl的基本语法。要使用perl,你当然可以在交互模式下使用,但是,一
般情况下,我们用perl是代替shell脚本完成自动化任务的。为此,我们需要把perl写成
程序来运行。写perl程序可以用任何文本编辑工具创建,一般总是设置其扩展名为.pl,
虽然实际上扩展名是不需要的。

要运行一个perl程序,有两种方法,一种是调用perl解释器读入perl源程序,例如,
我们已经写了一个perl程序,命名为test.pl,那么,可以用下面的命令执行它:

$ /usr/bin/perl test.pl

不过,在一般情况下,我们更喜欢在命令行下面直接敲入程序的名字执行它,为此,
可以在perl程序的头部加入这样的行:

#!/usr/bin/perl

注意#!符号,它告诉shell应该用什么程序来解释当前脚本,这里的定义是/usr/bin/
perl,这是缺省的的perl安装位置。如果你的perl可执行程序放在别的目录下,自己修
改这一行。这样,当shell读到这个文件的时候,就会自动启动perl来解释这个程序。

perl最常用的功能是用来写cgi程序,不过,我们不想涉及cgi程序的细节。相反,我
们介绍如何用perl进行日常管理,用perl程序代替书写晦涩的shell脚本。

让我们从一个我自己的例子开始,我经常要把一些txt文本直接转化成html文本,以便
做进一步的编辑和主页发布,其实这个操作非常简单,就是将回车和大于号,小于号都
换成对应的HTML标记,这可以用word或者netscape来完成,但是如果涉及的文件数目比
较多或者文件比较大,用这些软件就很困难了,所以我写了一个十分简单的perl脚本:

#!/usr/bin/perl

if((@ARGV[0] eq "")||(@ARGV[1] eq "")) {

print "convert inputfile outputfile\n";

exit;}

open (FILE1,"@ARGV[0]")||die"can not open source file!\n";

open (FILE2,">@ARGV[1]")||die "can not open taget file!\n";


while($line=<FILE1>){


$line=~s/</&lt/g;

$line=~s/>/&gt/g;

$line=~s/\^M//g;

$line=~s/\n/<BR>/g;

print FILE2 $line;


}

close (FILE1);

close (FILE2);

这个程序简单得几乎象是DOS的批命令,首先检查参数是否定义,然后从输入文件中读
取行,把回车和两个尖括号换成对应的html标记,写入对应文件,任务就完成了。要使
用它,比如把test.txt转换成test.html,可以直接执行:

./convert.pl test.txt test.html

perl也可以完成更复杂的操作,最常见的功能扩展是进行网络编程,如直接使用电子
邮件,电子新闻服务等等。不过我们这里不能进一步介绍它的强大功能。对perl感兴趣
的朋友,可以进一步查阅有关书籍学习编程技术。

11.4 其他工具

还有很多自动化脚本工具,例如python等等。另外,对于一个职业的系统管理人员,
熟悉C语言的编程是很有好处的。

就我个人而言,我感到,如果你不想学习shell,那么了解expect和perl就足以对付一
般的系统管理工作。另外,还有一些威力强大的可编程工具,一个是我们刚才提到的py
thon,这是一种面向对象的脚本平台,如果你的大部分任务使用shell和expect,那么这
个东西可能很适合你使用。

另外一个非常有争议的产品是emacs。这个编辑工具是GNU计划的头号产物,随着发展
,它已经从原来一个文本编辑程序发展成为一个使用lisp宏控制,几乎可以做文本界面
下的一切事情的集成环境。反对它的理由主要是它的运行速度在低配置的机器上几乎无
法忍受,而且配置起来也十分困难。不论如何,使用emacs有时显得比较有专业特色,至
少是很有GNU的特色

--
当我越过无尽虚空的时候,我看见星辰的欲望,光荣和毁灭,这是光辉世界的宿命,
一切的一切,最终必将落入黑暗和虚无。
所以,我随着星光飞翔,去逃脱必然的终结,也许有一天,我将回到世界的原初,
等待新的星辰的诞生。
尘埃是星的起源,星的终结。


※ 来源:・BBS 水木清华站 smth.org・[FROM: 202.112.90.20]

20090711

◇ Linux DOS2Linux mini-HOWTO (part(转寄)

发信人: mglow.bbs@bbs.ee.ntu.edu.tw (失落的恶魔), 看板: Linux
标 题: [转贴] Linux DOS2Linux mini-HOWTO (part 1/1) 中文□发信站: 台大电机 Maxwell 站 (Tue Feb 11 16:50:07 1997)
转信站: sobee!netnews.ntu!Maxwell

- From DOS to Linux - Quick!

By Guido Gonzato <Guido@ibogfs.cineca.it>
April 26, 1996
Version 1.1

中译 : mglow.bbs@bbs.ee.ntu.edu.tw
文中 <ps.> 的部份为译者补述, 并非为原文所译. 再者, 原文发表日期距今也将近
一年, 有部份的说明与今日现况或有些微出入..但不足以影响其立论的正确性 :)


前言


这个 mini-HOWTO 是为(将成为曾经是??) DOS 的使用者而写的. 尤其是那些已经饱受
DOS 凌虐而准备投向 Linux , 这个在 i386 上 Un*x clone OS 的朋友.

当我在我的 PC 上安装 Linux 後, 我很希望能够迅速的了解并且利用它. 我相信这也
是每个人的期望. 你可能已经知道 DOS 可以视为 Un*x 的一角. 身为一位 "资深" DOS
使用者, 我可以告诉你它们有许多的相似之概念: 档案系统, 目录, 环境变数, 程式的
执行, .BAT 档案..等等. 这篇文章的目的在於让已经用过 DOS 的使用者能够将他们在
DOS 上的认识转换到 Linux 上. 你尽可以在床上读个几页, 当然, 之後再去 PC 上
操作或许会更好.

在此, 我仍然要强调, 这篇文章 *不是* Linux 的入门课程 -- 对初学者而言, 我建议
你能够有一份 Matt Welsh 所写的 "Linux Installation and Getting Started",
这篇文章在 sunsite.unc.edu 或是其 mirrors, 放在 /pub/Linux/docs/LDP 下; 写得
很不错而且易懂. 另外 HOWTO 和 FAQ 也是不错的选择. 而这篇文章并不是以上作品的
同类型文章, 只是一定有一些家伙 (我就是其一) 在只看了二十页不到 (而非全部的
200 页以上), 就迫不及待的想要进入 Linux 的世界.


第零节: 介绍

0.1: Linux 就是你要的吗??

这个世界上并不存在著"最好的电脑" 或是 "最棒的 OS"(作业系统): 这完全视你的
需求而定. 比方说, 我很不喜欢 Mac (得罪了 :) : 它的确很容易使用, 但我还是喜欢
PC 帮我完成某些事. (试试在 Mac 上做和 dir/s/w >lst 一样的事) 至今, 对许多人
而言 Mac 只是 "最好" 的电脑. 同样的, 我也不认为 Linux 是最好的 OS : 显然,
Linux 优於 Win95, 但是很多人还是喜欢後者(Win95).

Un*x 的主要诉求对象是以程式设计师和科学家, 而 Mac & Win 是针对著一般性的使用者
而设计的. 因此, Linux 足以提供一个广阔的天空 -- 如果你需要的是 Compilers, 网路,
以及学术实验性质的软体, 而你也喜欢三不五时的修理你的 Linux : 换句话说, 你喜欢
hacking. 另一方面, 假使你非得要求有帮你完成所有事的对话框, 需要使用商用套装程
式, 或是说, 你对命令列的提示号有失落感的话 ... 那你还是早点死心吧...:p

接下来就看你了. 但在此之前先提醒一件事: Linux 和 DOS/Windows 是可以在同一台
电脑上并存的.


0.2: 这就是我要的!! 请告诉我更多一些!!

现在, 假设你已经装好了 Linux. 依照安装步骤, 你也应该有个帐号(account) (如果
不是这样, 请参考 6.1 节). 现在你的 Linux 已经在运作当中了. 或许有人已经告诉
你, 所有你需要的软体都已经在系统里了, 而你该做的就是 login. 这时你键入了 login
name, password, 然後两眼对著萤幕发直 -- "好啦, 但再来呢??"

别失望.. 你尽可以完成从前你在 DOS 下的工作, 甚至更多. 设想一下, 你现在面对
的不是 Linux, 而是 DOS. 这时, 你应该会作下列的工作:

1. 执行程式; 建立, 复制, 编修档案或是改变档名 -- 维护档案;
2. 建立, 删除, 或是在目录间跳动 -- 维护目录;
3. 格式化磁片然後备份档案 -- 磁碟处理;
4. 修修补补 autoexec.bat 和 config.sys 这两个档 -- 系统维护;
5. 写自己的程式, 可能是 .bat 或是 qbasic -- 程序处理;
6. ..其它还有吗..?? :)

你一定很高兴知道, 上述的事在 Linux 下一样可以完成, 而且方法和 DOS 极为相似.
在 DOS 下, 一般使用者使用的命令只是系统提供的许多命令中的少数几个 -- Linux
也一样. 在此, 如果你并未熟悉完成上述六点的相关 DOS 命令, 那请你先暂停.. 先
去翻翻 DOS 使用手册吧. 在往後的讨论里, 本文将不会对诸如什麽是目录或是 PATH
是什麽来做解释.

在继续讨论前的一些注意事项:

1) 和 DOS 不同, Linux 具有一些内建的系统安全机制. 在你打开 PC 电源然後启动
Linux 之後, 你必需 "log in", 这是为了使系统知道你即将使用系统并工作; 这是因
为 Un*x 是一个多人使用的 OS. 此外, 一般的使用者并非拥有这台电脑(的一切资源);
只有使用 root 来做 login 的使用者才是真正的拥有者(这个人就是所谓的系统管理者,
在你在己的 PC 上, 你就是 root); 每个档案及目录都有存取权限的定义, 这使得有些
档案无法被一般使用者存取. 另一方面, DOS, 却无法提供这些保护 -- 你可以存取整颗
的硬碟.

2) 一旦你结束工作要关机, 请 *不要* 马上切断 PC 的电源 ! 相反的, 这必须经过一
些正当的程序 (按下 ctrl-alt-del , 等到系统做完例行的检查之後, 告诉你 OK, 才可
以关掉电源). 从另一个角度来说, Linux 是一个很稳定的系统, 你完全不必担心一旦有
某个错误的应用程式导致系统挂掉, 逼迫你必需按下 reset 钮 :p

3) 我们非常鼓励你多尝试与实验 : 请放心, 这样不会对系统造成伤害 (ps. 前提是你
并不是用 root 的身份来尝试). 你可以在提示号後面打以下指令以得到帮助 ($ 是标准
提示号, 而 # 是 root 专用的) :
$ help
(相当合乎逻辑), 或是想得到更多讯息:
$ man <command>
在这里, 如果你已经安装了 man pages, 那麽这样就会将与 <command> 相关的 manual
(man 的原字) page 印出来. 你也可以试试这样:
$ apropos <command>

$ whatis <command>

4) 通用规则 (help, man ..etc)

a) <...> 表示必须给定的参数, [...] 则可有可无. 例如 :
$ tar -tf <file.tar> [> redir_file]
file.tar 一定要指定, 但是 redir (重新导向输出档) 是可以不用加的.
b) 以下 "RMP" 表示 "Read the Man Pages for further information" -- "请看 man
page".

你现在已经可以继续阅读下面的部份了.


第一节: 档案维护

1.1. 档案: 前言

Linux 拥有一个档案系统 - 意思是 "一个有很多档案和目录的结构" - 和 DOS 非常
相似. 档案都有一个名称(档名, 它的组成必须要遵循一些规则), 被置放在目录当中.
有些是可执行的程式, 它们必须要一些额外的参数才可以正确执行. 另外, 你也可以使用
"万用字元" (wild card) , 输出/入导向, 管道(piping). 但, 和 DOS 仍有些许的不同.

- - 第一: 在 DOS 下, 档名必须遵守 8.3 格式的规则. 比方说 NOTENOUG.TXT. 在
Linux 里我们有比较自由的空间. 假如你的 Linux 已经安装在 ext2 或是 umsdos 等等的
档案系统上, 你就可以使用长档名来为档案取名, 这样可以使档名中包含更多的字元和
"点". 举个例子, This_is.a.VERY_long.filename. 请注意到我用了大写和小写的字母,
而事实上...

- - 第二: 大小写是不一样的. 因此, FILENAME.tar.gz 和 filename.tar.gz 是两个不
同的档案. 这个规则在命令来说也是成立的: 当使用 ls 这个指令 (和 DOS 的 DIR 类似)
会得到目录下案的列表, 但是用 LS 的话, 系统只会丢给你一个错误的讯息.

- - 第三: 用 "." 开始的档案名称会被视作隐藏档. 例如: .I.am.a.hidden.file ,
使用 ls 时就看不到了 (ps. 用 ls -a 可以看见隐藏档)

- - 第四: Linux 并没有规定可执行档一定要有 .COM , .EXE 或 .BAT 的延伸档名. 如
果我们用 ls -F 这个命令, 就会发现执行档後面会被加上一个 '*'. 例如:
$ ls -F
letter_to_Joe Cindy.jpg cjpg* I_am_a_dir/ my_1st_script*
cjpg* , my_1st_script* 是可以被执行的. 在 DOS 中, .BAK 表示备份档案, 而 Linux
则在档名尾加上一个 '~'.

- - 第五: DOS 程式需要的参数要用 /switch 的方法给予, 而 Linux 却是用 -switch.
就像 dir /s 与 ls -R. 附注一点, DOS 下的某些程式如 PKZIP 和 ARJ, 是用和 Un*x
一样的参数给定方式.


1.2. 档案: 对应 DOS 与 Linux 相关的指令


左边是 DOS 的命令; 右边是 Linux 里具用同样功能的指令.
COPY: cp
DEL: rm
REN: mv
TYPE: more, less, cat

Redirection and plumbing operators: < > >> |
Wildcards: * ?
nul: /dev/null
prn, lst: /dev/lp0; lpr

- - EXAMPLES -

DOS Linux

C:\GUIDO>copy joe.txt joe.doc $ cp joe.txt joe.doc
C:\GUIDO>copy *.* total $ cat * > total
C:\GUIDO>copy fractals.doc prn $ cat fractals.doc | lpr
C:\GUIDO>del temp $ rm temp
C:\GUIDO>del *.bak $ rm *~
C:\GUIDO>ren paper.txt paper.asc $ mv paper.txt paper.asc
C:\GUIDO>type letter.txt $ more letter.txt
C:\GUIDO>type letter.txt $ less letter.txt
C:\GUIDO>type letter.txt > nul $ cat letter.txt >/dev/null
n/a $ more *.txt *.asc
n/a $ cat section*.txt

附注:

1) * 在 Linux 中变得聪明多了:
- - * 表示除了隐藏档外的所有档案; .* 表示所有隐藏档; *.* 就是"某些字元" +
一个点 + "某些字元" 的档案名称;
- - p*r 可以满足 peter 和 piper 两个字; picked 和 peck 也都可以表为 *c* ;
2) 使用 more 这个命令时, 按下<space> 可以继续阅读下一页, 'q' 或 ctrl-c 结束.
less 和 more 类似, 但可用方向键.
3) Linux 里 *没有* UNDELETE 这种命令. 所以, 砍掉档案前要三思啊..
4) 除了 DOS 中的 < > >> 之外, Linux 可以使用 2> 来对错误讯息导向. (stderr)
5) Linux 有另外一组万用字元: []. 使用:
- - [abc]* 表示以 a, b, 或是 c 开头的档案;
- - [I-N]* 表示由 I, J, K, L, M, 或 N 开头的档案;
6) 没有像 DOS 一样的 rename 可用; 也就是说, ren *.xxx *.yyy 是无效的.

1.3. 执行程式: 多工与 Session

要执行一个程式, 就像 DOS 一样键入它的名字就可以了. 如果说这个程式被放在 PATH
(第四节) 指定的目录 (第二节), 那麽该程式就会被启动. 例外的是, Linux 不像 DOS
可以执行放在现行目录下的程式 (.) , 除非 . 已经加入 PATH 中了. 若要执行现行
目录下的程式, 可以这样做: ./<prog>.

这是一个标准命令的格式:
$ command -s1 -s2 ... -sn par1 par2 ... parn <input >output
-s1 , -s2 ... -sn 是命令选项, parn 是程式参数. 我们可以在同一行上键入数个命令:
$ command1 ; command2 ; ... ; commandn
这就是执行命令的方法, 但是我们可以更进一步. 一个使我们使用 Linux 的主要原因
是, 它是一个多工的系统 -- 它可以同时执行数个程式 (以下, '程式'用'行程'代替)
你可以将一个原本在背景执行的行程提升到前景并继续执行. 而且, Linux 可以使你有
多个 session : 这就像同时有好几台电脑在工作!

- - 切换 session 1..8:
$ <ALT-F1> ... <ALT-F8>.
- - 开始一个新的 session , 但不离开原本 session :
$ su - <loginname>
例如:
$ su - root
这相当有用. 比如, 当你要 mount 一个磁碟(机) 时. 因为只有 root可以作这件事.
- - 结束一个 session:
$ exit
- - 在前景执行一个行程:
$ progname [-switches] [parameters] [<input] [>output]
- - 在背景启动一个行程(在背景执行) 要加上 '&' 这个符号:
$ progname [-switches] [parameters] [<input] [>output] &
- - 查看有多少行程正在执行:
$ ps
这样会列出一个表格. 每一个行程都会有一个 PID 来识别 (一个数字).
- - 杀掉一个行程:
$ kill <PID>
有时後你必需用这个方法来结束一些行程 (当你不知道怎样正确结束时 :) 一个行程或许
要用以下方法才可以结束:
$ kill -15 <PID>
$ kill -9 <PID>
此外, shell (像 COMMAND.COM 相等的东西) 允许你暂停某个行程的执行, 或是把前景
换到背景执行, 背景换来前景. 我们用'工作'来称呼行程.
- - 查看有多少工做:
$ jobs
这样列出来的工作是以一个 shell 分配的数字来区别的, 而不是用 PID.
- - 暂停一个前景的行程:
$ <CTRL-C>
(但不总是有效 :p )
- - 暂停前景的程式:
$ <CTRL-Z>
(ditto)
- - 将一个被暂停的程式移到背景继续执行:
$ bg <job>
- - 把一个背景执行中的程式提升到前景:
$ fg <job>
- - 再一次的, 要砍掉一个行程:
$ kill %<job>
这里的 <job> 可能是 1, 2, 3, ... 有了这些命令你就可以在同一个时间格式化磁碟,
压缩档案, 编译一个程式, 还可以解压缩, 而且, 你仍然还是看到提示号和游标在等
待你的命令. 不妨在 DOS 下试试看这麽做吧!! 在 Windows 下也试试, 比较一下它们
效率的不同, 然後你就会知道你的选择是否正确了.


第二节: 使用目录

2.1. 目录: 前言

我们已经大略看过 DOS 和 Linux 在档案处理方面的不同. 在目录方面, DOS 用 \
表示根目录, Linux 则是 /. 同样的, 巢状表示的目录, DOS也用 \ 来分隔每一层次
目录, Linux 用 /. 比方说以下路径:
DOS: \PROGRAMS\C++\SOURCES\HELLO.CPP
Linux: /home/guido/papers/geology/mid_eocene.tex

相同的是, .. 表示上一层目录, . 表示现行目录. 记得一件事, 系统并不允许你在任何
地方 cd , rd, 或是 md. 每一个 user 都有一个自己的目录称作是 home, (这是系统管
理者分配的) 例如在我自己的 PC 上, 我的 home 是 /home/guido.

2.2. 对应 DOS 和 Linux 中的相关指令

DIR: ls, find, du
CD: cd, pwd
MD: mkdir
RD: rmdir

- - EXAMPLES -

DOS Linux

C:\GUIDO>dir $ ls
C:\GUIDO>dir file.txt $ ls file.txt
C:\GUIDO>dir *.h *.c $ ls *.h *.c
C:\GUIDO>dir/p $ ls | more
C:\GUIDO>dir \*.tmp /s $ find / -name "*.tmp"
C:\GUIDO>cd $ pwd
n/a - see note $ cd
n/a - see note $ cd ~
n/a - see note $ cd ~/temp
C:\GUIDO>cd \other $ cd /other
C:\GUIDO>cd ..\temp\trash $ cd ../temp/trash
C:\GUIDO>md newprogs $ mkdir newprogs
C:\GUIDO>md \progs\turbo $ mkdir /progs/turbo
C:\GUIDO>rd newprogs $ rmdir newprogs
C:\GUIDO>rd \progs\turbo $ rmdir /progs/turbo

附注: ~ 是 home 的一个缩写. cd ~ 可以使你瞬间回到自己的 home, 不论你在哪里.
cd ~/tmp 会带你到 /home/your_home/tmp. 这样, 可以了解吗??

第三节: 软碟, 硬碟, 和类似装置

3.1. 使用软式磁碟

或许你从未想过, 但 DOS 的 FORMAT A: 作了比它看起来更多的事. 事实上, 当你键入
FORMAT 这个命令时, 它会:

1) 实际的去格式化磁碟;
2) 建立 A:\ 这个目录 (= 建立一个档案系统);
3) 使这个磁碟能够被使用者使用 (= mount 一个磁碟机).

这三个步骤在 Linux 中是分开的. 我们可以在 Linux 中建立及使用 MS-DOS 格式的磁碟,
但是当然还有其它的格式可用. 最常用的要算是 ext2. 以下是准备一份可用的磁碟之方法
(请先 su 为 root):

- - 格式化一张 1,44 Meg 的软碟 (A:)
# fdformat /dev/fd0H1440

- - 建立一个档案系统:
# mkfs -t ext2 -c /dev/fd0H1440

# mformat a:
建立一个 MS-DOS 的档案系统.

在使用这张磁片前, 你需要先将这个磁片连同磁碟机 mount.
- - mount 一个磁碟机:
# mount -t ext2 /dev/fd0 /mnt
or
# mount -t msdos /dev/fd0 /mnt

现在你已经可以使用这张磁片了. 当你结束工作, 要拿出磁片前, 请你 *一定* 要
umount. (重要!)
# umount /mnt
好, 你可以把磁片拿出来了. 很明显的, fdformat 和 mkfs 只要对未格式化的磁片进
行就可以了, 已作过的就不需要了. 如果要使用 B 磁碟机, 请参考前述的□例, 用
fd1H1440 和 fd1 替换 fd0H1440 和 fd0.

现在你从前对 A: , B: 的工作现在都移转到 /mnt 下了. 例如:

DOS Linux

C:\GUIDO>dir a: $ ls /mnt
C:\GUIDO>copy a:*.* \docs\temp $ cp /mnt/* /docs/temp
C:\GUIDO>copy *.zip a:\zip $ cp *.zip /mnt/zip
C:\GUIDO>a: $ cd /mnt
A:\> /mnt$

不消说, 对软碟成立的方法, 对其它的 device 一样成立. 比方, 你可以 mount 另一
个硬碟或是光碟机. 稍微看一下 /dev 下的 device. 下面是 mount 一个光碟机的□例:
# mount -t iso9660 /dev/cdrom /cdrom


第四节: 量身订作一个 OS

4.1. 系统初始化相关档案

在 DOS 中有两个很重要的档案 -- AUTOEXEC.BAT 和 CONFIG.SYS. 当启动 DOS 时,
这两个档案负责一些系统的设定工作 , 诸如设定环境变数 PATH, FILES 等等, 甚至
呼叫其它必需在开机时载入的程式.

在 Linux 中也有一些类似工作的档案, 但其中有些却是相当的危险; 再你真正了解
你将要做的事之前, 请千万别去碰它. 无论如何, 我还是得告诉你那些需要特别谨慎
的档案:

FILES NOTES

/etc/inittab don't touch for now!
/etc/rc.d/* ditto

如果你想做的是设定 PATH 或是其它的环境变数, 抑或修改 login 时的讯息, 呼叫一个
程式, 以下大概就是你要的:

FILES NOTES

/etc/issue sets pre-login message 设定 login 前的讯息
/etc/motd sets post-login message 设定 login 後的讯习
/etc/profile sets PATH and other variables, etc. 设定环境变数
/home/your_home/.profile does whatever you want 这里可以随意加入你想要的

如果最後那一个档案(~/.profile) 存在, (注意到它是隐藏档), 那麽在你 login 之後
它的内含命令就会被执行.

□例 - 请看这个 .profile:

# I am a comment
echo Environment:
printenv | more # equivalent of command SET under DOS
alias d='ls -l' # easy to understand what an alias is
alias up='cd ..'
echo "I remind you that the path is "$PATH
echo "Have a good day, "$LOGNAME

PATH 和 LOGNAME, 正如你所预料的, 是两个环境变数.

4.2. 程式初始化设定档

在 Linux 下, 一般来说所有的事都可以依照你的要求而作安排. 大多数的程式都有
它们自己的初始化设定档, 这些都可以由你来修改. 通常, 它们都以 .prognamerc 的
型式存在於 home 目录里. 在这里, 首先你可能需要接触的设定档有:

..xinitrc: 用来初始化 X Window System.
..fvwmrc: 初始化 fvwm 这个 window manager. 你可以在
/usr/lib/X11/fvwm/system.fvwmrc 找到□例.
..xfm/*: xfm (一个 file manager) 的设定档.
..Xdefault: 由 rxvt -- 一个终端机模拟程式 -- 所使用.

对於这些设定档, 或是你将来会遇到的其它档案, 请查 Man page.


第五节: 关於程式设计

5.1. 编写叙述程式 (Scripts): .BAT 档的聚合体

你或许使用过 .BAT 来当做一长串命令的缩写 (我常做这种事). 这件事可以藉由在
profile 或 .profile 中设定 alias 来达成. 但是, 一旦 .BAT 档太过复杂, 那麽你
会喜欢由 shell 提供的叙述语言 (Scripting language) : 它几乎和 Qbasic 一样强
大而且易用. 它可以使用变数, 可以拥有像是 while , for, case if.. then .. else,
的语法结构; 它还有其它的优点 -- 例如, 它可以当成是程式语言的替代品.

要撰写一个 script - 就像在 DOS 下写 .BAT 档一样 - 只要编写一个 ASCII 的档案,
内含你想要的指令, 然後储存, 再使用下面这个命令让它可以被执行:
$ chgmod u+x <scriptfile>
当要执行时, 只要键入它的档名就可以了.

这里有个小小的警告: 系统内定的编辑器叫作 vi, 它有一点难以使用, 我想你应该也
会这样认为它有些烦人. 我不会在这篇文章中讨论它 -- 我自己也还尚未找到使用的窍
门 :p 请参考 Matt Welsh 的 "Linux installation... ", 109 页. 但你也可以使用其
它的编辑器, 如 joe 或是 X 下的 emacs. 这里只稍为说明一点你必需知道的 vi 知识
(至少让你可以 quit :p )

- - 加入一段文字可以用在文章中按下 'i';
- - 离开 vi 但不储存文章 : 请按 ESC 再打 :q!
- - 离开且存存 : 按 ESC, 再打 :wq

在 Linux 中编写 shell script 是一门大学问 -- 它几乎要一本书才能讲得详细.
本文不会再更深入的讨论这个问题; 但是, 以下提供了几个有用(希望是)的□例, 希望
能够使你对 shell script 有个初步的了解.

EXAMPLE 1: first_script

#!/bin/sh
# I am a comment
# don't change the first line - it's got to be there
echo "Today is `date`"
echo "My name is "$0
echo "You gave me the following "$#" parameters: "$*
echo "First parameter is "$1
echo "Have you grasped the trick?"

EXAMPLE 2: 2exe

#!/bin/sh
echo "making "$1" executable... "
chmod u+x $1

EXAMPLE 3: backup

#!/bin/sh
echo "Copying files in ~/bak... "
for name in $*
do
cp ${name} ~/bak
done

EXAMPLE 4: fmta

#!/bin/sh
echo "I remind you that only root can format disks"
fdformat /dev/fd0H1440
mkfs -t ext2 -c /dev/fd0H1440
echo "disk formatted."

EXAMPLE 5: mnta

#!/bin/sh
echo "I remind you that only root can mount disks"
mount -t msdos /dev/fd0 /mnt
echo "don't forget to umount when you've done."

5.2. 自己写个 C 程式

你应该不会期待在 Linux 有 GW/Qbasic 吧?? 在 Un*x 中, 系统内定的语言是 C,
不管你是喜欢还是讨厌它. 当然还有其它的语言可以用 (FORTRAN, Pascal, Lisp, Bacic
.. 但没有 Turbo Pascal ^Q^ ).

假设你懂得 C.. 如果你曾经被 Turbo C++ 或是它在 DOS 下的兄弟们宠爱过, 那这里
有两句话恐怕不啻晴天霹雳: Linux 下的 C 编译器叫作 gcc, 但, 它没有 IDE 环境,
没有线上求助系统, 没有整合式除错器,..等等. 只有一个命令列的编译器, 但强大
且有效率. 以下这个命令可以编译你已写好的 hello.c :
$ gcc hello.c
这样会产生一个 a.out 的执行档. 如果你想让 gcc 造出其它档名的执行档, 键入
$ gcc -o hola hello.c
要联结一个程式库, 你要在 gcc 後加上一个 -l<arg> 的选项. 比如说要联结 math
library
$ gcc -o mathprog mathprog.c -lm
( -l<arg> 会迫使 gcc 联结 /usr/lib/lib<arg>.a; 因此, -lm 会联结
/usr/lib/libm.a)

对小程式来说, 这是一个好方法. 但是若程式是由数个 source files 组成, 我们可能
会需要 make 这个程式. 假设我们已经写好一个语法分析程式 parser.c, 它 #include
了两个 .h : parser.h , xy.h. 现在, 有个 calc.c 需要使用到 parser.c 中的功能.
这样该怎麽办??

我们可以写一个叫作 makefile 的档案, 告诉编译器所有 source 和 object files
间的关系, 在这个例子里,

# This is makefile, used to compile calc.c
# Press the <TAB> key at appropriate positions!

calc: calc.o parser.o
<TAB>gcc -o calc calc.o parser.o -lm
# calc depends on two object files: calc.o and parser.o

calc.o: calc.c parser.h
<TAB>gcc -c calc.c
# calc.o depends on two source files

parser.o: parser.c parser.h xy.h
<TAB>gcc -c parser.c
# parser.o depends on three source files

# end of makefile.

储存, 然後键入
$ make
以便编译程式. 或者, 这个档案被存在 calc.mak 里, 那麽就必需
$ make -f calc.mak
当然, 请参阅 Man pages 以得到更多的资讯.

此外, 某些函数的用法在 man pages 可以找得到, 例如
$ man printf


第六节: 剩下的 1%

6.1. 使自己暂时脱离 root 的身份

用 root 来 login , 并且处理每天的工作实在不是一件好事情. 因为这个帐号相当
危险, 应该仅供系统维护时使用. 下面这个命令可以为你自己建立一个一般性的帐号.
用 root login, 再键入
# adduser
然後输入系统询问的资料. (如果有你看不懂的问题, 请按下 <RET>. 这样系统将会
使用预设的资料.)

6.2. 建立虚拟记忆体

虽然 Linux 理论上只要 2M 就可以执行, 但是愈多的记忆体会使工作更顺利. X Window
System 会要求至少 8M RAM , 否则它就罢工. 要使用虚拟记忆体来增加可用记忆空间,
请以 root 身份执行:
# dd if=/dev/zero of=/swapfile bs=1024 count=8192
# mkswap /swapfile 8192
# sync
# swapon /swapfile
接著把最後一行加入 /etc/rc.d/rc.local 中, 使每次开机时就会自动开启 swap.

6.3. 使用 tar & gzip

在 Un*x 中有著几个被广泛使用於保存或压缩档案. tar 是一个保存档案的工具. 它有
点类似 PKZIP 但并不会作压缩的工作 -- 它只是将许多档案包装成一个而已:
$ tar -cvf <archive_name.tar> <file> [file...]
把 tar file 解开, 要用以下的命令:
$ tar -xpvf <archive_name.tar> [file...]
如果欲检视 tar file 中的档案列表,
$ tar -tf <archive_name.tar> | less
用 compress 或是 gzip 可以压缩档案. 压缩後档案就无法再使用除非解开来:
$ compress <file>
or:
$ gzip <file>
这样建立出来的压缩档会以 .Z (compress) 或 .gz (gzip) 作为延伸档名. 这些档案
无法再被 compress 或 gzip 再压缩. 想将档案解压缩的话, 请用
$ compress -d <file>
or
$ gzip -d <file>

请参阅 Man pages.

其它像是 arj ,zip 或是 unzip (PK??ZIP 相容) 的工具在 Linux 下一样找得到.
在 Un*x 的世界里, .tar.gz 或 .tgz 的档案就像 DOS 下的 .ZIP 一般普遍.
以下这个命令可以浏览 .tar.gz 档案中的档案列表:
$ gzip -dc <file.tar.gz> | tar tf - | less


6.4. 安装应用程式

大多数的 Linux 软体都以 .tar.gz 的方式包装; 有些软体就可以在 / 下用这个命令
完成安装:
# gzip -dc <file.tar.gz> | tar xvf -
档案会在正确的目录中解压缩 (目录亦会自动建立好). 看来不错, 不是吗?? :)
Slackware distribution 的使用者(其实其它的 distribution 也不差, 像是 redhat)
就有一个满有亲和力的安装程式 pkgtool.

其它的软体可能有它们自己的安装方试; 你可以找到说明的文件. 此外, 有些软体
是以 C 或 C++ source code 的方式来制成, 并未编成执行档. 这时就必须自行编译.
绝大多数的情形你只要打
# make

很明显的, 你会需要 gcc -- 这个应该在所有的 distributions 中都有. 但是记住:
请用 root 身份完成以上这些软体安装的工作.

6.5. 你不能不知道的小技巧

让系统帮你键入命令: 按下 <TAB> 可以让系统自动补齐命令中未打完的字. 例如:
gcc this_is_a_long_name.c ; 只要键入 gcc thi<TAB> 就够了. (当有数个使用以上
相同字元开始的档案时, 你必需给与一定数量的字元使系统可以判定到底你想要的是哪
一个).

回卷: 按 SHIFT + PAG UP 可以使萤幕回卷数页, 这视你拥有的 Video RAM 多寡而定.
更进一步的说, 当你要使用下面这个命令
$ script <script_file>
这样会使得在萤幕上出现的任何东西都被复制到 script_file 中, 直到你使用 exit
这个命令为止. 这样就可以在稍後再来检视刚才的内容.

重设萤幕: 如果你不慎 more 或是 cat 一个像可执行档的档案, 我想你的萤幕应该会
充满了奇怪的符号. 用 reset 就可以解决这个问题:
$ reset
或是依序键入 CTRL-V ESC c RETURN.

来自 kernel 的讯息: 看一下 /var/adm/messages (以 root 身份), 里面包含了
kernel 要告诉你的资讯, 其中有启动时出现的讯息.

6.6. 一些有用的程式

最重要的一件事就是, 要怎样找到这些软体. 我想大家都已经知道如何在网路中驰骋,
以及使用 archie 和 ftp. 这里列出三个最重要的 Linux 相关 ftp site:
sunsite.unc.edu, TSX-11.mit.edu, nic.funet.fi . 请使用距离你比较近的 mirror.
(ps. 国内的 nctuccca.nctu.edu.tw, linux.cis.nctu.edu.tw, ftp.ncu.edu.tw..etc)

at 可以让你在特定的日期及时间执行程式

delete-undelete 就像它们的名字一样 :)

df 会告诉你有关你的硬碟的资讯;

dosemu 能执行一些 DOS 的程式 (但非全部..ps. 我想应该是大多数了..) - 包含
Windows 3.x , 但需作些调整;

file <filename> 告诉你 <filename> 是哪一种档案 (ASCII 文字档, 可执行档, 包装後
的档案 ..等等);

find (参看 2.2 节) 是最强且有用的工具之一. 它能够找寻档案系统中, 所有符合
搜寻条件的档案, 并且对它作处理. 最常见的使用方法是:
$ find <directory> <expression>
这里的 <expression> 包含了搜寻的标准和对目标档案的处理动作. 例如:
$ find . -type l -exec ls -l {} \;
会找到所有的 symbolic links 并且显示它们联结的目的地.
$ find / -name "*.old" -ok rm {} \;
则找寻所有符合 *.old 型式的档案, 然後询问你是否要删除它.
$ find . -perm 755
搜寻所有存取权限为 755 的档案 (即可执行档)
$ find . -user root
会找到拥有者为 root 的档案. 其它还有很多的用法, 当然, RMP :p

grep 可以在档案中找寻一个字串. 比如说
$ grep -l "geology" *.tex
将列出所有包含 "geology" 这个字的 tex 档. 其馀请 RMP;

gzexe 将可执行档压缩, 但仍可执行;

joe 是一个很棒的编辑器. 用 jstar 可以启动它 -- 你会得到一个看起来像, 用起来
也像 WordStar 或是它的兄弟姊妹如 DOS editor 的编辑器;

lpr <file> 可以在背景列印档案. 用 lpq 可查看在列印序列中排队的情形;

mc -- 一个很好用的 file manager;

pine , 一个不错的 e-mail 程式;

sudo 使一般使用者做某些 root 才能做的事 (像是 format 或是 mount .. RMP :p )

uname -a 会告诉你系统目前的状况;

zcat 和 zless 可以帮助你阅读被 gzip 压缩的文字档 - 却不用先将它解压.
$ zless textfile.gz
$ zcat textfile.gz | lpr

6.6. 常用的档案, 与它们的延伸名称, 相关程式

在 Linux 里你会遇到许多型式的档案 -- 它们都有不同的延伸名称. 以下是一个大
略的列表:

..1 ... .8: Manual Pages, 由 man 来读取.

..arj: arj 产生的压缩档.

..dvi: TeX (後述) 产生的输出档. 用 xdvi 就可以阅读; dvips 可以用来将 .dvi
转换成 .ps 的 postscript 档.

..gif: 图型档. 用 seejpeg 或是 xpaint.

..gz: gzip 的压缩档.

..info: info 档 ( man page 的一种型式). 由 info 来读取.

..jpg,.jpeg: 图型档. 使用 seejpeg.

..ps: postscript file. 用 gs , 或是 ghostview 来阅读. (great !!)

..tgz,.tar.gz: 由 tar 包装, 再由 gzip 压缩的档案.

..tex: 经过 TeX 处理的文字档, TeX 是一个强大的排版程式. 许多的 distributions
都附有 tex.

..texi: texinfo 档. (和 .info 类似). texinfo 使用之.

..xbm,.xpm,.xwd: 图形档. 用 xpaint.

..zip: 由 zip 压缩包装的档案, 请使用 zip 和 unzip.

..Z: 由 compress 压缩的档案.


剧终 -- 现在...

恭喜你!! 你已经对 Un*x 有了最初步的认识, 而可以准备用 Un*x 来进行更多的工作了.
但仍要提醒你, 这样仍然不够, 你仍然需要多一点 Linux 的使用及尝试的磨□. 不过,
如果你只不过是要使用一些应用程式, 那麽以上的讨论我猜已经足够了..:) 虽然我现在
仍然对 Linux 懂得不多, 但是 Linux 已经可以帮我处理许多每天必需的工作了!

我相信你一定会迷上 Linux , 进而更进一步的学息它 - 每个人都这样的. 我猜,
有了 Linux , 你一定不会想念 DOS ! 虽然我并不敢奢望有太多的读者, 但我仍深切
的希望能够对阅读本文的读者有所帮助 :)

Disclaimer


"From DOS to Linux - Quick!" was written by Guido Gonzato,
<Guido@ibogfs.cineca.it>, April 1996. Many thanks to Matt Welsh, the author
of "Linux Installation and Getting Started", to Ian Jackson, the author of
"Linux frequently asked questions with answers", to Giuseppe Zanetti, the
author of "Linux - Il sistema operativo FREE SOFTWARE per personal computer
386/486", to all the folks who emailed me suggestions, and especially to
Linus Torvalds and GNU who gave us Linux.

This document is provided "as is". I put great effort into writing it as
accurately as I could, but you use the information contained in it at your
own risk. In no event shall I be liable for any damages resulting from the
use of this work.


This document can be freely distributed as long as:

- - it is distributed in its entirety, including this disclaimer and
permission notice;
- - no money is charged for it;
- - it is not modified in any way without my permission.

Translations, prints, re-editings, or extractions from this document must
be approved by me before being distributed. For any requests, suggestions,
flames, etc., feel free to contact me. If you don't have e-mail access, my
snail-mail address is:

Guido Gonzato - Via Monte Ortigara 19/a, 37127 Verona - Italy

Enjoy life!

Guido =8-)

中译感言

看了这麽多前辈们累积的心血, 心中总是有点不安.. 偶然间有这个机会做一点翻译的
工作, 还是万万不足以表答小弟心中对前辈们的感谢..
希望这篇文章能够对您有所帮助. 小弟第一次从事翻译的工作, 谬误在所难免.
关於本文的译文若是您有任何的疑问或是建议, 您可以到 freebsd.ee.ntu.edu.tw
Interpret 板讨论, 或是 e-mail
b84089@cctwin.ee.ntu.edu.tw
mglow.bbs@bbs.ee.ntu.edu.tw 告诉我, 小弟一定会尽力给予满意的答覆..
另外, 本文尽可在 TANet 上流传而不需作者(就是我啦)的同意.. 但如欲移作商业用途,
还是请先知会一下..:)

mglow.bbs@bbs.ee.ntu.edu.tw
on 970211

- --- END Linux DOS2Linux mini-HOWTO part 1/1 ---
--
* Origin: ● 台大电机 Maxwell 站 ● From: REMOTEHOST=moonglow.m3.ntu.edu.tw