Archive

Archive for the ‘脚本语言’ Category

10个最有用的Linux命令

July 7th, 2009 ricky.zhu No comments

Categories: 脚本语言 Tags:

Unix高手的另外十个习惯

August 27th, 2008 ricky.zhu 1 comment

大家都知道著名的Unix高手的十个习惯,殊不知Unix高手还有另外十个习惯,呵呵,偶不是Unix高手,不过有几个习惯还是挺常用的,转载一下IBM的网站上还是有很多好文章的。

您应当采纳的其他 10 个好习惯包括:

* 使用文件名自动完成功能 (file name completion)。
* 使用历史扩展。
* 重用以前的参数。
* 使用 pushd 和 popd 管理目录导航。
* 查找大型文件。
* 不使用编辑器创建临时文件。
* 使用 curl 命令行实用工具。
* 最有效地利用正则表达式。
* 确定当前用户。
* 使用 awk 处理数据。

使用文件名完成

如果不需要在命令提示符处键入长的、令人费解的文件名,这是不是很棒呢?的确,您不需要这样做。相反,您可以配置最流行的 UNIX Shell 以使用文件名完成。该功能在各个 Shell 中的工作方式略有不同,因此我将向您展示如何在最流行的 Shell 中使用文件名完成。文件名完成使您可以更快地输入并避免错误。懒惰?也许吧。效率更高?当然!

我正在运行哪种 Shell?

如果您不知道目前使用的是哪一种 Shell,会怎么样?虽然这个诀窍不是另外 10 个好习惯的正式组成部分,但它仍然很有用。如清单 1 所示,您可以使用 echo $0 或 ps -p $$ 命令显示您正在使用的 Shell。对于我来说,运行的是 Bash Shell。
Read more…

Categories: 脚本语言 Tags:

sed中如何引用变量

August 5th, 2008 ricky.zhu 2 comments

最近写了一个系统资源监控的脚本,想让它每次机器重启后也可以自动启动,决定放到inittab中去,于是写了一个脚本run.sh,执行的时候启动脚本,并且加入inittab,可是调试的时候发现sed对shell的变量引用一直无法替换,百思不得其解,后来得到几位高手指点,终于解决,还有两种解决方法。

问题描述:

命令是这样的

work_dir=/home/ractest

sed ‘/start_mon.sh/d
/init.crsd/ a\
h4:3:respawn:${work_dir}/scripts/start_mon.sh >/dev/null 2>&1
‘ /etc/inittab

想把work_dir替换,但是没成功。

sed “/start_mon.sh/d
/init.crsd/ a\
h4:3:respawn:${work_dir}/scripts/start_mon.sh >/dev/null 2>&1
” /etc/inittab
试过,报错
sed: command garbled: /init.crsd/ ah4:3:respawn:/home/ractest/ricky/atm_monitor/scripts/start_mon.sh >/dev/null 2>&1

在Solaris上不行,但是在Linux上是可以的,所以初步确定是sed版本的问题,但是在Solaris上怎么解决这个我呢?有如下两种方法:

方法一:使用单引号
单引号:

sed ‘/start_mon.sh/d
> /init.crsd/ a\
> h4:3:respawn:’${work_dir}’/scripts/start_mon.sh >/dev/null 2>&1
> ‘ /etc/inittab

方法二:使用双引号

sed “/start_mon.sh/d
> /init.crsd/ a\\
> h4:3:respawn:${work_dir}/scripts/start_mon.sh >/dev/null 2>&1
> ” /etc/inittab

经过测试,两种方法都可以成功。
记录之!

Categories: 脚本语言 Tags:

Shell综合水平测试

June 5th, 2008 ricky.zhu No comments

一直以为自己很喜欢shell,看了ChinaUnix上的这个水平测试,才知道原来自己什么都不是 :(

[ OVERVIEW 篇 ]

1. 有很多种 shell, 你熟悉几种? 各个 shell 的 home page 在那里?

2. 为什么说 zsh 是目前为止功能最为强大的 shell.

3. 为什么说 pdksh 功能较弱?

4. ksh88 与 ksh93 有何区别?

5. 为什么 shell 编程最好用 ksh?

6. 你的系统都有哪些 shell? 版本是多少?

7. 你知道 POSIX 吗?最新版本是多少?和你的 shell 有什么关系?

8. /sbin/sh 和 /bin/sh 有何区别?

9. 你分析过 1000 行以上的 shell 程序吗?

10. 各种 shell 的变量名长度有何限制?
Read more…

Categories: 脚本语言 Tags:

Clusterware Console脚本分享

March 5th, 2008 ricky.zhu No comments

做Clusterware和RAC的测试的时候,节点多的时候,需要不停的在节点之间切换,而且容易出错,于是写了这样一个脚本,跟大家分享一下。
目前主要完成一些简单的功能,支持的平台有Linux,Solaris, AIX and HP,打算继续扩展。也欢迎使用并提出意见,

[ractest@sun880-1 ~]$ more console
#!/bin/bash

#This script is used to control the whole cluster nodes in one interface

echo "******************************************************************"
echo "                  Welcome to Cluster Console                      "
echo "                                                                  "
echo "The console is used to control the whole cluster nodes in one node"
echo "now it can support start/stop stack,check stack status, process   "
echo "priority, check node uptime and will support more in the future   "
echo "                                                                  "
echo "  Any bug or comment please report to ricky.zhu@gmail.com        "
echo "******************************************************************"

get_nodename () {
  $CH/bin/olsnodes -n > tmp
  name=`head -n $1 tmp | tail -1 | awk ‘{print $1}’`
  echo "$name"
}
check_uptime() {

  nl=`$CH/bin/olsnodes `
  for node in $nl
  do
    echo "node=$node"
    $RSH $node "hostname; date; /usr/bin/uptime"
  done
}

UNAME=‘/bin/uname’
PLATFORM=`$UNAME`
 <a href="http://www.rickyzhu.com/444_cluster-console-script.html#more-444" class="more-link">Read more…</a>

Categories: 脚本语言 Tags:

十大原因告诉你不要使用CShell

February 27th, 2008 ricky.zhu No comments

正当我满心欢喜,打算看看C Shell到底是何方神圣,并重新温习一下的时候,我居然发现了这篇文章,既然发现了,就转载一下吧。
正如作者最后说的一样,别重复犯他犯过的错误, ^_^

=================================================
Top Ten Reasons not to use the C shell
=================================================

Written by Bruce Barnett
With MAJOR help from
Peter Samuelson
Chris F.A. Johnson
Jesse Silverman
and of course Tom Christiansen

Updated:
September 22, 2001
November 26, 2002
July 12, 2004
February 27, 2006
October 3, 2006
January 17. 2007
November 22, 2007

Thanks to the following for finding typo’s etc.

Ed Morton

In the late 80′s, the C shell was the most popular interactive
shell. The Bourne shell was too “bare-bones.” The Korn shell had to
be purchased, and the Bourne Again shell wasn’t created yet.

I’ve used the C shell for years, and on the surface it has a
lot of good points. It has arrays (the Bourne shell only has one). It
has test(1), basename(1) and expr(1) built-in, while the Bourne shell
needed external programs. UNIX was hard enough to learn, and spending
months to learn two shells seemed silly when the C shell seemed
adequate for the job. So many have decided that since they were using
the C shell for their interactive session, why not use it for writing
scripts.

THIS IS A *BIG* MISTAKE.

Oh – it’s okay for a 5-line script. The world isn’t going to
end if you use it. However, many of the posters on USENET treat it as
such. I’ve used the C shell for very large scripts and it worked fine
in most cases. There are ugly parts, and work-arounds. But as your
script grows in sophistication, you will need more work-arounds and
eventually you will find yourself bashing your head against a wall
trying to work around the problem.

I know of many people who have read Tom Christiansen’s essay
about the C shell (http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
), and they were not really convinced. A lot of Tom’s examples were
really obscure, and frankly I’ve always felt Tom’s argument wasn’t as
convincing as it could be. So I decided to write my own version of
this essay – as a gentle argument to a current C shell programmer from
a former C shell fan.

[Note – since I compare shells, it can be confusing. If the line starts
with a “%” then I’m using the C shell. If in starts with a “$” then it
is the Bourne shell.

————————————-
Top Ten reasons not to use the C shell
————————————-

1. The Ad Hoc Parser
2. Multiple-line quoting difficult
3. Quoting can be confusing and inconsistent
4. If/while/foreach/read cannot use redirection
5. Getting input a line at a time
6. Aliases are line oriented
7. Limited file I/O redirection
8. Poor management of signals and sub-processes
9. Fewer ways to test for missing variables
10. Inconsistent use of variables and commands.

Read more…

Categories: 脚本语言 Tags:

Linux Bash 测试和比较函数

January 16th, 2008 ricky.zhu No comments

本文系转载, 最近写了一些小而实用的脚本,经常用到测试和比较函数,所以转载一下,原始出处是ibm的网站

Bash shell 在当今的许多 Linux® 和 UNIX® 系统上都可使用,是 Linux 上常见的默认 shell。Bash 包含强大的编程功能,其中包括丰富的可测试文件类型和属性的函数,以及在多数编程语言中可以使用的算术和字符串比较函数。理解不同的测试并认识到 shell 还能把一些操作符解释成 shell 元字符,是成为高级 shell 用户的重要一步。这篇文章摘自 developerWorks 教程 LPI 102 考试准备,主题 109: Shell、脚本、编程和编译,介绍了如何理解和使用 Bash shell 的测试和比较操作。

这个技巧解释了 shell 测试和比较函数,演示了如何向 shell 添加编程功能。您可能已经看到过使用 && 和 || 操作符的简单 shell 逻辑,它允许您根据前一条命令的退出状态(正确退出或伴随错误退出)而执行后一条命令。在这个技巧中,将看到如何把这些基本的技术扩展成更复杂的 shell 编程。

测试

在任何一种编程语言中,学习了如何给变量分配值和传递参数之后,都需要测试这些值和参数。在 shell 中,测试会设置返回的状态,这与其他命令执行的功能相同。实际上,test 是个内置命令!

test 和 [

内置命令 test 根据表达式expr 求值的结果返回 0(真)或 1(假)。也可以使用方括号:test expr 和 [ expr ] 是等价的。 可以用 $? 检查返回值;可以使用 && 和 || 操作返回值;也可以用本技巧后面介绍的各种条件结构测试返回值。
Read more…

Categories: 脚本语言 Tags:

Crontab还可以这样写

January 8th, 2008 ricky.zhu 3 comments

crontab类似于windows下面的计划任务,是用来定时执行程序的,我也一直习惯于把一些需要经常关心的脚本放入crontab中去执行,并监控起来,比如磁盘空间是否快满了,机器是否reboot过等等,crontab的用法很简单,有几个常用的命令,如下:

crontab -l 显示当前crontab中的任务
crontab -e 编辑当前crontab中的任务
crontab file,重新导入新的任务

我一般的做法就是首先把当前的任务导出到一个文件中,然后vi修改之后,在导入,比如:
crontab -l > myfile
#edit my file here, add my own task
crontab myfile

注意,这里crontab的操作都需要是root用户来执行。
多谢Ningoo地提醒,这里不一定是root用户去执行

crontab文件的格式大概是:

30 * * * * xxxx.script

其中每个星号的意义如下:
minute (0-59),
hour (0-23),
day of the month (1-31),
month of the year (1-12),
day of the week (0-6 with 0=Sunday).

今天发现居然crontab还可以这样写
@hourly /usr/local/www/awstats/cgi-bin/awstats.sh
使用 @hourly 對應的是 0 * * * *, 還有下述可以使用:

string meaning
—— ——-
@reboot Run once, at startup.
@yearly Run once a year, “0 0 1 1 *”.
@annually (same as @yearly)
@monthly Run once a month, “0 0 1 * *”.
@weekly Run once a week, “0 0 * * 0″.
@daily Run once a day, “0 0 * * *”.
@midnight (same as @daily)
@hourly Run once an hour, “0 * * * *”.

特別是看到 @reboot, 所以寫 rc.local 的東西, 也可以使用 @reboot 寫在 crontab 裡面?
受教了,以后有多了一种选择。

Categories: 脚本语言 Tags: