星期三, 七月 25, 2007

Patent Application

Some website of the existed patent:

Google->Patent --all the US patent applied

US Patent Office-USPTO

European Patent Office

State Intellectual Property Office of the P.R.C.

 

Patent Apply Process: before submit

使用专利

外观专利

发明专利 -- 最有商业价值

 

1. Get an idea

2. Prove it works and keep the records

    - Prior Art Search- How do others solve the problem? Compare is needed!

    - Lab book  (Important! A Patent submitted has a time span to be protected, if someone shows prove that he invent before your date, then u lose the patent)

3. Make an Invention disclosure

    - A written description of an idea

    - i.e. recognizing a problem/need and providing a solution

4. Submit your invention to your company

     -- http://disclose.**.com

星期一, 七月 23, 2007

孙振耀在北大的演讲 zZ

惠普职业 25年

谈到经验总结就避免不了提一提我这 25年在中国惠普到底做了什么。

如果不谈在学校的时候的半工半读身份,我在惠普公司是服务了 25年时间。惠普公司有一个 "提前退休计划 ",而我自己也一直想去学校里面续学习,于是就申请了这个计划。离开之前,公司送给我一个纪念牌,上面记录了我在惠普公司每一个工作阶段的时间、我负责什么工作、以及我的年龄等信息。事实上我在 20 岁的时候就加入了惠普公司,一直到51 岁退休。

1976 年我加入台湾惠普。还是计算机科学系学生的时候,我在大学二年级的暑假到惠普公司申请工作,一做就是两年。因为大学毕业以后需要服兵役,所以在服役结束之后于1982 年26 岁的时候重新加入惠普公司,从工程师开始做起。

1983 年因为公司的要求,我在" 百般不愿" 的情况下从工程师转为销售。那个时候我刚结婚没有多久,我太太是我高中和初中的同学,她一直说她嫁给我的原因是因为我是工程师,但现实情况却是,我一结婚马上就离开工程师岗位转做销售。我父亲是一个非常传统的公务员,他一辈子就是为政府企业服务,他听说公司要我转销售的时候说万万不可,他认为销售是一个 "骗人 "的工作。但是我现在回想起来,如果没有惠普公司把我从工程师转成销售,可能我很多人生的经验会非常不一样。有人会问,是不是做销售会比较有成就?这个很难讲,但是我相信如果不做销售应该不会出现经过 25 年以后我现在的这样的情形,我觉得这是一个非常精彩的职业生涯。

1988 年开始我第一次担任" 管人" 的工作,也就是开始带领一个团队,这对我来说也是一次很深的体会。我在没有担任这个工作之前,我觉得我就像我公司部门里面的" 大哥" 一样,好像" 地下领袖" ,只要一声吆喝,就有很多人跟着你一起行动,你的影响力是非常大的。但是等到我变成经理以后,我发现这些人开始逐渐疏远我。以前担任 "大哥 "的时候,我如果中午没有回公司,他们都等着我回去一起吃饭。当我做了经理以后,有的时候一忙忙到十二点多,头一抬起来,发现人都不见了,吃午饭没有人找你了,这是从心情方面有些不太好接受的地方。而从领导层面,从管理层面,这却是我开始体验怎么样带领一个团队展开工作的开始。

1991 年我加入中国惠普公司,这又是一种完全不一样的经验的开始。刚才湘明说我刚开始在内地工作的时候还负责分房、计划生育等不同的工作。其实我一开始也以为来内地是做业务,但是后来发现在这个环境里做业务有很多我们从来没有经历过的事情需要熟悉和体会,于是我一待就是 16年,我想这恐怕是我职业生涯里最宝贵的也是最值得回忆的经历,我也认为这是我人生最精彩的 16年。

25 年的时间里面,从我开始负责销售业务开始,一直到我明天最后一天离开这个工作岗位,我的业务指标成长了6000 倍。从负责整个中国惠普开始,这16 年我的业务指标增长了 250倍。我提这个数字是想让大家知道,量本身也是变化的一部分。而我到大陆做管理学习的第一件事情就是:在量变过程当中如何保证质量。

我个人另外一个比较特殊的地方是我在惠普公司担任过十个以上不同的职位,所以平均担任每个职位的时间是 2.5年,我从 2000年开始担任中国惠普总裁,这恐怕是我在惠普公司担任时间最长的工作岗位,差不多七年时间。在此之前,我是一直不断的在不同的岗位上转换。这给我带来了另外一种特殊的经验,就是我同时负责大陆、香港、台湾三个不同地区的业务。它们之间业务有很大的不同,也让我体会到虽然都是中国人,但还是有很多生活方式和习惯的不同。比如说,在香港几乎很少有客户晚上出来跟你吃饭,如果是应酬,一定是吃早餐,或者是吃中餐。但是反过来在大陆,如果是重要的客户,你跟他吃早餐的话,那是不太礼貌的,表示不够重视,这反映了各个地区业务活动的不同社会特征。

此外,我在惠普公司 25年中曾经参与七次惠普公司重大战略及组织变革,各位听到比较多的是近年惠普跟康柏的合并。但是之前惠普从电子仪表业务转型到计算机业务,以及后来拆分电子仪表业务,在这个部门基础上成立安捷伦公司,让惠普公司专职做计算机业务等等,这中间有很大的变革跨越。

同时我在惠普 25年期间换了 19位领导,光是担任中国惠普总裁七年的时间里,我就经历三个领导,这表明我们这个行业里面变化的速度跟频率是非常高的。由于我工作的关系,我亲自接待过惠普的创始人 CEO,以及卡莉,还有马克,在跟他们直接接触的过程中,我看到不同的企业家在经营企业的过程里所展现出来的不同的思维方式。接下来我总结几个我想让各位了解的、我认为做一名位职业人士非常需要的观点。

" 适应变化"——" 狮子与骆驼"

企业的经营不像人的生命,事实上企业如果经营得法,它是可以做成百年老店,甚至可以是千年老店,它是可以永续经营的。同时企业经营过程当中所追求的就是不断更加卓越的成果,这个卓越成果也是支撑企业永续经营一个非常重要的一个原因。但是企业的经营不可能是一帆风顺的,企业经营过程里面会有很多的变化和挑战 —— 包括内部变化和外部变化、国家政策变化,甚至全球经济的变化。在不断变化的过程当中还能够持续增长的企业,我把这样的企业叫做" 动成长企业" ,也就是在动态的环境中还能持续增长。当然我现在所谈的都是做为一个职业人士可能没有办法掌握的东西,我们只能去理解这个变化,同时去适应这个变化。

很多企业失败的原因不是自身经营不善,而是被快速变化的生态系统所淘汰。这里我们用狮子和骆驼,或者用草原和沙漠做个比喻。我们知道,沙漠里面最强的物种就是骆驼,如果骆驼无法控制的气侯因素起了变化,雨水丰富把沙漠逐渐变成了草原,骆驼该如何生存?各位知道在草原里面看不到骆驼,是因为骆驼在草原很难生存。而在草原上最强的物种是狮子。所以沙漠最强的骆驼面临环境变化的时候它该怎么办,它会有什么选择?骆驼要么自杀,认为老天对你不公平,让你没有生存的环境。或者找一个整形外科医生,把自己改造成狮子的样子。虽然看起来像狮子,但是狮子的外表却带了骆驼的心 —— 狮子是攻击人的,而骆驼不攻击人。

我谈这个比喻的目的是说:当企业面临变化,我们作为职业经理人的很多价值可能会因为环境变化从有价值变成没有价值。行业环境变化让很多事物从有价值变成没有价值的,或者从没有价值变成有价值的,这点我想各位可以充分理解。

我们中国人有谚语说 "男怕入错行、女怕嫁错郎 "。但是 "怕入错行 "这一点不能轻易做出判断。因为行业是有生命周期的,行业不断的变化也带来我们职业经理人市场价值的变化,我们怎么样能够在这个变化里面适应它,掌握它,我认为这是做职业人士面临的一个非常大的挑战。

惠普公司 1939年成立的是电子仪表公司,但是它今天却是一家计算机公司,两个行业是完全不一样的。同时,在这个过程里它每 20年就有一次非常重大的战略变化。我们作为职业人士,绝对不希望这个重大战略变革让我们的市场价值消失。反过来我们希望:在这个变革过程中如果我们适应得好,我们可以不断提升自己的价值。

从电子仪表到工程计算机到商用计算机,到 IT基础设施,再到 PC、打印机等,惠普公司经历了一系列不断的变革。坦白来说惠普公司的变革也不完全是自己想这么做,而是在面临行业变化的时候如果不这样做,可能就很难生存和发展。可是这会给我们职业人士带来很多要应对的变化。同时这个过程里面,惠普公司不只是有科技产品方面的变革,各位看到今天的惠普公司有六家公司组成的 —— 包括最近一次合并进来的康柏,这其中每一个重大的举措也会带来很多我们不可控制的变化。所以从另外一个角度看就是,这场变革中惠普公司的业务量还在持续的增长。而业务量的变化也会带来对经理人的挑战。所以结合在一起考虑就是:企业变化过程的战略、组织结构的变化会带来员工能力的变化,也就是对职业经理人要求的变化。这会影响到我们该有的对企业的认知、我们的价值观、我们的行为。

就像之前举的例子,当环境从沙漠变成草原的时候你是从骆驼变成狮子,不只是要你有能力去攻击别人,你还要有攻击的这种战斗欲望才行,这就是文化和行为上面的变化。这些变化就像分别代表了 " 企业文化" 、" 目标战略" 、" 组织运营" 、" 员工能力" 四个齿轮一样,它们紧密结合在一起的。这个变化过程里面,我们作为一个职业人士处在这个变化当中,如果不能够有效去适应它,去掌握它,这个变化对我们来讲就不是机会了,反而变成是一个灾难—— 它没有办法让我们在市场上增值,它也没有办法让我们继续成长。

所以我认为从外部来讲,我们要理解行业产生变化会影响企业所在的市场位置,而企业变化对企业未来经理人职位的要求也会发生变化。关键说这些变化都发生的时候,我们怎么应对?作为职业人士,因为有一些不是我们完全可以控制的因素,比如说企业家可以决定企业何时关门不做,但是职业经理是不能这样子的,我们不能掌握企业,我们可能失去工作,我们可能得不到个人价值增长,我们的市场价值也可能打了折扣。

从我自己的人生哲学以及本身的管理经验来看,或者作为职业人士,我是非常相信达尔文的进化论所讲的 "物竞天择、适者生存 "的,因为他提到了可以长久生存的物种不是最强的物种,而是适应能力最强的物种,所以最强的不见得长久生存,有的时候最强的物种适应能力是最差的。我们作为一个职业人士也是如此,要不断的适应变化。这里我想让各位理解变化是一个不变的真理,作为职业人士,我们所面临的不可控制的变化可能比任何人都多。但是那个变化不完全是一种威胁,也不完全是一个困难,如果我们知道去掌握它,调整自己去适应它,它可能就会成为一种机会。这可能对我们在这个行业当中实现价值的增长和个人的成长会产生很多好的作用。

所以关键是我们怎么样掌握变化?我总结我自己的一个体验,我认为这个变化里面有四点非常重要,我们自己作为一个职业人士要能够认知和理解这四点。

变革从改变思想开始

首先第一个是思想,这个核心的变化是四点当中最困难的。我们知道一句话, "思想决定行为,行为变成习惯,习惯决定性格,而性格决定命运,如果要改变命运的话,恐怕最重要的就是改变思想 " 。

我举我自己为例,当我 1983年从工程师转成销售的时候,那九个月到一年的时间是我在惠普公司非常艰苦的阶段,我现在依然记忆犹新。记得我曾经要卖一个大幅面绘图仪给一位客户,由于我是工程师出身,所以我卖东西就会把产品的规格,操作手册研究的很透彻。我的对象客户也是工程师,我们两个人在那个没有电子邮件和手机的时代,在那个交通不方便的时代,除了我登门拜访之外,就是写信。结果三个月以后,这个项目还是没有结果。我老板就来找我,我很得意的给他报告说,你看我给我的客户写了这么多信 —— 他告诉我什么样的规格不好、这个规格不合适他,我就说你是不对的,这个规格,这个产品肯定合适你。我跟客户不断在谈这个东西,因为我做工程师,我认为做工程师有" 天理 "的,工程师的世界是黑白分明的。但是老板之后给我讲了一句话,他说 "这个客户究竟是不喜欢你的产品,还是不喜欢你这个人? "

这句话对我这一辈子可以说是非常大的一个触动,他让我体会当我转换工作的时候,实际上整个环境都变化了。我做工程师的时候,我态度再牛、再不好,只要我把客户的问题解决了,他还是请我喝咖啡。反过来我态度再好,我不能帮客户解决问题,那他理都不理你。当我做销售的时候,我产品再好,价格再低也没有人买,而我的竞争对手却把一个很差的产品卖到很高的价格,我客户买了以后还说非常满意。好像这个世界没有真理,没有公理。

但是事实是这个新的 "生态环境 "当中决定是非的标准起了变化。而我们的思想如果没有跟着改变,我相信我不可能把这个销售工作做好。这句话对我的触动很大,我马上就调整思路。我认识到:原来问题不在产品,而在于他对我这个人的看法。于是接下来我的工作就比较辛苦:我利用下班的时间快到的时候开很远的车到办公室找他聊天。下班之后还会顺便载他一程,在车上我们聊很多东西,三个礼拜之后这个项目就做下来了。这是我第一个 " 从思想上面改变命运" 的一个体会。后来我碰到很多环境变化的情形,首先就是问一下自己我的思想有什么问题。

像我刚做领导的时候一样,公司把我提升做经理之后有三个礼拜的时间,我是非常兴奋的,碰到朋友就说, "我在惠普公司开始做领导了,这个很不容易 " 。但过了三个月以后我想辞职,因为我觉得我周围的人跟我的关系非常冷漠。后来我听了一个演讲,这个演讲者是一位" 启智" 学校的校长(" 启智学校" 是给智障人士上的学校)。公司工会办这次活动请来的这位校长演讲的主题是 "快乐人生 "。他问我们,你们知不知道世界上最快乐的人是谁?答案是 "白痴最快乐 "。而我们以前都认为白痴是可怜的,但他们最快乐。郑校长说,因为他们没有什么想法:你教他每天浇水,他就每天浇水,下大雨的时候他会打一把伞继续浇。他生活很单纯,不会有什么痛苦或者是其他的感觉。这个故事就是说,我们看事情的时候,如果角度改变,思想改变,看到的这个世界是非常不一样的。

所以每次面临变革,我都问我自己我的思想有没有问题?我在惠普公司换了十几个工作岗位,经历了 19个领导。每次换一个工作岗位,或者换一个领导,我都当作自己是从惠普公司辞职之后重新加入一家公司,我会用一家新公司的看法来看这个职位,来看这个环境,看一看自己有什么需要调整的地方没有。

我觉得思想的改变是变革过程里面一个非常重要的部分,而且我认为这是可以训练的。只要你不断的问自己: "今天你看这个事情的角度是这样子,那么你有没有机会用第二个角度看它? " 当时那位校长拿一个硬币,他说竖着看它是一条直线,横着看它是一个椭圆,再换一个角度看它就是一个圆。而这个硬币本身它其实没有改变的,它改变的只是你看它的角度。

所以在变革过程当中,如果我们不能在变革的环境改变之后改变自己看事情的角度,那么这个变革的推行是非常艰难的。但是如果能够调整好,特别是能比别人调整得更快,调整得更好,那么变革对别人就是一个威胁,对你却是机会。因为别人原来是骆驼,变成狮子之后比你慢,而你变成狮子比它快,那你就有了优势。本来你做骆驼的时候你跟不上前辈,因为前辈是老骆驼,他已经做骆驼很长时间了,你可能很长时间不见得追的上。可是环境一变,是老骆驼没有用,除非把自己改造成狮子,你你就是狮子,你变得快一点,这反而是一个机会。

在《高效的习惯》这本书中,作者提到技能知识库很重要,可是更重要的是愿望和动机。我们有希望去做事情的动机,我们的技能跟知识才会产生效果,这里的愿望和动机也是思想的问题。我们很多人上班,都说自己是 " 为五斗米折腰" ,很辛苦。认为自己是服侍老板,服侍客户,于是心里就很不平衡。可是有人上班是交朋友的,是为了跟客户有很多知识和感情方面的交流,这其中的关键就是看你怎么看这个世界。

我在离开惠普的时候到各个分公司去交接工作,他们问我最多的问题是:为什么在 25年的时间里面,他们总是感受到我每天对工作热情。我说我每天早上一起床就迫不急待想上公司上班,我大部分时间都是第一个到公司的 —— 我星期一是这样子,周末也是如此,因为我把上班当成去认识朋友,而跟朋友在一起的感觉,就像郊游一样。所以我离开这个公司,我最舍不得不是总经理的头衔,不是这个职位带来的薪水,也不是这个职位带来的待遇,我最舍不得是在惠普公司这么多年跟同事工作当中建立起来的感情,我把它当成每天上班非常重要的力量。

变革中的目标管理

你要变革,你要掌握变化,就一定要很清晰地知道不同阶段的目标。企业经营不可能是从头到尾保持一个目标不变,企业经营过程中,因为环境不断变化而必须有不同的目标,这跟我们登山是一样的道理。所以你要知道每个阶段的目标,知道我们应该做什么事情,同时专注这个目标跟外部环境之间的关系是什么。这样才不会把自己陷入到自己的内心世界里面。

变革很艰苦,我们做不到,或者说我们做得很好了,老板为什么这么不停的要求,还要做得更好呢,于是你会认为公司是在压榨我们,欺负我们。但是如果你把这个目标拿出来跟外部竞争环境做比较,你会发现原来我们还有很多成长的空间,还有很多努力要做好的地方。此时可能你变革的动力也会不太一样,同时因为目标很清晰了,你就会找到更有效的办法。我们在解决问题的时候,非常重要的就是要转换思想,如果你对问题本身不清楚,你就不可能把它解决的好。所以职业经理人在工作当中虽然学得未必是这个专业,也不是对每项工作都知道答案,但是作为总经理,最重要的是把每个问题弄清楚,明白究竟我们今天要解决的是什么问题。当你把问题定义的非常清楚之后,就可以找到非常专业的人士去解决问题。

目标也是一样的,每个阶段都有每个阶段不同的目标。拿惠普公司举例,惠普公司是非常强调个人目标跟部门目标相结合的一家公司。因为公司付你薪水,你是职业经理,你就要为公司的目标做贡献。公司目标也不会一成不便的,所以你要清楚目标是什么,怎么去评测这个目标的成功与失败。当你弄清楚目标的时候,你自然会有动力去完成它。

用开放的心去历练

我谈的第三个部分是用开放的心来历练不同的职位。我在惠普公司平均每个职位做 2.5年,多次岗位轮换让我问:变革为什么困难?是因为我们没有经历过,我们恐惧,我们害怕,或者我们担心变革不成功。就像学游泳一样,你读再多的书,如果你不下水的话,你还是不可能成为好的游泳选手。所以如果我们没有不同的经历,我们可能就会拒绝变革。

像我自己学飞行也是一样,很多人很怕飞行,可是我把它当成挑战,我认为飞行带给我很多乐趣,而飞行的体验也带来了很多很多意想不到的效果。经过了飞行以后,你的历练、你的眼界、你的感觉跟没有离开地面的人是非常不一样的,你看到的世界也是非常不同。而这种历练对你接受变革很有帮助。所以谈到在我们的职业生涯当中,不一定非要换不同的公司才可以达到这种历练的目的。你可问一下你自己:你是一辈子做销售?还是你有机会去负责服务、负责工程师、负责财务等不同的工作?你是不是接触过不同的市场?你是不是承担过不同的职责?或者在不同的地区工作过?我当时很幸运,我有机会历练过惠普大部分不同的工作,这对我接受变革、理解变革,掌握变革起了很大的作用。

我认为变革有的时候不是完全靠自身思想上面的调整,或者一些 "斗争 "可以完成的,有的时候你需要一些导师。你需要有一些人来引导你。这个导师不一定是你的经理,他可以是你的朋友,可以是公司内部的其他不同部门的人,只要你信任他,你就可以跟他聊这个事情,听听他的看法。中国人说 " 当局者迷" ,有的时候别人给你说一说,真能够起到非常重要的作用,因为这样我们就可以不断调整态度,这样也可以激励我们去不断学习新知识和技能。所以我认为思想上的调整有助于对变革过程中不同阶段的目标有清晰的了解,我们需要用外部的评测做标准,用开放的心来历练不同的职位。中国人说 "行万里路,读万卷书 "是有道理的。你看过的东西越多,你的历练就越丰富,你接受变革的宽度就越大。同时我们也可以求助于一些导师,导师不一定只有一个,不同的问题可以有不同的导师,通过请教来避免自己陷入 " 当局者迷" 的情况,我想这对掌握变化是有帮助的。

企业生态系统的文化与价值观

接下来我要谈一谈文化。

任何企业都有它的生态系统,沙漠里面只有它相对应的价值观,同样在草原上也是一样。惠普公司是一家相信以人为本、重管理哲学的一个公司,我自己深受 " 惠普之道" 的影响,甚至连我家里面都用惠普之道管理的,因为我深信这里面很重要的道理。这个道理可能是永恒不变的。这个道理就是" 公司只要创造适当的环境,员工必然全力以赴" ,这里有非常重要的两个部分,一个是 "适当的环境 ",一个是 "必然全力以赴 "。

什么意思呢?我常常做一个比喻,如果你在路上面你看到一个很脏的人,要把他变干净,我总结大概有两种不同的办法,一个办法是,由于这个人脏的原因是因为他不了解什么叫干净,他不知道每天要洗澡,他不知道应该是头发要梳整齐,他甚至不知道衣服是每天要换的,所以我们应该送他去学习正确的思想,以及学习正确的技能,他就可以把自己弄干净了。

可是另外一种观点是:他不干净的原因其实不是他不知道什么叫干净,或者不是他不愿意干净,而是因为他所生活的地方没有水,没有肥皂,没有这些东西。所以我如果给他水,我给他肥皂,我甚至给他香水,给他洗发精,给他干净的衣服,第二天他肯定打扮的干干净净。惠普公司的管理哲学更多相信第二种办法,就是:我们创造一个环境,我们相信员工会全力以赴。我们相信环境里面的资源是有的,我们每个员工也都想把事情做好,所以我们的管理挑战是每天研究这个环境,这个环境是要与时俱进的。

我刚才所提到分房的事情,当我刚来大陆的时候,因为我们中国惠普是合资公司,是受国企的管理,而国企的薪资是封顶的,四百块人民币是最高的,经理跟一般员工的差距只有 50 块人民币。所以我们吸引人才的办法就是创造分房的条件。那个时候没有商品房,我们就盖了一个300 户的宿舍楼,我每三个月组织一次分房,那个时候只有厅局级干部才有一居室、两居室,我们年轻的小伙子 40岁不到进来就一居室,这是非常好的条件。但是 94年国家推动房改,我们是第一批试点单位,开始推商品房,大家拥有住房的观念形成。这个愿望显现出来以后,光是分配住房的政策,作用就不太大了。于是我们就推动房改,我们把房子卖给我们员工,我们让员工贷款去买房,我们创造另外一个环境条件来激励员工。

以前公司早上有班车,到下午五点,员工全部不见了,因为他们要赶上五点半的班车,五点就要去换衣服。后来我们把班车取消,变成交通补贴,自此就没有五点一到到员工就离开的情况了。环境的条件不一样,员工的行为就不一样,我们每天要思考的,就是怎么样创造一个适当的环境,让员工因为这个环境而愿意每天全力以赴,这是一种管理上的哲学,这是一种信仰。

这个哲学深深影响到我的做人,甚至家庭,我觉得家里面要孩子天天去念书,你每天要求是没有用的,你要给他创造一个环境,告诉他如果把书念好了会得到什么,告诉他为什么每天要这么辛苦去念书。你要给他这种思想,给他理念,让他自己愿意去念书,而不是你天天盯着他念书,所以这个理念反映在公司,反映在企业,反映在家庭都是一样的道理。

环境里有一个非常重要的东西是价值观。这个环境必须存在一种像我们把中国人都维系在一起的华夏文明一样的东西。我们中国人为什么生活在一起?为什么老外会发现中国人到哪儿去都有一个唐人街出现?你没有听到有什么美国城、欧洲城、法国城的?这种文化和价值观不自觉的把中国人凝聚在一起。所以企业把员工凝聚在一起,不是只要有钱,不是只要有福利,不是只要有住房,不是只要有总经理的头衔就行了,产生更大力量的是价值观,这个价值观能让我们分享一种共同的感觉。

那么什么是价值观?价值观本身会起什么作用?实际上价值观本身就是我们对是非和成败的定义。在一次对话中我问学生,我说要是你要衣锦还乡,你怎么衣锦还乡法?你是要拎着两箱人民币钞票回去?还是有保镖跟在后面表示我是很大的官?或者拿着很好的名片,说我是大公司老板?这个衣锦还乡的定义其实是我们人生价值观的一部分。我想在座的很多优秀的博士生导师之所以选择学术而没有选择企业,或者没有选择做像我这样的职业人士,有一大部分原因不只是外在机遇的问题,更多他是他们自己的选择。他们认为这是对自己人生成功的一种看法。所以你不可能把不同的成功标准放在一起的。

一家企业把人才维系在一起的原因是认同一套共同的价值观。所以价值观决定了对卓越的定义是什么:我拎两箱人民币回家算是衣锦还乡,还是我今天要拿官位回去才是衣锦还乡,还是拿博士学位回去是衣锦还乡?同样定义对完成衣锦还乡的方法也是起影响的。有的人拎两箱人民币是去偷抢,有一些人用一种努力的方式,努力找一个工作,努力赚钱,所以,方法也是价值观的体现。

一个企业里面这些人之所以能够凝聚在一起,中国人说志同道合,就是因为有可以分享的共同的价值观。惠普公司吸引人的原因不只是惠普这个的名字,更重要的是惠普公司后面的价值观,让这些人在里面舍不得离开,即使外面用很高的薪水诱惑他也不会,就像他舍不得离开他的家庭一样。一个成功的企业一定有非常强和非常深厚的文化,问题是我们作为一个企业人士,我们作为一个职业人士,如果我们服务于一家企业却不认同这个文化,怎么能创造价值、实现自我?我们在这里不是为了赚钱,我们是为了一份成就感,是为了理想才来的。

所以我们常常说惠普公司吸引人的地方不是因为惠普公司给的钱多,而是因为惠普公司是一个培养人才很好的环境,很多人因为这个原因而加入惠普公司。惠普公司是一个做事的公司,从做事里面得到成就感,而不是因为钱多。我跟很多面试的人面谈,我说你要加入惠普公司,如果你要只想要钱,我说对不起,这里不是赚钱的地方,可是它是一个学习跟成长的地方。因为企业吸引了一批有着共同价值观的人,所以你自己要成为这个企业的优秀职业人士,你也必须很深刻让自己认识到这个企业有什么价值观,它对成功和是非的定义是什么,它对卓越的定义是什么,它对追求卓越的方法是什么,然后自己深深融入到里面。否则的话,会像狮子和骆驼的故事那样,你虽然是活在草原世界的一头狮子,可是这个狮子带着骆驼的心,你要生存都有问题,何谈发展?

惠普公司如何选拔人才,如何提拔员工,如何奖励员工以及如何赏识员工,背后非常清晰的就是那套价值观在发挥作用。今天在北大,我相信学校对是非、对成功的标准的定义跟企业一定非常不一样,同样和政府、军队也不一样。如果你不能认同这家企业的文化,你就不可能在这里被选择成为人才,更不可能被提拔,也不可能被奖励,也不可能被赏识,我们职业经理人可能就会失去一个非常好的工作机会,或者我们的市场价值就因为这样降低了。

所以杰克韦尔奇说,在企业里面碰到认同企业机制的人,他一定会步步高升,因为企业一定是找志同道合又有能力的人士。如果碰到融入企业价值观但是偶尔犯错的员工,会给他两到三次的机会,因为志同道合的人难找,这种缘分难得,所以你犯错了,给你第二次机会,让你可以调整。如果碰到那种不接受企业价值观也没有成绩的员工,二话不说马上开除。这些人是公司里面非常危险的分子,既做不好,又对企业价值观起着破坏的作用。

但是最难的是第三种人,他不完全认同企业文化,但是成绩非常好,你怎么办?我能够很清楚的告诉大家,在我 25年的职业生涯里面(不只是在惠普公司),我看到很多这样的人最后都是被送走离开公司的。因为他越成功,越做得好,就越证明这家企业的失败,他的成功会让所有的员工都对公司的价值观起一个问号。所以我谈这个的目的是说,作为职业人士,如果不能有效的认同企业文化,他就很难为企业家重用,不能为企业家重用,他的市场价值就会降低,他更自然的失去掉很多成长机会。

所以惠普公司特别强调,不仅要突出业绩,要有经验技能和知识,更要有领导能力和执行惠普价值观的能力。只有符合这三个条件的职业人士才有职业发展的机会。可是我们很多刚刚学校毕业的学生,或者工作时间不是很长的职业人士们都轻视或者忽视了价值观认同的问题,大部分人都把重心放在业绩和培养更强的能力上面。

在一个企业里面,并不是最强的物种才取得生存,不是智商满分、考试满分就能取得生存和发展。更重要的是你能适应这个企业摸不到可又感受得到的价值观。你适应了这个价值观,你才能有办法在一家企业里面发展。一个好的职业人士为企业家所重用不仅仅是因为他的专业能力,更重要的是他要有一个企业认同的价值观。

今天因为时间有限,所以没有办法把我 25年的东西都说完,但我还是要再度感谢北京大学给我这个机会能在这里发表我在惠普公司 25年职业生涯的最后一个演讲,也谢谢《 IT经理世界》的刘湘明先生给我这个机会来这儿给大家分享我的感受,谢谢你们。

One Joke about Software Engineer

Boy is smoking and leaving smoke rings into the air. The girl gets irritated with the smoke and says to boy "Can't you see the warning written on the cigar packet, smoking is injurious to health" The boy replies back. "I am a software engineer. We don't worry about warnings we only worry about errors"

星期三, 七月 18, 2007

UNIX bc的小数除法

这个东东整了我大半天的时间。。。

因为前面的代码算出一个TotalTime的结果是个科学计数法1.23456e+08这样的东东,在windows的MKS下只要简单的用

AverageTime=`expr $TotalTime / $total_num`

就ok了。

搬到UNIX服务器下跑就报expr expect a integer

 

开始是以为分子必须为整数,就搜出来一个方法,用bc控制分子的精度

AverageTime=`echo "scale=0;$TotalTime/$total_num"|bc'   //这个对于分子是小数点的情况还是好使的:)

这个方法在UNIX命令行直接跑能赋值,但是echo $AverageTime就会报Syntex Error in line1,

 

代入数字,一行行跑:

AverageTime=`echo "scale=2;$2.5/$2"|bc'

echo $AverageTime

能得出1

 

然后找Jason帮我调了调,到代人

AverageTime=`echo "scale=1.23456e+08;$2.5/$2"|bc'

同样报出Syntex Error in line1,

 

最后一顿狂搜,快下班的时候终于让我找到打印命令,开始用%d,说无法转换,想起来以前C的时候也用一样的格式符,就试了%.0f,搞定~

AverageTime=`expr $TotalTime / $total_num`

星期二, 七月 17, 2007

写shell script的基础篇zz

找了个写shell script的基础篇,zz一下:

Shell Programming

[Illustration]

Abstract:

In this article we explain how to write little shell scripts and give many examples.



Why shell programming?

Even though there are various graphical interfaces available for Linux the shell still is a very neat tool. The shell is not just a collection of commands but a really good programming language.You can automate a lot of tasks with it, the shell is very good for system administration tasks, you can very quickly try out if your ideas work which makes it very useful for simple prototyping and it is very useful for small utilities that perform some relatively simple tasks where efficiency is less important than ease of configuration, maintenance and portability.
So let's see now how it works:

Creating a script

There are a lot of different shells available for Linux but usually the bash (bourne again shell) is used for shell programming as it is available for free and is easy to use. So all the scripts we will write in this article use the bash (but will most of the time also run with its older sister, the bourne shell).
For writing our shell programs we use any kind of text editor, e.g. nedit, kedit, emacs, vi...as with other programming languages.
The program must start with the following line (it must be the first line in the file):
    #!/bin/sh

The #! characters tell the system that the first argument that follows on the line is the program to be used to execute this file. In this case /bin/sh is shell we use.
When you have written your script and saved it you have to make it executable to be able to use it.
To make a script executable type
chmod +x filename
Then you can start your script by typing: ./filename

Comments

Comments in shell programming start with # and go until the end of the line. We really recommend you to use comments. If you have comments and you don't use a certain script for some time you will still know immediately what it is doing and how it works.

Variables

As in other programming languages you can't live without variables. In shell programming all variables have the datatype string and you do not need to declare them. To assign a value to a variable you write:
varname=value
To get the value back you just put a dollar sign in front of the variable:
#!/bin/sh
# assign a value:
a="hello world"
# now print the content of "a":
echo "A is:"
echo $a
Type this lines into your text editor and save it e.g. as first. Then make the script executable by typing chmod +x first in the shell and then start it by typing ./first
The script will just print:
A is:
hello world
Sometimes it is possible to confuse variable names with the rest of the text:
num=2
echo "this is the $numnd"
This will not print "this is the 2nd" but "this is the " because the shell searches for a variable called numnd which has no value. To tell the shell that we mean the variable num we have to use curly braces:
num=2
echo "this is the ${num}nd"
This prints what you want: this is the 2nd
There are a number of variables that are always automatically set. We will discuss them further down when we use them the first time.
If you need to handle mathematical expressions then you need to use programs such as expr (see table below).
Besides the normal shell variables that are only valid within the shell program there are also environment variables. A variable preceeded by the keyword export is an environment variable. We will not talk about them here any further since they are normally only used in login scripts.

Shell commands and control structures

There are three categories of commands which can be used in shell scripts:
1)Unix commands:
Although a shell script can make use of any unix commands here are a number of commands which are more often used than others. These commands can generally be described as commands for file and text manipulation.

Command syntax
Purpose

echo "some text"
write some text on your screen

ls
list files

wc -l file
wc -w file
wc -c file
count lines in file or
count words in file or
count number of characters

cp sourcefile destfile
copy sourcefile to destfile

mv oldname newname
rename or move file

rm file
delete a file

grep 'pattern' file
search for strings in a file
Example: grep 'searchstring' file.txt

cut -b colnum file
get data out of fixed width columns of text
Example: get character positions 5 to 9
cut -b5-9 file.txt
Do not confuse this command with "cat" which is something totally different

cat file.txt
write file.txt to stdout (your screen)

file somefile
describe what type of file somefile is

read var
prompt the user for input and write it into a variable (var)

sort file.txt
sort lines in file.txt

uniq
remove duplicate lines, used in combination with sort since uniq removes only duplicated consecutive lines
Example: sort file.txt | uniq

expr
do math in the shell
Example: add 2 and 3
expr 2 "+" 3

find
search for files
Example: search by name:
find . -name filename -print
This command has many different possibilities and options. It is unfortunately too much to explain it all in this article.

tee
write data to stdout (your screen) and to a file
Normally used like this:
somecommand | tee outfile
It writes the output of somecommand to the screen and to the file outfile

basename file
return just the file name of a given name and strip the directory path
Example: basename /bin/tux
returns just tux

dirname file
return just the directory name of a given name and strip the actual file name
Example: dirname /bin/tux
returns just /bin

head file
print some lines from the beginning of a file

tail file
print some lines from the end of a file

sed
sed is basically a find and replace program. It reads text from standard input (e.g from a pipe) and writes the result to stdout (normally the screen). The search pattern is a regular expression (see references). This search pattern should not be confused with shell wildcard syntax. To replace the string linuxfocus with LinuxFocus in a text file use:
cat text.file | sed 's/linuxfocus/LinuxFocus/' > newtext.file
This replaces the first occurance of the string linuxfocus in each line with LinuxFocus. If there are lines where linuxfocus appears several times and you want to replace all use:
cat text.file | sed 's/linuxfocus/LinuxFocus/g' > newtext.file

awk
Most of the time awk is used to extract fields from a text line. The default field separator is space. To specify a different one use the option -F.

 cat file.txt | awk -F, '{print $1 "," $3 }'

Here we use the comma (,) as field separator and print the first and third ($1 $3) columns. If file.txt has lines like:
Adam Bor, 34, India
Kerry Miller, 22, USA

then this will produce:
Adam Bor, India
Kerry Miller, USA

There is much more you can do with awk but this is a very common use.

2) Concepts: Pipes, redirection and backtick
They are not really commands but they are very important concepts.
pipes (|) send the output (stdout) of one program to the input (stdin) of another program.

    grep "hello" file.txt | wc -l
finds the lines with the string hello in file.txt and then counts the lines.
The output of the grep command is used as input for the wc command. You can concatinate as many commands as you like in that way (within reasonable limits).
redirection: writes the output of a command to a file or appends data to a file
> writes output to a file and overwrites the old file in case it exists
>> appends data to a file (or creates a new one if it doesn't exist already but it never overwrites anything).
Backtick
The output of a command can be used as command line arguments (not stdin as above, command line arguments are any strings that you specify behind the command such as file names and options) for another command. You can as well use it to assign the output of a command to a variable.
The command
find . -mtime -1 -type f -print
finds all files that have been modified within the last 24 hours (-mtime -2 would be 48 hours). If you want to pack all these files into a tar archive (file.tar) the syntax for tar would be:
tar xvf file.tar infile1 infile2 ...
Instead of typing it all in you can combine the two commands (find and tar) using backticks. Tar will then pack all the files that find has printed:
#!/bin/sh
# The ticks are backticks (`) not normal quotes ('):
tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print`

3) Control structures
The "if" statement tests if the condition is true (exit status is 0, success). If it is the "then" part gets executed:
if ....; then
....
elif ....; then
....
else
....
fi
Most of the time a very special command called test is used inside if-statements. It can be used to compare strings or test if a file exists, is readable etc...
The "test" command is written as square brackets " [ ] ". Note that space is significant here: Make sure that you always have space around the brackets. Examples:
[ -f "somefile" ]  : Test if somefile is a file.
[ -x "/bin/ls" ] : Test if /bin/ls exists and is executable.
[ -n "$var" ] : Test if the variable $var contains something
[ "$a" = "$b" ] : Test if the variables "$a" and "$b" are equal
Run the command "man test" and you get a long list of all kinds of test operators for comparisons and files.
Using this in a shell script is straight forward:
#!/bin/sh
if [ "$SHELL" = "/bin/bash" ]; then
echo "your login shell is the bash (bourne again shell)"
else
echo "your login shell is not bash but $SHELL"
fi
The variable $SHELL contains the name of the login shell and this is what we are testing here by comparing it against the string "/bin/bash"
Shortcut operators
People familiar with C will welcome the following expression:
[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors"
The && can be used as a short if-statement. The right side gets executed if the left is true. You can read this as AND. Thus the example is: "The file /etc/shadow exists AND the command echo is executed". The OR operator (||) is available as well. Here is an example:
#!/bin/sh
mailfolder=/var/spool/mail/james
[ -r "$mailfolder" ] || { echo "Can not read $mailfolder" ; exit 1; }
echo "$mailfolder has mail from:"
grep "^From " $mailfolder
The script tests first if it can read a given mailfolder. If yes then it prints the "From" lines in the folder. If it cannot read the file $mailfolder then the OR operator takes effect. In plain English you read this code as "Mailfolder readable or exit program". The problem here is that you must have exactly one command behind the OR but we need two:
-print an error message
-exit the program
To handle them as one command we can group them together in an anonymous function using curly braces. Functions in general are explained further down.
You can do everything without the ANDs and ORs using just if-statements but sometimes the shortcuts AND and OR are just more convenient.
The case statement can be used to match (using shell wildcards such as * and ?) a given string against a number of possibilities.
case ... in
...) do something here;;
esac
Let's look at an example. The command file can test what kind of filetype a given file is:
file lf.gz

returns:

lf.gz: gzip compressed data, deflated, original filename,
last modified: Mon Aug 27 23:09:18 2001, os: Unix
We use this now to write a script called smartzip that can uncompress bzip2, gzip and zip compressed files automatically :
#!/bin/sh
ftype=`file "$1"`
case "$ftype" in
"$1: Zip archive"*)
unzip "$1" ;;
"$1: gzip compressed"*)
gunzip "$1" ;;
"$1: bzip2 compressed"*)
bunzip2 "$1" ;;
*) error "File $1 can not be uncompressed with smartzip";;
esac

Here you notice that we use a new special variable called $1. This variable contains the first argument given to a program. Say we run
smartzip articles.zip
then $1 will contain the string articles.zip
The select statement is a bash specific extension and is very good for interactive use. The user can select a choice from a list of different values:
select var in ... ; do
break
done
.... now $var can be used ....
Here is an example:
#!/bin/sh
echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
break
done
echo "You have selected $var"
Here is what the script does:
What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux
In the shell you have the following loop statements available:
while ...; do
....
done
The while-loop will run while the expression that we test for is true. The keyword "break" can be used to leave the loop at any point in time. With the keyword "continue" the loop continues with the next iteration and skips the rest of the loop body.
The for-loop takes a list of strings (strings separated by space) and assigns them to a variable:
for var in ....; do
....
done
The following will e.g. print the letters A to C on the screen:
#!/bin/sh
for var in A B C ; do
echo "var is $var"
done
A more useful example script, called showrpm, prints a summary of the content of a number of RPM-packages:
#!/bin/sh
# list a content summary of a number of RPM packages
# USAGE: showrpm rpmfile1 rpmfile2 ...
# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpm
for rpmpackage in $*; do
if [ -r "$rpmpackage" ];then
echo "=============== $rpmpackage =============="
rpm -qi -p $rpmpackage
else
echo "ERROR: cannot read file $rpmpackage"
fi
done
Above you can see the next special variable, $* which contains all the command line arguments. If you run
showrpm openssh.rpm w3m.rpm webgrep.rpm
then $* contains the 3 strings openssh.rpm, w3m.rpm and webgrep.rpm.
The GNU bash knows until-loops as well but generally while and for loops are sufficient.
Quoting
Before passing any arguments to a program the shell tries to expand wildcards and variables. To expand means that the wildcard (e.g. *) is replaced by the appropriate file names or that a variable is replaced by its value. To change this behaviour you can use quotes: Let's say we have a number of files in the current directory. Two of them are jpg-files, mail.jpg and tux.jpg.
#!/bin/sh
echo *.jpg
This will print "mail.jpg tux.jpg".
Quotes (single and double) will prevent this wildcard expansion:
#!/bin/sh
echo "*.jpg"
echo '*.jpg'
This will print "*.jpg" twice.
Single quotes are most strict. They prevent even variable expansion. Double quotes prevent wildcard expansion but allow variable expansion:
#!/bin/sh
echo $SHELL
echo "$SHELL"
echo '$SHELL'
This will print:
/bin/bash
/bin/bash
$SHELL
Finally there is the possibility to take the special meaning of any single character away by preceeding it with a backslash:
echo \*.jpg
echo \$SHELL
This will print:
*.jpg
$SHELL
Here documents
Here documents are a nice way to send several lines of text to a command. It is quite useful to write a help text in a script without having to put echo in front of each line. A "Here document" starts with << followed by some string that must also appear at the end of the here document. Here is an example script, called ren, that renames multiple files and uses a here document for its help text:
#!/bin/sh
# we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ] ; then
cat <<HELP
ren -- renames a number of files using sed regular expressions

USAGE: ren 'regexp' 'replacement' files...

EXAMPLE: rename all *.HTM files in *.html:
ren 'HTM$' 'html' *.HTM

HELP
exit 0
fi
OLD="$1"
NEW="$2"
# The shift command removes one argument from the list of
# command line arguments.
shift
shift
# $* contains now all the files:
for file in $*; do
if [ -f "$file" ] ; then
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
if [ -f "$newfile" ]; then
echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile ..."
mv "$file" "$newfile"
fi
fi
done
This is the most complex script so far. Let's discuss it a little bit. The first if-statement tests if we have provided at least 3 command line parameters. (The special variable $# contains the number of arguments.) If not, the help text is sent to the command cat which in turn sends it to the screen. After printing the help text we exit the program. If there are 3 or more arguments we assign the first argument to the variable OLD and the second to the variable NEW. Next we shift the command line parameters twice to get the third argument into the first position of $*. With $* we enter the for loop. Each of the arguments in $* is now assigned one by one to the variable $file. Here we first test that the file really exists and then we construct the new file name by using find and replace with sed. The backticks are used to assign the result to the variable newfile. Now we have all we need: The old file name and the new one. This is then used with the command mv to rename the files.
Functions
As soon as you have a more complex program you will find that you use the same code in several places and also find it helpful to give it some structure. A function looks like this:
functionname()
{
# inside the body $1 is the first argument given to the function
# $2 the second ...
body
}
You need to "declare" functions at the beginning of the script before you use them.
Here is a script called xtitlebar which you can use to change the name of a terminal window. If you have several of them open it is easier to find them. The script sends an escape sequence which is interpreted by the terminal and causes it to change the name in the titlebar. The script uses a function called help. As you can see the function is defined once and then used twice:
#!/bin/sh
# vim: set sw=4 ts=4 et:

help()
{
cat <<HELP
xtitlebar -- change the name of an xterm, gnome-terminal or kde konsole

USAGE: xtitlebar [-h] "string_for_titelbar"

OPTIONS: -h help text

EXAMPLE: xtitlebar "cvs"

HELP
exit 0
}

# in case of error or if -h is given we call the function help:
[ -z "$1" ] && help
[ "$1" = "-h" ] && help

# send the escape sequence to change the xterm titelbar:
echo -e "\033]0;$1\007"
#
It's a good habit to always have extensive help inside the scripts. This makes it possible for others (and you) to use and understand the script.
Command line arguments
We have seen that $* and $1, $2 ... $9 contain the arguments that the user specified on the command line (The strings written behind the program name). So far we had only very few or rather simple command line syntax (a couple of mandatory arguments and the option -h for help). But soon you will discover that you need some kind of parser for more complex programs where you define your own options. The convention is that all optional parameters are preceeded by a minus sign and must come before any other arguments (such as e.g file names).
There are many possibilities to implement a parser. The following while loop combined with a case statement is a very good solution for a generic parser:
#!/bin/sh
help()
{
cat <<HELP
This is a generic command line parser demo.
USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2
HELP
exit 0
}

while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
-f) opt_f=1;shift 1;; # variable opt_f is set
-l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2
--) shift;break;; # end of options
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done

echo "opt_f is $opt_f"
echo "opt_l is $opt_l"
echo "first arg is $1"
echo "2nd arg is $2"
Try it out! You can run it e.g with:
cmdparser -l hello -f -- -somefile1 somefile2
It produces
opt_f is 1
opt_l is hello
first arg is -somefile1
2nd arg is somefile2
How does it work? Basically it loops through all arguments and matches them against the case statement. If it finds a matching one it sets a variable and shifts the command line by one. The unix convention is that options (things starting with a minus) must come first. You may indicate that this is the end of option by writing two minus signs (--). You need it e.g with grep to search for a string starting with a minus sign:
Search for -xx- in file f.txt:
grep -- -xx- f.txt
Our option parser can handle the -- too as you can see in the listing above.

Examples

A general purpose sceleton
Now we have discussed almost all components that you need to write a script. All good scripts should have help and you can as well have our generic option parser even if the script has just one option. Therefore it is a good idea to have a dummy script, called framework.sh, which you can use as a framework for other scripts. If you want to write a new script you just make a copy:
cp framework.sh myscript
and then insert the actual functionality into "myscript".
Let's now look at two more examples:
A binary to decimal number converter
The script b2d converts a binary number (e.g 1101) into its decimal equivalent. It is an example that shows that you can do simple mathematics with expr:
#!/bin/sh
# vim: set sw=4 ts=4 et:
help()
{
cat <<HELP
b2h -- convert binary to decimal

USAGE: b2h [-h] binarynum

OPTIONS: -h help text

EXAMPLE: b2h 111010
will return 58
HELP
exit 0
}

error()
{
# print an error and exit
echo "$1"
exit 1
}

lastchar()
{
# return the last character of a string in $rval
if [ -z "$1" ]; then
# empty string
rval=""
return
fi
# wc puts some space behind the output this is why we need sed:
numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
# now cut out the last char
rval=`echo -n "$1" | cut -b $numofchar`
}

chop()
{
# remove the last character in string and return it in $rval
if [ -z "$1" ]; then
# empty string
rval=""
return
fi
# wc puts some space behind the output this is why we need sed:
numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
if [ "$numofchar" = "1" ]; then
# only one char in string
rval=""
return
fi
numofcharminus1=`expr $numofchar "-" 1`
# now cut all but the last char:
rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`
}


while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;; # function help is called
--) shift;break;; # end of options
-*) error "error: no such option $1. -h for help";;
*) break;;
esac
done

# The main program
sum=0
weight=1
# one arg must be given:
[ -z "$1" ] && help
binnum="$1"
binnumorig="$1"

while [ -n "$binnum" ]; do
lastchar "$binnum"
if [ "$rval" = "1" ]; then
sum=`expr "$weight" "+" "$sum"`
fi
# remove the last position in $binnum
chop "$binnum"
binnum="$rval"
weight=`expr "$weight" "*" 2`
done

echo "binary $binnumorig is decimal $sum"
#
The algorithm used in this script takes the decimal weight (1,2,4,8,16,..) of each digit starting from the right most digit and adds it to the sum if the digit is a 1. Thus "10" is:
0 * 1 + 1 * 2 = 2
To get the digits from the string we use the function lastchar. This uses wc -c to count the number of characters in the string and then cut to cut out the last character. The chop function has the same logic but removes the last character, that is it cuts out everything from the beginning to the character before the last one.
A file rotation program
Perhaps you are one of those who save all outgoing mail to a file. After a couple of months this file becomes rather big and it makes the access slow if you load it into your mail program. The following script rotatefile can help you. It renames the mailfolder, let's call it outmail, to outmail.1 if there was already an outmail.1 then it becomes outmail.2 etc...
#!/bin/sh
# vim: set sw=4 ts=4 et:
ver="0.1"
help()
{
cat <<HELP
rotatefile -- rotate the file name

USAGE: rotatefile [-h] filename

OPTIONS: -h help text

EXAMPLE: rotatefile out
This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1
and create an empty out-file

The max number is 10

version $ver
HELP
exit 0
}

error()
{
echo "$1"
exit 1
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;;
--) break;;
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done

# input check:
if [ -z "$1" ] ; then
error "ERROR: you must specify a file, use -h for help"
fi
filen="$1"
# rename any .1 , .2 etc file:
for n in 9 8 7 6 5 4 3 2 1; do
if [ -f "$filen.$n" ]; then
p=`expr $n + 1`
echo "mv $filen.$n $filen.$p"
mv $filen.$n $filen.$p
fi
done
# rename the original file:
if [ -f "$filen" ]; then
echo "mv $filen $filen.1"
mv $filen $filen.1
fi
echo touch $filen
touch $filen

How does the program work? After checking that the user provided a filename we go into a for loop counting from 9 to 1. File 9 is now renamed to 10, file 8 to 9 and so on. After the loop we rename the original file to 1 and create an empty file with the name of the original file.

Debugging

The most simple debugging help is of course the command echo. You can use it to print specific variables around the place where you suspect the mistake. This is probably what most shell programmers use 80% of the time to track down a mistake. The advantage of a shell script is that it does not require any re-compilation and inserting an "echo" statement is done very quickly.
The shell has a real debug mode as well. If there is a mistake in your script "strangescript" then you can debug it like this:
sh -x strangescript
This will execute the script and show all the statements that get executed with the variables and wildcards already expanded.
The shell also has a mode to check for syntax errors without actually executing the program. To use this run:
sh -n your_script
If this returns nothing then your program is free of syntax errors.
We hope you will now start writing your own shell scripts. Have fun!

References



  • The man page and the info page of bash are very good and you will discover many more tricks there:
    man bash
    tkinfo bash
    (I hate the standard info browser but tkinfo or konqueror (the url is info:/bash/Top ) are quite good)
  • The LinuxFocus article on GNU file utilities
  • The LinuxFocus article on Regular expressions
  • The LinuxFocus article on AWK
  • The LinuxFocus article on Basic UNIX commands
  • If bash isn't installed on your system or you want to get the newest version you can download it from the GNU website at http://www.gnu.org (to find out if bash is already installed on your system just type bash. If you don't get an error message bash is already installed)

Talkback form for this article

Every article has its own talkback page. On this page you can submit a comment or look at comments from other readers:

星期三, 七月 11, 2007

How to put files from Windows to UNIX

open CMD window in Windows, type

ftp 111.111.111.111 //UNIX server name ,

or :

ftp -i 111.111.111.111 //will not prompt to ask y/n for each file; if you already typed ftp 111.111.111.111, you can also just type prompt to switch the on/off of the y/n asking

type UNIX username and password when asked

cd to the path you want to transfer files to after the ftp>

ftp> cd location

type mput filename to transfer files:

ftp> mput fileA fileB

or transfer many files:

ftp> mput file*

 

I tried to use FTP on my own computer, one Network Map to the Windows server, the other Unix server to transfer the files, but quite slow. And coz I forgot to changed the transfer mode from Smart to ASCII, it prompt to ask me for each file coz the file sufix is .1 .2 .3 etc. :(

The method of this blog is much faster, thanks Eric for suggestion~

星期二, 七月 10, 2007

致HP总裁的一封信 zz

同事传的,关于DCC,说总结的不错。还没仔细看,先转载过来慢慢看吧。

出处: 戴尔直通车

Glenn Keels,商用服务器高级经理

---亲爱的赫德先生:我们的想法是一致的。30/70是企业IT的目标

一些我们和惠普公司的共同客户向我们询问了上周惠普首席执行官赫德在维加斯的活动中所做的主题演讲(我猜在维加斯发生的任何事情都会传出去)。

赫德先生称惠普公司要削减开支,以更多地投资于创新,这的确很有必要。他指出,虽然资本支出比较平稳,但是IT维护及运营费都在增加。赫德先生保证惠普公司将把维护和运营费控制在IT总预算的30%,而创新占70%。我们对这个比例完全赞同,事实上,我们为实现同样的目标也取得了切实的进展。

06财年,戴尔公司的IT支出占收入的1.39%,创造了14亿美元的利益。目前我们用于维护和创新的费用分别占40%和60%。我们正在努力削减多余的10%(shoud it be 10%)维护费用。关于如何实现30/70的目标,我们真诚希望听取你们的意见和建议。你们可以对这个帖子畅所欲言地进行评论,访问IdeaStorm网站,或者在你们自己的博客上发表评论,标上戴尔30/70,方便我们查找。

惠普计划把原有的85个数据中心削减为6个。而我们已经成功地把30个数据中心缩减为两个。这个过程虽然痛苦,但很值得,不仅节省了数亿美元,而且大规模地化繁为简,消除重复工作,回想起来很疯狂。几年多来我们实施了六个项目,带来了巨大的效益。我们做了以下工作:

  1. 整合业务流程和应用。针对每一主要业务流程我们都设计了一个全球通用的流程。只有这样,我们才可以选择最佳应用来支持每个流程,并在一个地点进行统一控制。比如,以前我们在86个国家管理戴尔网站,后来我们建立了一个全球通用的电子商务流程,现在我们就可以在一个地点囊括戴尔网站的86个版本了。
  2. 迁移到更少的操作系统上。在戴尔曾经有几十种的操作系统,包括不同种的UNIX、Solaris, Windows NT,2000和2003。而且,没有标准的桌面系统镜象。现在,我们有三个服务器系统镜象(Linux,Windows和Netware),一个单独为台式机设计的系统镜象,这样IT经理晚上终于可以有时间睡觉了。
  3. 整合了服务器。如果没有缩减数据中心,现在我们很可能有超过5万台服务器,并需要数百名员工来管理它们!减少操作系统和应用之后,通过标准化,我们现在只需要2万个服务器来开展业务。而整个操作只需要十几个人管理。尽管如此,我们并没有因整合服务器而满足不前。现在在淘汰旧服务器的过程中,我们推出了虚拟化。我们相信在今后的几年,能对我们整个IT基础架构进行50%的虚拟化,而且这个比例还将随着我们建立更多全新的专为虚拟化服务器设立的应用而逐步增加。这样,我们就能更加接近30/70的目标。
  4. 合理化存储。对于我们多数客户来说,控制存储是一个挑战。以前,我们有所有类型的存储,后来我们转移到存储区域网络(SAN)以提高效率和稳定性,减少复杂度和成本,这很奏效。戴尔的SAN产品团队成功降低了SAN的入门和实施成本。现在我们的存储容量几乎达到了7PB,这是目前运营的最大商用SAN之一。
  5. 优化数据库。几年前,几乎每个戴尔团队都有自己的数据栈,包括Excel表格,Access,SQL服务器和大Oracle数据库(还有中间的所有东西)。我们给自己定的目标是很远大的:为戴尔所有的任务关键型数据建立一个统一的数据库。这个项目是10年前开始的,现在这个任务已经实现了。这里有一个很好的例子:当你在戴尔网站上购物时,客户的点击100%都被捕捉到这个数据库中。我们的团队最近还用点击数据,IP地址和Google Earth建立了一个mashup,真的很酷。
  6. 建立了新数据中心,关闭了旧数据中心。经过一些教训,我们认识到建立新数据中心比扩大已有的数据中心要容易得多,而且要确保支持新技术的合适设备,这是唯一的方法。于是从几年前,我们把30个数据中心缩减为2个,现在我们利用全公司的人才再多建立两个世界上最先进的最节能的数据中心。

通过这6个项目,我们学到了很多,而且我们很高兴把我们的经验"开源"出来,帮助其他公司-包括赫德先生和惠普公司--实现30/70的目标。以下就是我想和大家分享的三个教训:

  • 不要刻薄你的IT基础设施,因为它会处罚你的。对IT投资不足就像给汽车加廉价汽油,在汽油和定期检修上省钱。在某个时候,你会发现,车停在路边动弹不了。同样的,用老的"先进"的数据中心,是不能够满足你IT和企业扩展的需要的。因此,要三思而后行,建设容易扩展的数据中心。
  • IT创新要与业务接近。这个教训来之不易。我们在印度建立了一个发展中心,配备了世界上最好的软件架构师和程序员,但他们离业务和业务领导太远了。现在我们把关键的应用程序的开发与业务保持得很近,我们的开发团队,不管他们在印度、巴西,中国、欧洲还是美国,都被紧密地纳入了他们所支持的业务流程中。
  • 分散并减少风险。这个问题对于我们和我们所有的客户都非常重要。我们的方法很直接:每季度,我们故意"打破"关键的应用程序,确保我们的备份系统正常运作。这样做使我们发现,原来我们太过分依赖磁带了。尽管磁带很适合某些应用程序,但对其他应用程序备份数据却很慢。 

30/70目标是我们在戴尔经常谈论的话题。在接下来的几周里,我和我的同时将就实现这一目标交流更多的想法。同时,我们希望听听IT业界人士有些什么想法。你是怎么削减开支,简化系统,以更加专注于创新的?让讨论继续下去吧。

用mkdir 命令一次生成嵌套的多个目录

mkdir 命令

功能:创建一个目录(类似DOS下的md命令)。

语法:mkdir [选项] dirname

说明:该命令创建由 dirname 命名的目录。要求创建目录的用户在当前目录中(dirname 的父目录中)具有写权限,并且 dirname 不能是当前目录中已有的目录或文件名称。

命令中各选项的含义如下:

-m 对新建目录设置存取权限。也可以用chmod命令设置。 -p 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后, 系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录。

指令实例:

mkdir -p -m 700 ./inin/mail/ % 该命令的执行结果是在当前目录中创建嵌套的目录层次inin 和inin下的mail目录, 权限设置为只有文件主有读、写和执行权限。

---------------------------------------------------------------------摘自指令说明

Scattered T-Storms

下了个鱼鱼桌面,刚好有个看天气的插件,就看了13号后几天温州的天气,结果全是Scattered T-Storms,吓死我了。在网上google一下,原来是骤雨。因为插件里面用云+闪电的样子,感觉比较可怕。。。

有趣的是,搜到的前两个结果都是一个叫 跟我去马尔地夫的台湾论坛,里面有很详细的马尔代夫自由行的资料:)那里也是很多Scattered T-Storms的说,所以很多人会担心,不过据说后来都只是很短的骤雨或者阴天哦~

但愿雁荡山也是大部分时间的好天气啦·^_^~

星期一, 七月 09, 2007

Some tips when migrating ksh+java program from Windows to UNIX

1. When transfering .jar and .class file, remember to use binary transfer

2. The delimiter of classpaths is ":" in UNIX, while in Windows it was ";"

3. UNIX doesn't allow empty line in .ksh file, while it is OK in windows

4. When calling a .ksh file, such as "sh test.ksh", in UNIX, remember to follow this ".ksh" subfix, while in windows, it can be "sh test" (I haven't tried if it can have ".ksh" in windows)

5. For a while statement, in Windows, it can use "while do"("do" begins a new line), nothing between while and do, to make a Infinite Loop, but we should change it to "while true do". (we tried while [1] do, but failed.)  (There's expression "while [ condition ]; do", but we didn't try, coz get the solution before trying it. If something happens to while statement, u may try to add a ";" before "do")

6. UNIX script uses [] for most parameters, when use it, remember to insert a space between the [] and the parameter in it (You can refer to the UNIX SHELL man exported: www.research.att.com/~gsf/man/man1/ksh.html )