shell脚本

1、数组

1)数组定义

例:不同文件类型数组定义

fileTypeArray=(txt xml conf out properties rar zip word excel ppt sh log war yml)

2)数组遍历

循环获取数组内容

for fileType in ${fileTypeArray[*]}
do
echo ${fileType}
done

2、for循环

for b in {1..10}
do
outputfilename=`date '+%Y%m%d%H%M%S'"_"$fileSize"k"`.$fileType
dd if=/dev/urandom of=D:\/csfile\/$outputfilename bs=1k count=$fileSize
#每个文件间隔大小100K
let fileSize=$fileSize+100
#文件大小循环次数
let b++
done

3、转义字符

windows路径:D:/csfile/ 使用时需加上转义字符D:/csfile/

4、数值格式转换

转换数值变量a为四位数输出

format=`printf "%04d" $a`

5、查找替换

替换“=”后面的值
spring.profiles.active=0023

sed -i "s#spring.profiles.active=.*#spring.profiles.active=${format}#g" ${propPath}

6、写文件

直接写文件内容

cat <<EOF >${testngPath}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <classes>
            <class name="com.dragoninfo.unittest.duceap.testng.datasource.TcDtsjyTest${format}"/>          
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->
EOF

拷贝文件

cat ${dacmpomPath} > ${pomPath}

7、打印

echo "重写文件"

8、条件判断语句

if [[ ${format} == 0003 || ${format} == 0004 || ${format} == 0008 || ${format} == 0012 ]]
then
cat ${dacmpomPath} > ${pomPath}
echo "无引入dacm包,重写pom.xml完成"
else
cat ${nondacmpomPath} > ${pomPath}
echo "引入dacm包,重写pom.xml完成"
fi

1、if ...then... 条件判断为真,然后执行命令

if如果,条件判断结果为真,then那么执行command命令,fi表示判断语句结束。

语法:

if [ 条件 ];then

command

fi

2、if...then...else...如果条件判断为真,执行then后面的命令,否则else,执行else后的命令

3、多条件判断

if ...then...elif...then...fi

if [ 条件 ];then

command

elif [ 条件 ];then

command

elif [ 条件 ];then

command

else

command

fi

9、jmeter自动化执行用例脚本


#!/bin/bash

#变量i指线程组名:1代表查询数广服务10K;2代表查询数广服务100K;3代表查询数广服务1M;4代表查询理想服务10K;5代表查询理想服务100K;6代表查询理想服务1M;循环1~6分别测试不同的并发数

currenttime=`date +%Y%m%d%H`

formate="000"

for i in {1..6}

do

#变量a指并发数,这里为了便于循环会将变量值拼接成1000

for a in {1..6}

do

vuser=${a}${formate}

jmeter -n -Jthreads${i}=${vuser} -t /opt/sss/scripts/cxfwjk.jmx -l /opt/sss/results/threads${i}_${vuser}user_${currenttime}.jtl -e -o /opt/sss/results/threads${i}_${vuser}user_${currenttime}/ -j /opt/sss/results/threads${i}_${vuser}user_${currenttime}.log

echo 线程组${i} 并发数${vuser} 执行完成

let a++

done

let i++

done

原文地址:https://www.cnblogs.com/seamy/p/15638911.html