您的位置:首页 > 运维架构 > Shell

常用shell命令:rsh-crontab-email-ed-tty--ymatch$

2013-05-19 17:31 417 查看
tips:

shell 思路基本都是一条命令实现。批处理。不用for循环。而不是一行一行的读取。不用初始化

用NF控制多个文件or 用—F等分割符号

1 : rsh

rsh 居然可以多个命令在一条语句中实现。只需要用;号将其分开。

timedrun -${G_MAX_TIMEOUT} /bin/rsh -l $r_userID $r_Host ". /opt/wsde/1/bin/setenv -S $NMAKE_SYSTEM -g $GENERIC -l $LOADID -F -E -b $BLDTYPE -r $r_ROOT > /dev/null 2>&1;\

cd $r_ROOT;tgetversion ${_mr} ${_g} ${_rm} ${_mkdir} ${_dep} ${_check} ${_restrict} 2>&1 && print 0 || print 1" > ${_OUT_FILE} 2>&1

11 [[]]、[]都是用来测试的,[[]]是关键字,里面可以用&&、||、<、>等类似C语言的语法;[]是bash内嵌的命令,也是用于测试的。[[]]通用性更好些。

(()),类似let,用于计算表达式。()可以把多个命令括起来,在一个子shell运行。

2: crontab

crontab -e edit . crontab -l :list crontab -r:delete

第1列分钟1~59 第2列小时1~23(0表示子夜) 第3列日1~31 第4列月1~12 第5列星期0~6(0表示星期天) 第6列要运行的命令

下面是crontab的格式:

分 时 日 月 星期 要运行的命令

这里有crontab文件条目的一些例子:

30 21 * * * /usr/local/apache/bin/apachectl restart

上面的例子表示每晚的21:30重启apache。

45 4 1,10,22 * * /usr/local/apache/bin/apachectl restart

上面的例子表示每月1、10、22日的4 : 45重启apache。

10 1 * * 6,0 /usr/local/apache/bin/apachectl restart

上面的例子表示每周六、周日的1 : 10重启apache。

0,30 18-23 * * * /usr/local/apache/bin/apachectl restart

上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

0 23 * * 6 /usr/local/apache/bin/apachectl restart

上面的例子表示每星期六的11 : 00 pm重启apache。

* *1 * * * /usr/local/apache/bin/apachectl restart

每一小时重启apache

* 23-7/1 * * * /usr/local/apache/bin/apachectl restart

晚上11点到早上7点之间,每隔一小时重启apache

0 11 4 * mon-wed /usr/local/apache/bin/apachectl restart

每月的4号与每周一到周三的11点重启apache

0 4 1 jan * /usr/local/apache/bin/apachectl restart

一月一号的4点重启apache

how to debug crontab :

some crontab have issue .but when you run "/opt/..../clean.active.cron -s" ,it is well .

because the enviroment is different . so you need add trace in crontab .

You could change the cron the invoke it with ksh -x /opt/..../clean.active.cron -s 2>/home/cool/cron/cron.trace

That would show you what happens during the cron run, which may be different from the command line because the environment is different.

3 email

shell 格式化输出,用管道送给email

printf "\n %-20s \n \n %-20s \n \n %-20s \n \n \n %-20s \n %-20s" "Failed to bring up $load." "Detail:$n, return value $x."

"You can look into the log $m and the code in for more detail." "--" "Team" | /bin/mailx -rabc@126.com -s "Bring up $load" efg@126.com gfh@qq.com
2>/dev/null

收件人为多个

4 stty -echo 表示输入可以看见,stty echo 表示输入不显示

stty -echo

echo "123"

read aa

echo $aa

---

stty echo

echo "123"

read aa

echo $aa

5 ed 相当于 实际上是全文拷贝到内存做镜象,通过编辑镜象和回写保存整个文件

ed可以操作无名字的临时文件

send "EDITOR=ed\r";

send "a\r"; #put in append mode

#this is the read file from command line from user

send "My dummy MR descritption\r" ; #enter test

send "$ext\r"; #end text

send ".\r" ;#end text

send "w\r"; #write file

send "q\r"; #quit 'ed'

6

TIMOUT=${1:-40}---------------------------if $1 is not empty ,TIMEOUT=$1; otherwise TIMEOUT=40

next means continue:

seq 1 10| awk 'NR==5{next}{print $1}'

if [ "$1" != "" ]

如果用 {$1 != ""} 当$1 为空的时候会产生 {!=""} 的语法错误

所以shell的判断string 要用加引号

7

shell 中for用法

for 默认是按空白符分隔

#!/bin/ksh

a="1 3 5 7 9"

for index in $a

do

print "$index"

done

如果是其他的分隔符,先替换成space,在循环

8 shell的分割

b= a当成命令

ksh: a: not found

metacharacter

A character that, when unquoted, separates words. One of the following:

| & ; ( ) < > space tab

空格是meta字符,所以对于b= a,bash会分成“b=”和"a"两部分

9

oldIFS=$IFS

IFS=":"

set -- $(print - "${@}")

IFS=$oldIFS

--A -- signals the end of options and disables further option processing.

Any arguments after the -- are treated as filenames and arguments.

An argument of - is equivalent to --.

10 比较全的shell资料

http://bbs.chinaunix.net/thread-1776727-1-1.html

11 set -o vi (use in term)

12

xterm -e a.o

cat a.c

while (1) { sleep (1) printf ("1\n") };

用xterm run a.out程序的结果

13 ypmatch coolap31 hosts
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: