使用场景:当有多个java任务需要有序运行时,比如要做一组实验,每组实验的入参不一样。可以写一个java任务的列表,再由shell脚本逐个执行。
原理:通过shell里轮询判断当前任务是否完成,如果完成执行下一个任务。
使用方式: ./runBatch.sh tasklist
#!/bin/bash
#####根据一个java任务列表,串行的执行一系列的java任务#####
if [ "$1" = "" ] ; then
echo "Bad command. No tasklist file been appointed. Right format: $0 [tasklist.txt]"
exit 1
fi
tasks=`cat $1|wc -l`
echo "total tasks founded:$tasks"
echo "Now begin running....."
cat $1|while read line
do
tasks=`jps|wc -l`
#轮询
while [ $tasks -gt 1 ]
do
sleep 20
echo "task is running"
tasks=`jps|wc -l`
done
$line &
echo "running $line"
sleep 5
done
echo "end."
分析cookielog,统计出某ip访问某URL最频繁的列表等。
#!/bin/bash
####分析cookielog,统计出某ip访问某URL最频繁的列表,倒序排#####
if [ "$1" = "" ] ; then
echo "Bad command. No cookie file been appointed. Right format: $0 [cookie_file] [output_file]"
exit 1
fi
awk '{print $1 $8}' $1| awk -F "?" '{print $1}'|sort|uniq -c|sort -nro $2
exit 0
#!/bin/bash
if [ "$1" = "" ] ; then
echo "Bad command. No cookie file been appointed. Right format: $0 [cookie_file] [output_file]"
exit 1
fi
awk -F "\"" '{print $2}' $1 | awk '{print $2}' | awk -F "?" '{print $1}'|sort|uniq -c|sort -dro $2
exit 0
使用场景:代码重构是往往会有一些需求批量往一批文件的某个特定位置插入一段代码或替换一段代码。
#!/bin/bash
###将每个tmp2文件列表里每个类文件import区域里加入"import org.springframework.stereotype.Component;",类头加上注解"@Component"
cat tmp2|while read line
do
if grep "@Component" $line
then
echo "$line has add @component"
else
n1=`grep -n "import\s" $line|tail -1|awk -F ":" '{print $1}'`
n2=$[ $n1 + 1 ]
echo $n2
gsed -i ''$n2'i import org.springframework.stereotype.Component;' $line
n3=`grep -n "public\sclass" $line|head -1|awk -F ":" '{print $1}'`
gsed -i ''$n3'i @Component' $line
fi
done
如输入:
A 222
B 33
C 33
A 23
D 34
B 34
要求对第一列进行分类,得到第二列的平均值和最大值。 awk脚本如下:
awk '{A[$1]++;SUM[$1]+=$2;MAX[$1]=($2>MAX[$1]?$2:MAX[$1])}
END {for (service in A) print SUM[service]/A[service],service,MAX[service],A[service]}' file|sort -n