Shell 数组

来源:互联网 发布:蔡和森 知乎 编辑:程序博客网 时间:2024/06/06 19:15

1.一般我们写脚本传递参数都是通过$1~$n 去传递给脚本,那么怎么传递数组元素函数(贴一下我之前写过minicom 串口log的代码):
+#start capture serial log
+#kvm and xen environment
+start_capture_serial_log()
+{
+ local minicom_host_serial_log=$1
+ local ret=0
+ local exp_cmd=cat << EOF
+ eval spawn ssh -t ${MINICOM_SERVER} "minicom $(hostname) -C ${minicom_host_serial_log}"
+ set timeout -1
+ expect {
+ "*assword:" { send "123456\r"; exp_continue}
+ "*(yes/no)?" { send "yes\r"; exp_continue }
+ eof { exit [ lindex [wait] 3 ] }
+ }
+ EOF

+ ping -c 1 ${MINICOM_SERVER}|| ret=1
+if [ $ret -eq 1 ];then
+ vmm_infoline $VMM_INFO "can not ping ${MINICOM_SERVER}"
+ return 1
+ fi
+ VMM_SSH ${MINICOM_SERVER} "ls /etc/|grep minirc\.$(hostname)" || ret=1
+ if [ $ret -eq 1 ];then
+ vmm_infoline $VMM_INFO "can not find minirc $(hostname) configure file"
+ return 1
+ fi
+ ps -ef|grep -E "minicom\s
hostname" || ret=1
+ if [ $ret -eq 1 ];then #judge init environment
+ VMM_SSH ${MINICOM_SERVER} "ps -ef|grep minicom |grep
hostname| grep -v bash" 10
+ if [ $? -eq 0 ];then
+ vmm_infoline $VMM_INFO "You should kill minicom pid at vt-sa3 platform"
+ return 1
+ fi
+ else #judge many guest
+ kill -9 $(ps -ef|grep -E "minicom\s`hostname`"|awk '{print $2}') || ret=1
+ if [ $ret -eq 1 ];then
+ vmm_infoline $VMM_INFO "kill minicom pid Fail"
+ return 1
+ fi
+ sleep 10
+ fi
+ expect -c "${exp_cmd}" &
+ return 0
+}
+
+#public :check all serial log through keyword
+check_error_message()
+{
+ **local search_keyword=("${!1}")**
+ for i in ${!search_keyword[@]}
+ do
+ case $i in
+ 0)
+ sed -n "/${search_keyword[$i]}/,/end[^$]trace/p" $2 > $3
+ ;;
+ esac
+ done

+ if [ wc -c $3 | awk '{print $1}' -gt 0 ];then
+ dos2unix -f $3
+ vmm_infoline $VMM_INFO "
cat $3"
+ return 1
+ fi
+ return 0
+}
+
+#check kvm or xen host serial log
+check_host_serial_log()
+{

+ local journal_name=cat ${XVS_DATA}/var/journalname
+ local serial_log_path=dirname ${journal_name}
+ local minicom_host_serial_log
+ local host_serial_log_name
+ local serial_error_log
+ local ret=0
+ case $XVS_VMM in
+ 0|2)
+ host_serial_log_name=host_serial_${1}.log
+ minicom_host_serial_log=/home/serial/${host_serial_log_name}
+ serial_error_log=${serial_log_path}/serial_error_host.log
+ ;;
+ 1|3)

+ local guest_name=echo $1 | awk -F "tmp-img_" '{print $NF}'
+ host_serial_log_name=host_serial_${guest_name}.log
+ minicom_host_serial_log=/home/serial/${host_serial_log_name}
+ serial_error_log=${serial_log_path}/serial_error_host.log
+ ;;
+ *) exit
+ ;;
+ esac
+ ps -ef|grep -E "minicom\s
hostname"
+ if [ $? -eq 0 ];then
+ kill -9 $(ps -ef|grep -E "minicom\s`hostname`"|awk '{print $2}') || ret=1
+ if [ $ret -eq 1 ];then
+ vmm_infoline $VMM_INFO "kill minicom pid Fail"
+ return 1
+ fi
+ fi
+ VMM_SCP "${MINICOM_SERVER}:${minicom_host_serial_log}" "${serial_log_path}/${host_serial_log_name}" || ret=1
+ if [ $ret -eq 1 ];then
+ vmm_infoline $VMM_INFO "Copy ${minicom_host_serial_log} to ${serial_log_path}/${host_serial_log_name} Fail"
+ return 1
+ fi
+ VMM_SSH ${MINICOM_SERVER} "rm ${minicom_host_serial_log}" || ret=1
+ if [ $ret -eq 1 ];then
+ vmm_infoline $VMM_INFO "Delete ${minicom_host_serial_log} Fail"
+ return 1
+ fi
+ case $XVS_VMM in
+ 0|2)
+ **check_error_message XEN_ERROR_KEYWORDS[@] "${serial_log_path}/${host_serial_log_name}" "${serial_error_log}" || return 1**
+ ;;
+ 1|3)
+ check_error_message KVM_ERROR_KEYWORDS[@] "${serial_log_path}/${host_serial_log_name}" "${serial_error_log}" || return 1
+ ;;
+ *) exit
+ ;;
+ esac
+ return 0
+}
+
+#check guest serial log
+check_guest_serial_log()
+{
+ local serial_error_log
+ local guest_serial_log

+ local journal_name=cat ${XVS_DATA}/var/journalname
+ local serial_log_path=dirname ${journal_name}
+ case $XVS_VMM in
+ 0|2)
+ guest_serial_log=${1}.log
+ serial_error_log=${serial_log_path}/serial_error_guest.log
+ ;;
+ 1|3)

+ local guest_name=echo $1 |awk -F "tmp-img_" '{print $NF}'
+ guest_serial_log=guest_serial_${guest_name}.log
+ serial_error_log=${serial_log_path}/serial_error_guest.log
+ ;;
+ *) exit;;
+ esac

+ if [ ! -f seriallogpath/{guest_serial_log} ]; then
+ return 0
+ fi
+ check_error_message Guest_ERROR_KEYWORDS[@] “seriallogpath/{guest_serial_log}” “${serial_error_log}” || return 1
+ return 0
+}

原创粉丝点击