【shell】循环测试脚本|历史脚本记录|启动OSD脚本
目录
获取device name、ib_port、gid index 、mtu的脚本
自动根据ceph.conf 中的public和cluster IP 配置RDMA的参数
启动OSD脚本
#ps -ef | grep fio | grep -v grep | awk '{print $2}' | xargs kill -s 9
rm -rf /var/log/ceph/ceph-osd.*.log
rm -rf /var/run/ceph/ceph-adm*
rm -rf /var/run/ceph/ceph-osd.*if [ -z $1 ] ; then
echo "******************"
echo "Please input host number:"
echo "61? 62? 63? ……"
echo "******************"
exit
else
HOST_NUM=$1
echo "Start host${HOST_NUM}'s osd demon."
fi
CEPH_OSD=/opt/h3c/bin/ceph-osd
ceph osd tree|awk -v flag=0 '
/host rdma'$HOST_NUM'/ {flag=1; next}
flag == 1 && /host rdma/ {flage=0; exit}
flag == 1 {${CEPH_OSD} --cluster=ceph -i $1 -f --setuser ceph --setgroup ceph > "osd"$1".log" 2>&1 &}'ps -aux|grep ceph-osd
echo "total is:"`ps -aux|grep ceph-osd|grep -v 'grep'|wc -l`
循环测试脚本
#!/bin/bash
#说明函数定义脚本#***************************************************************
#函数 status_check
#判断程序$1是否已经在运行 $1:第一个参数
#***************************************************************
function status_check()
{pids=`ps aux|grep -w ${1}|grep -v grep|awk '{print $2}'|head -1` #获取${1}进程号grep -w ${1}:匹配第一个参数单词的行;grep -v grep:去掉含grep的行,awk '{print $2}':取行的第二列(进程号),head -1:(多个同名进程时)只取第一行
#pids=pidof ${1}|awk '{print $1}' #获取${1}进程号,awk '{print $1}' 有多个同名的进程时,取第一个(第一列)
if [ ${pids} ]then
echo ${1}' running'
return 1else
echo ${1}' not running'
return 2
fi}
#
#***************************************************************
#函数 stop()
#停止$1
#***************************************************************
function stop()
{
TRY=1
while true; do
status_check ${1}
if [ $? -ne 1 ] #-ne:不等于then
echo ${1}' stoped'break
elseps aux|grep -w ${1}|grep -v grep|awk '{print $2}'|xargs kill -15 #-w ${1} 仅完全匹配字词${1}(既参数1),grep -v grep”是在列出的进程中去除含有关键字“grep”的进程,awk '{print $2}'取行的第二列(进程号),
#“xargs kill -15”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -15”命令的参数,并执行该命令。kill发信号-15给进程,进程释放资源后退出,发-9给进程则强制退出
echo ' stop ' ${1}fi
sleep 5
done}
#--------------------------------------------------------------------------
TEST_NUM=3
ONE_RESULT="./test_one_relt.txt"
RESULTS="./test_relt.txt"
FIND_STR="Total op"
CHECK_INTERVAL=5
waittime=0for((i=1;i<=${TEST_NUM};i++))
do
waittime=0
echo "现在时间:`date '+%Y-%m-%d %H:%M:%S'`"
echo "NO.${i} test start."rm -rf ${ONE_RESULT}
./ceph_perf_msgr_client 172.17.31.54:12345 1 1 100 10 40960 &> ${ONE_RESULT} &
while [[ `grep -c "${FIND_STR}" ${ONE_RESULT}` -eq '0' ]]; doecho "Test not finished,continue to wait. ${waittime}s"
sleep ${CHECK_INTERVAL}
((waittime+=${CHECK_INTERVAL}))
done
echo "NO.${i} test finished."
cat ${ONE_RESULT} >> ${RESULTS}
stop ceph_perf_msgr_client
echo " "
echo " "done
cat ${RESULTS} |grep 'run time'
((i--))
echo "${i}/${TEST_NUM} Test finished."
获取device name、ib_port、gid index 、mtu的脚本
获取intel or mellonx 的device name、ib_port、gid index 、mtu的脚本
用法:get-rdma-device-info eth0
#!/bin/bash
g_vendor=""
g_hexip=""
g_nic_name=""
MY_UUID=$(cat /proc/sys/kernel/random/uuid)
MY_TMP_FILE_PATH=/tmp/${MY_UUID}.txt
MY_LOG_FILE_PATH=/var/ceph_osd_get_rdma_info.log
#mellonx or intel
function set_vendor()
{
vendor_id=`ibv_devinfo|grep "vendor_id"|awk 'NR==1 {print $2}'`
if [[ "0x8086" = ${vendor_id} ]]; then
g_vendor="INTEL"
elif [[ "0x02c9" = ${vendor_id} ]]; then
g_vendor="MELLONX"
else
echo "unknown rdma hca vendor." >>${MY_LOG_FILE_PATH}
exit 1
fi
}
#ip4 to hex ip
function ip4_to_hex()
{
tmpifs=${IFS}
IFS="."
num=0
for str in $1
do
ip[num]=${str}
((num++))
done
g_hexip=`printf "%x%x:%x%x" ${ip[0]} ${ip[1]} ${ip[2]} ${ip[3]}`
IFS=${tmpifs}
}
#intel hca process
#reference:https://downloadmirror.intel.com/30368/eng/README_irdma_1.4.22.txt
function intel_hca()
{
echo "vendor is intel">>${MY_LOG_FILE_PATH}
devices=`ibv_devices|awk 'NR > 2 {print $1}'`
for dev in $devices
do
ifname=`ls /sys/class/infiniband/${dev}/device/net`
if [[ ${g_nic_name} = ${ifname} ]];then
device_name=${dev}
fi
done
ethip=`ip route|grep 'link src'|grep ${g_nic_name}|awk '{print $9}'`
ip4_to_hex ${ethip}
if [ "X${device_name}" != "X" ];then
echo "device_name"=${device_name} >>${MY_TMP_FILE_PATH}
else
echo "get device_name failed">>${MY_LOG_FILE_PATH}
exit 1
fi
for port in ` ls /sys/class/infiniband/${device_name}/ports/`
{
for gidx in `ls /sys/class/infiniband/${device_name}/ports/${port}/gids`
{
hca_hex_ip=`cat /sys/class/infiniband/${device_name}/ports/${port}/gids/${gidx}`
if [[ ${hca_hex_ip} =~ ${g_hexip} ]];then
gid_index=${gidx}
ib_port=${port}
fi
}
}
if [ "X${gid_index}" != "X" ];then
echo "gid_index"=${gid_index} >>${MY_TMP_FILE_PATH}
else
echo "get gid_index failed">>${MY_LOG_FILE_PATH}
exit 1
fi
if [ "X${ib_port}" != "X" ];then
echo "ib_port"=${ib_port} >>${MY_TMP_FILE_PATH}
else
echo "get ib_port failed">>${MY_LOG_FILE_PATH}
exit 1
fi
mtu_index=`ibv_devinfo|grep -A 17 ${device_name} |grep active_mtu|awk '{print $3}'|awk -F "[()]" '{print $2}'`
if [ "X${mtu_index}" != "X" ];then
echo "mtu_index"=${mtu_index} >>${MY_TMP_FILE_PATH}
else
echo "get mtu_index failed">>${MY_LOG_FILE_PATH}
exit 1
fi
}
#mellonx hca process
#ibdev2netdev、show_gids
function mellonx_hca()
{
echo "vendor is mellonx">>${MY_LOG_FILE_PATH}
device_name=`ibdev2netdev | grep -w ${g_nic_name} | awk -F ' ' '{print $1}'`
if [ "X$device_name" != "X" ];then
echo "device_name"=${device_name} >>${MY_TMP_FILE_PATH}
else
echo "get device_name failed">>${MY_LOG_FILE_PATH}
exit 1
fi
gid_index=`show_gids | grep -w ${device_name} |grep -w "v2"| awk -F ' ' '$5 !="" {print $3}'|head -n 1`
if [ "X${gid_index}" != "X" ];then
echo "gid_index"=${gid_index} >>${MY_TMP_FILE_PATH}
else
echo "get gid_index failed">>${MY_LOG_FILE_PATH}
exit 1
fi
ib_port=`show_gids | grep -w ${device_name} |grep -w "v2"| awk -F ' ' '$5 !="" {print $2}'|head -n 1`
if [ "X${ib_port}" != "X" ];then
echo "ib_port"=${ib_port} >>${MY_TMP_FILE_PATH}
else
echo "get ib_port failed">>${MY_LOG_FILE_PATH}
exit 1
fi
mtu_index=`ibv_devinfo|grep -A 17 ${device_name} |grep active_mtu|awk '{print $3}'|awk -F "[()]" '{print $2}'`
if [ "X${mtu_index}" != "X" ];then
echo "mtu_index"=${mtu_index} >>${MY_TMP_FILE_PATH}
else
echo "get mtu_index failed">>${MY_LOG_FILE_PATH}
exit 1
fi
}
#====================================================================
#start shell
#====================================================================
echo "input interface name is:$1">${MY_LOG_FILE_PATH}
if [ "X$1" == "X" ]; then
echo "interface is not specific,excample:$0 eth0">>${MY_LOG_FILE_PATH}
exit 1
fi
g_nic_name=$1
is_virtual=`ls -l /sys/class/net/ | grep " $g_nic_name " | grep "\/virtual\/net\/" | wc -l`
if [ $is_virtual -ne 0 ]; then
g_nic_name=`echo $g_nic_name | awk -F "." 'OFS="." {$NF="" ;print $0}' | sed 's/.$//'`
fi
set_vendor
if [[ "INTEL" = ${g_vendor} ]]; then
intel_hca
elif [[ "MELLONX" = ${g_vendor} ]]; then
mellonx_hca
else
echo "Unable to determine the vendor. exit 1">>${MY_LOG_FILE_PATH}
exit 1
fi
cat ${MY_TMP_FILE_PATH}
rm -f ${MY_TMP_FILE_PATH}
读取某个 device的信息,获取里面某些参量的数值
#/bin/bash
set -e
function get_param()
{
if [[ ${1} =~ "max_sge:" ]];then
echo "in here"
max_sge=`echo ${1}|awk '{print $2}'`
elif [[ ${1} =~ "sm_lid:" ]];then
sm_lid=`echo ${1}|awk '{print $2}'`
elif [[ ${1} =~ "max_srq:" ]];then
max_srq=`echo ${1}|awk '{print $2}'`
echo "in here2"
fi
}
#----------------main--------------------------
devicename=${1} #hca_id
foundflag=0
MY_UUID=$(cat /proc/sys/kernel/random/uuid)
MY_TMP_FILE_PATH=/tmp/${MY_UUID}.txt
if [ "X${devicename}" == "X" ];then
echo "Please input provide devicename!"
exit 1
fi
ibv_devinfo >> ${MY_TMP_FILE_PATH}
while read line
do
if [[ "${line}" =~ "${devicename}" ]];then
foundflag=1
elif [[ ${foundflag} -eq 1 ]] && [[ "${line}" =~ "hca_id:" ]];then
echo "finish"
break
fi
if [[ ${foundflag} -eq 1 ]];then
get_param "$line"
fi
done < ${MY_TMP_FILE_PATH}
echo "max_sge=" $max_sge
echo "max_srq=" $max_srq
echo "sm_lid=" $sm_lid
自动根据ceph.conf 中的public和cluster IP 配置RDMA的参数
ms_async_rdma_devicename_portnum_gidindex_use_config = true
ms_async_rdma_front_device_name =
ms_async_rdma_back_device_name =
ms_async_rdma_front_device_port_num = 1
ms_async_rdma_back_device_port_num = 1
ms_async_rdma_front_device_gid_idx =
ms_async_rdma_back_device_gid_idx =
#/bin/bash
set -e
CEPH_CONF=/etc/ceph/ceph.conf
g_vendor="" #vendor intel or mellonx
g_hexip="" #ip in hex
#g_nic_name=$1 #netwok interface
device_name="" #rdma device name
#mellonx or intel
function get_vendor()
{
vendor_id=` ibv_devinfo|grep "vendor_id"|awk 'NR==1 {print $2}'`
if [[ "0x8086" = ${vendor_id} ]]; then
g_vendor="INTEL"
echo "vendor is intel"
elif [[ "0x02c9" = ${vendor_id} ]]; then
g_vendor="MELLONX"
echo "vendor is mellonx"
else
echo "unknown rdma hca vendor."
exit 1
fi
}
#ip4 to hex ip
function ip4_to_hex()
{
tmpifs=${IFS}
IFS="."
ipf1=`echo $1|awk '{print $1}'`
ipf2=`echo $1|awk '{print $2}'`
ipf3=`echo $1|awk '{print $3}'`
ipf4=`echo $1|awk '{print $4}'`
IFS=${tmpifs}
g_hexip=`printf "%x%x:%x%x" ${ipf1} ${ipf2} ${ipf3} ${ipf4}`
echo "ip:"$1
echo "hexip:"$g_hexip
}
function get_rdma_device_name()
{
if [[ "INTEL" = ${g_vendor} ]]; then
devices=` ibv_devices| awk 'NR > 2 {print $1}'`
for dev in $devices
do
ifname=` ls /sys/class/infiniband/${dev}/device/net`
if [[ ${g_nic_name} = ${ifname} ]];then
device_name=${dev}
echo "device_name="${device_name} #out put to C++ stream who call this shell
break
fi
done
elif [[ "MELLONX" = ${g_vendor} ]]; then
device_name=` ibdev2netdev | grep -w ${g_nic_name} | awk -F ' ' '{print $1}'`
echo "device_name="${device_name} #out put to C++ stream who call this shell
else
echo "Unable to determine the vendor. exit 1"
exit 1
fi
if [ "X$device_name" == "X" ];then
echo "get device_name failed!Please check whether the provided IP is RDMA interface's IP."
exit 1
fi
}
function get_port_num_and_gid_idx()
{
#ethip=` ip route|grep 'link src'|grep ${g_nic_name}|head -n 1|awk '{print $9}'`
ip4_to_hex ${ethip}
if [[ "INTEL" = ${g_vendor} ]]; then
for port in ` ls /sys/class/infiniband/${device_name}/ports/`
{
for gidx in ` ls /sys/class/infiniband/${device_name}/ports/${port}/gids`
{
hca_hex_ip=` cat /sys/class/infiniband/${device_name}/ports/${port}/gids/${gidx}`
if [[ ${hca_hex_ip} =~ ${g_hexip} ]];then
gid_index=${gidx}
port_num=${port}
echo "gid_index="${gid_index} #out put to C++ stream who call this shell
echo "port_num="${port_num} #out put to C++ stream who call this shell
ip_found=true
break 2
fi
}
}
if [ "true" != $ip_found ];then
echo "get port_num /gid_index failed"
exit 1
fi
elif [[ "MELLONX" = ${g_vendor} ]]; then
gid_index=` show_gids | grep -w ${device_name} |grep -w "v2"|grep ${ethip}| awk '{print $3}'`
port_num=` show_gids | grep -w ${device_name} |grep -w "v2"|grep ${ethip}| awk '{print $2}'`
echo "gid_index="${gid_index} #out put to C++ stream who call this shell
echo "port_num="${port_num} #out put to C++ stream who call this shell
else
echo "Unable to determine the vendor. exit 1"
exit 1
fi
if [ "X${gid_index}" == "X" ];then
echo "get gid_index failed"
exit 1
fi
if [ "X${port_num}" == "X" ];then
echo "get port_num failed"
exit 1
fi
}
function parse_device_global_param()
{
if [[ ${1} =~ "max_sge:" ]];then
max_sge=`echo ${1}|awk '{print $2}'`
echo "max_sge="${max_sge} #out put to C++ stream who call this shell
elif [[ ${1} =~ "max_qp_wr:" ]];then
max_qp_wr=`echo ${1}|awk '{print $2}'`
echo "max_qp_wr="${max_qp_wr}
elif [[ ${1} =~ "max_srq:" ]];then
max_srq=`echo ${1}|awk '{print $2}'`
echo "max_srq="${max_srq} #out put to C++ stream who call this shell
fi
}
function parse_device_port_param()
{
if [[ ${1} =~ "active_mtu:" ]];then
mtu_index=`echo ${1}|awk '{print $3}'|cut -d "(" -f 2|cut -d ")" -f 1 `
echo "mtu_index="${mtu_index} #out put to C++ stream who call this shell
elif [[ ${1} =~ "sm_lid:" ]];then
sm_lid=`echo ${1}|awk '{print $2}'`
echo "sm_lid="${sm_lid} #out put to C++ stream who call this shell
elif [[ ${1} =~ "sm_sl:" ]];then
sm_sl=`echo ${1}|awk '{print $2}'`
echo "sm_sl="${sm_sl} #out put to C++ stream who call this shell
fi
}
#----------------main--------------------------
#====================================main=======================
#must get vendor first
get_vendor
#delete
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/d" $CEPH_CONF
#add xxxx after ms_cluster_type
sed -i "/ms_cluster_type/a\ms_async_rdma_devicename_portnum_gidindex_use_config = true" $CEPH_CONF
#get public rdma info >>>>>>>>>>>>>>>>>>>>>
echo "get public rdma info >>>>>>>>>>>>>>>>>>>>>"
ethip=$(line=`cat $CEPH_CONF |grep -E 'public_addr'`&&echo ${line#*=})
g_nic_name=$(ifconfig | awk -F ":" '/'$ethip'/{print a}{a=$1}')
if [ "X${ethip}" == "X" ];then
echo "Please input provide ip!"
exit 1
fi
#get rdma device name by eth interface
get_rdma_device_name
#get rdma port num and gid index by device name
get_port_num_and_gid_idx
#now we have the device name and port ,so we can get more attr from "ibv_devinfo -v"
#<-------------------------get device's info line
#put devinfo to tmp file1
ibv_devinfo -d ${device_name} -i ${port_num} -v|while read line
do
#get device's global param
parse_device_global_param "$line"
#get device's specify port's param
parse_device_port_param "${line}"
done
#delete
sed -i "/ms_async_rdma_front_device_name/d" $CEPH_CONF
sed -i "/ms_async_rdma_front_device_port_num/d" $CEPH_CONF
sed -i "/ms_async_rdma_front_device_gid_idx/d" $CEPH_CONF
#add xxxx after ms_async_rdma_devicename_portnum_gidindex_use_config
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/a\ms_async_rdma_front_device_name=$device_name" $CEPH_CONF
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/a\ms_async_rdma_front_device_port_num=$port_num" $CEPH_CONF
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/a\ms_async_rdma_front_device_gid_idx=$gid_index" $CEPH_CONF
#get cluster rdma info >>>>>>>>>>>>>>>>>>>>>
echo "get cluster rdma info >>>>>>>>>>>>>>>>>>>>>"
ethip=$(line=`cat /etc/ceph/ceph.conf |grep -E 'cluster_addr'`&&echo ${line#*=})
g_nic_name=$(ifconfig | awk -F ":" '/'$ethip'/{print a}{a=$1}')
if [ "X${ethip}" == "X" ];then
echo "Please input provide ip!"
exit 1
fi
#get rdma device name by eth interface
get_rdma_device_name
#get rdma port num and gid index by device name
get_port_num_and_gid_idx
#now we have the device name and port ,so we can get more attr from "ibv_devinfo -v"
#<-------------------------get device's info line
#put devinfo to tmp file1
ibv_devinfo -d ${device_name} -i ${port_num} -v|while read line
do
#get device's global param
parse_device_global_param "$line"
#get device's specify port's param
parse_device_port_param "${line}"
done
#delete
sed -i "/ms_async_rdma_back_device_name/d" $CEPH_CONF
sed -i "/ms_async_rdma_back_device_port_num/d" $CEPH_CONF
sed -i "/ms_async_rdma_back_device_gid_idx/d" $CEPH_CONF
#add xxxx after ms_async_rdma_devicename_portnum_gidindex_use_config
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/a\ms_async_rdma_back_device_name=$device_name" $CEPH_CONF
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/a\ms_async_rdma_back_device_port_num=$port_num" $CEPH_CONF
sed -i "/ms_async_rdma_devicename_portnum_gidindex_use_config/a\ms_async_rdma_back_device_gid_idx=$gid_index" $CEPH_CONF
echo "get_success=true"
#excample:
#[root@rdma59 perf5.0]# ibv_devinfo
#hca_id: rocep216s0f0
# transport: InfiniBand (0)
# fw_ver: 1.54
# node_guid: b696:91ff:fea5:9a70
# sys_image_guid: b496:91a5:9a70:0000
# vendor_id: 0x8086
# vendor_part_id: 5523
# hw_ver: 0x2
# phys_port_cnt: 1
# port: 1
# state: PORT_ACTIVE (4)
# max_mtu: 4096 (5)
# active_mtu: 1024 (3)
# sm_lid: 0
# port_lid: 1
# port_lmc: 0x00
# link_layer: Ethernet
#
#hca_id: rocep216s0f1
# transport: InfiniBand (0)
# fw_ver: 1.54
# node_guid: b696:91ff:fea5:9a71
# sys_image_guid: b496:91a5:9a71:0000
# vendor_id: 0x8086
# vendor_part_id: 5523
# hw_ver: 0x2
# phys_port_cnt: 1
# port: 1
# state: PORT_DOWN (1 )
# max_mtu: 4096 (5)
# active_mtu: 1024 (3)
# sm_lid: 0
# port_lid: 1
# port_lmc: 0x00
# link_layer: Ethernet
保持空间/删除旧文件
写脚本keepspace.sh然后配置crontab 定时执行。
仿照下面例子在/etc/crontab 中添加定时任务
*/1 * * * * /usr/local/keepspace.sh
因为crontab最小定时单位是1分钟,如果想让脚本每5秒执行一次,则再写脚本crontab.sh,然后配置crontab
#!/bin/bash
step=5for (( i = 0; i < 60; i=(i+step) )); do
echo `/bin/date`" /etc/crontab delete corefile" >> /corefile/delete_recored.txt
/usr/local/bin/keepspace.sh
sleep $step
done
exit 0
*/1 * * * * /usr/local/crontab.sh
keepspace.sh
yuzhi=50
FileDir=/corefilefunction delete()
{ls ${FileDir} -rt |while read line
do
OldFile=${line}
echo $date "Delete File:"$OldFile
rm -rf $FileDir/$OldFile
spaceleft=`df |grep -w "/"|awk '{print $5}'|tr -cd "[0-9]"`;if [ $spaceleft -lt ${yuzhi} ];then
echo "finsh"
break
fi
done}
while :
do
spaceleft=`df |grep -w "/"|awk '{print $5}'|tr -cd "[0-9]"`;if [ $spaceleft -gt ${yuzhi} ];then
echo "big"
deleteelse
echo "small"
breakfi
done
上面的脚本用df,文件系统坏了df可能会卡住,换下面的脚本,比上面的脚本需要配置磁盘总空间totalspace=390 #G
keepspace.sh
yuzhi=50
FileDir=/corefile
totalspace=390 #Gfunction count_used()
{total=0
num=0for line in `ls -lh ${FileDir} |grep "/*G"|awk '{print $5}'`
do
num=$( echo $line|tr -cd "[0-9]")
echo $num
total=$((total+num))
doneused=$((100*total/totalspace))
echo $used}
function delete()
{ls ${FileDir} -rt |while read line
do
OldFile=${line}
echo $date "Delete File:"$OldFile
rm -rf $FileDir/$OldFilecount_used
if [ $used -lt ${yuzhi} ];then
echo "finsh"
break
fi
done}
while :
docount_used
if [ $used -gt ${yuzhi} ];then
echo "need to delete"
#deleteelse
echo "no need to delete"
breakfi
done
多个单元,取某个单元的参数
#<-------------------------get device's info line
#put devinfo to tmp file1
ibv_devinfo -v >> ${MY_TMP_FILE_PATH}#get the specify device ${devicename} devinfo ,put into tmp file2
#the specify device devinfo is between "hca_id:"<-->"hca_id:",or "hca_id:" to the end
founddevice=0
while read line
do
if [[ "${line}" =~ "${devicename}" ]];then
#found the first "hca_id: devicename"
founddevice=1
elif [[ ${founddevice} -eq 1 ]] && [[ "${line}" =~ "hca_id:" ]];then
#found the next by "hca_id: devicenamexx",only the line between "hca_id: devicename"<--->"hca_id: devicenamexx" is what we need
#so end.
echo "finish"
break
fiif [[ ${founddevice} -eq 1 ]];then
echo "$line" >> ${MY_TMP_FILE_PATH2} #put the line between "hca_id: devicename"<--->"hca_id: devicenamexx" into tmp file2
fidone < ${MY_TMP_FILE_PATH}
给用户SDS_Admin做免密登录
#!/bin/bash
#把本机的公钥拷贝到/etc/ceph/ceph.conf 中 all_manage_ip 指定的所以IP的主机上
line=`cat /etc/ceph/ceph.conf |grep -E 'all_manage_ip'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)
OLD_IFS="$IFS" #备份原值
IFS="," #设分隔符为“,”
for ip_addr in ${HOSTS}
do
echo "$ip_addr"
ip_addr=$ip_addr expect << 'EOS'
set timeout 10
spawn ssh-copy-id SDS_Admin@$::env(ip_addr)
expect "*password*"
send "Admin@123stor\r"
expect eof;
#exit 0
EOS
done
IFS="$OLD_IFS"
数组处理
将ls命令返回值存入数组
(转自:https://blog.csdn.net/rgndao/article/details/126752271)
arr=(`ls`)
输出数组个数echo ${#arr[*]}
这个问题一开始困扰到我了,ls的返回值是一个以空格为分隔符的长字符串,而shell里又没有像python一样方便的字符串split操作,没办法一步拆分,难道就只能写在循环里一步步赋值了么?
后来,我偶然发现shell里数组的定义方式为:
array_name=(value1 value2 ... valuen)
参数本身就是以空格分隔的!那不正好,我们只需要在ls返回值的外面套上一层圆括号就好,例如这样
arr=(`ls`)
接下来你可以试试输出arr的元素个数,看是不是成功将目录名存储为了数组
echo ${#arr[*]}
字典和数组
#!/bin/bash
echo "shell定义字典"
#必须先声明
declare -A dic
dic=([key1]="value1" [key2]="value2" [key3]="value3")
#打印指定key的value
echo ${dic["key1"]}
#打印所有key值
echo ${!dic[*]}
#打印所有value
echo ${dic[*]}
#遍历key值
for key in $(echo ${!dic[*]})
do
echo "$key : ${dic[$key]}"
done
echo "shell定义数组"
#数组
list=("value1" "value2" "value3")
#打印指定下标
echo ${list[1]}
#打印所有下标
echo ${!list[*]}
#打印数组下标
echo ${list[*]}
#数组增加一个元素
list=("${list[@]}" "value3")