shell example

1 example 1:

remote visitor servers

==
$ cat rsh.sh

pool=$1
echo $pool
while read line
do
        # Skip comments
        if [ "${line%%\#*}" = "" ]
        then
                continue
        fi
        # Skip blank
        if [ "${line%%\b*}" = "" ]
        then
                continue
        fi
#$() to get output 
server=$(echo $line|awk '{print $1}')
echo "server=$server"
r=$(rsh -n $server "[ -d /opt/sc12 ];echo \$?")
echo "r=$r"
done<$pool
rsh $server /bld/xyz/1 (rsh call remote server script to run )

2 example 2:computer average

NF number of field ; NR number of row

下面1,shell自动每行读入,等于在外层自动有一层循环。所以里面在加一层等于双重循环了。所以多加了一遍
nawk '{ for (i=0;i<NF;i++) {a[$1]=a[$1]+$2;b[$1]++};print $1,a[$1],b[$1]} END {for (i in a) print i,a[i],b[i]}' 1-2.txt
nawk '{a[$1]=a[$1]+$2;b[$1]++}END {for (i in a) print i,a[i]/b[i]}' 1-2.txt
 有两列数据,格式如下:

  1. a 1
  2. b 3
  3. c 8
  4. d 5
  5. e 5
  6. c 4
  7. e 2
  8. a 3

其中a、c、e重复出现,我想对第一列重复出现的量取平均值,结果应如下:

  1. a 2
  2. b 3
  3. c 6
  4. d 5
  5. e 3.5

other method:

awk '{a[$1]+=$2;b[$1]++}END{for (i in a)print i,a[i]/b[i]}'

-----------------------------------------------------------------------------------

3 example 3

shell 调用tcl,即调用./1.tcl 整体

shell is a language

awk and sed are shell command,but they can do program. they had their syntax

cut ,head ,tail ,grep , find ... are simple comand

4 shell自动交互

ed行编辑器vi全屏幕编辑器程序默认用$EDITOR file交互改成手动使用的时候,(a-append; wq 存盘退出)

所以不是vi这种visual 可视化的。所以可以自动化交互detailed description? (multiple-line entry, you are in edit)

ed 的这种功能可以用于expect的自动化,而vi不可以

0

a

123

. w

q

5 shell for 循环

遍历数组

for ((i=0;i<100;i=i+1))
do
     cp center1.tar center$i.tar
done
for data in ${array[@]}  
do  
    echo ${data}  
done 

 6 检查shell 特殊字符

#!/bin/bash

if [ $# -gt 0 ]; then
        echo "参数个数为$#个"
	echo "参数1 is $1"
else
        echo "没有参数"
fi

7 trim content between begin and end lines or get from keyword line to file end

#!/bin/bash
file=$1
if [ "$2" = "" ]
then
    #get all keyword lines 
        a=`sed -n '/id=call_request2rs type=COMMAND/=' $file`
        #separator is \n ; IFS='\n';"\n";\r\n etc are wrong   #it waste many hours
        IFS=$'\n'
        #change string to array
        ary=($a)
#for key in "${!ary[@]}"
#do
#       echo "$key;${ary[$key]}"
#done
        #from before id=call_request2rs type=COMMAND line begin
        #${} is batch to do all; #ary[*] array length;
        k=${ary[${#ary[*]}-1]}
        let begin=$k-1
else
        begin=$2
fi
if [ "$3" = "" ]
then
        end=`cat $file|wc -l`
else
        end=$3
fi
echo "begin=$begin;end=$end"


#sed use shell variable ,not sed variable , need change '' to "" ; not use sed -n ''p   ; #it waste me many hours
sed -n "$begin,$end"p $file >$file.out
#perl -ne 'print if 22..33' $file >$file.out

9 skip # and blank line read file

check hang process in server

file=$1
while read line
do
        echo "line5:$line"
        #skip comments
        if [ "${line%%\#*}" == "" ]
        then
                continue
        fi

        #skip blank
        if [ "${line%%\b*}" == "" ]
        then
                continue
        fi

        server=`echo $line|nawk '{ print $1 }'`
        echo "line20:$server"
        num=`rsh -n $server ps -ef |grep tweet |wc -l`------------------注意此处要用-n,否则rsh会自动读取下一行,造成1.txt读取错误
                if [ $num -ne 1 ]
                then
                        echo  "hang up process:$server"
                fi
done<$file

rsh cool0081 ps -fu tweet>1.txt
rsh cool0081 kill -9 1
while read line
do
        echo $line |grep ps
        if [ $? -eq 1 ]
        then
                echo "$?:$line"
        else
                echo "$?:$line"
        fi
        if [ $? -ne 1 ]
        then
               num=`echo $line |nawk '{print $3}'`
               echo $num
               rsh -n cool0081 kill -9 $num
        fi
done <1.txt

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值