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
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文件条目的一些例子:https://www.cnblogs.com/cocowool/archive/2009/04/22/1441291.html
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 常用命令: grep/find/cut 命令:
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
14 trap catch signal
trap "doCleanup" HUP INT QUIT TERM KILL EXIT
doCleanup(){
typeset PS4; PS4='doCleanup[$LINENO]+ '
# don't interrupt cleanup
trap "print $ME: interrupt ignored" HUP INT QUIT TERM KILL EXIT
ReserveLog=/opt/cool/.logs/aps/ids/$(hostname -s).${APID}.$($LIB/username)
rm -f $ReserveLog
fi
15 统计user
#!/bin/ksh
logpath=/home/felixzh
execpath=/opt/cool/.logs
#redirect stdout/stderr to file
exec 2>&1
exec 1> /home/felixzh/$1
#use function
function readfile
{
filename=$1
y=$2
>$logpath/$y.name
while read line1
do
#use nawk to split line
user=`echo $line1|nawk -F ';' '{print $1}'`
#use >/dev/null or -q ,stop result output to files
grep $user $logpath/$y.name >/dev/null
#grep -q $user $logpath/$y.name
if [ $? != 0 ]
then
echo $user >>$logpath/$y.name
fi
done <$filename
#static lines for file
i=`wc -l $execpath/$filename`
j=`wc -l $logpath/$y.name`
echo "i=$i"
echo "j=$j"
}
function usage
{
echo "./static 2014"
}
if [ $# -lt 1 ]
then
usage
exit
fi
cd $execpath
ls -ltr use* >$logpath/1
year=$1
while read line
do
time=`echo $line |nawk '{print $8}'`
if [ "$time" == "$year" ]
then
name=`echo $line |nawk '{print $9}'`
readfile $name $year
fi
done <$logpath/1
16 比较全的shell资料: http://bbs.chinaunix.net/thread-1776727-1-1.html