⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reptransfer.sh

📁 一个awk和bsh混合编程的例子
💻 SH
字号:
#!/bin/ksh#For ISN compitable report file transfering#Author: RuanWX#Usage: `basename $0` -v version -i report_id [-f report_file_name] [-u user_id -r request_id ] [-a attributes_string]#in which attributes_string is like: userLabel:neType:availableCapNum#Please note if an attribute includes metacharacters, the metacharacters must be escaped#Example: $FDN must bu \$FDN#Or the whole attributes_string is quoted by ', such as '$FDN:userlabel'.USAGE="`basename $0` -v version -i report_id [-f report_file_name] [-u user_id -r request_id ] [-a [attr1[:attr2[:attr3 ...]]]";if [ $# -ne 12 -a $# -ne 10 -a $# -ne 8 -a $# -ne 6 -a $# -ne 4 ]; then	echo "$USAGE";	exit 1;fiwhile getopts v:i:f:u:r:a:o OPTION ; do	case "$OPTION" in		v) VERSION="$OPTARG" ;;		i) REPORT_ID="$OPTARG" ;;		f) REPORT_FILE_NAME="$OPTARG" ;;		u) USER_ID="$OPTARG" ;;		r) REQUEST_ID="$OPTARG" ;;		a) ATTRIBUTES="$OPTARG" ;;		\?) echo "$USAGE" ;			exit 1			;;	esacdoneecho "VERSION: $VERSION REPORT_ID: $REPORT_ID ATTRIBUTES: $ATTRIBUTES REPORT_FILE_NAME: $REPORT_FILE_NAME USER_ID: $USER_ID REQUEST_ID: $REQUEST_ID";if [ -z "$VERSION" ] ; then	echo "ERROR : version is not specified.";	exit 1;fiif [ -z "$REPORT_ID" ]; then	echo "ERROR: report_id is not specified.";	exit 1;fiif [ -z "$ATTRIBUTES" ]; then	echo "WARNING: attributes are not specified";else	ATTRIBUTES_TMP=${ATTRIBUTES#*\{};	ATTRIBUTES=${ATTRIBUTES_TMP%*\}};fiif [ -z "$USER_ID" ]; then	echo "WARNING: user_id are not specified";fiif [ -z "$REQUEST_ID" ]; then	echo "WARNING: request_id are not specified";fiif [ -z "$REPORT_FILE_NAME" ]; then	echo "ERROR: report_file_name is not specified";	exit 1;#	if [ -z "$ATTRIBUTES" ]; then#		echo "ERROR: attributes should not be empty while report_file_name is not specified";#		exit 1;#	fifiCONFIG_FILE_NAME="./data/Rep_$VERSION.cfg";echo "Try to find config file: $CONFIG_FILE_NAME";if [ ! -r $CONFIG_FILE_NAME ]; then	echo "ERROR: config file $CONFIG_FILE_NAME not exist";	exit 1;fiif [ $REPORT_ID -le 0 ]; then	echo "ERROR: report_id must be a number larger than 0";	exit 1;fiSEP_CONF="./data/Seperator.cfg";parseSepConf(){	if [ -r $SEP_CONF ];then	{		Assemble_start=`awk '{			if ($0 ~ /^ *\t* *#.*/ ){			}			else if ($0 ~/ *\t* *$/){			}			else if ($1 ~/^NESTING_INDICATOR_OPEN/ ){				printf "%s\n",$3			}		}' $SEP_CONF`;		if [ -z "$Assemble_start" ];then			echo "ERROR:NESTING_INDICATOR_OPEN is not defined";			exit 1;		fi				Assemble_end=`awk '{			if ($0 ~ /^ *\t* *#.*/ ){			}			else if ($0 ~/ *\t* *$/){			}			else if ($1 ~/^NESTING_INDICATOR_CLOSE/ ){				printf "%s\n",$3			}		}' $SEP_CONF`;		if [ -z "$Assemble_end" ]; then			echo "ERROR:NESTING_INDICATOR_CLOSE is not defined";			exit 1;		fi				Field_sep=`awk '{			if ($0 ~ /^ *\t* *#.*/ ){			}			else if ($0 ~/ *\t* *$/){			}			else if ($1 ~/^ATTRIBUTE_SEPARATOR_INDICATOR/ ){				printf "%s\n",$3			}		}' $SEP_CONF`;		if [ -z "$Field_sep" ];then			echo "ERROR:ATTRIBUTE_SEPARATOR_INDICATOR is not defined";			exit 1;		fi				Esc_sep=`awk '{			if ($0 ~ /^ *\t* *#.*/ ){			}			else if ($0 ~/ *\t* *$/){			}			else if ($1 ~/^ESCAPE_INDICATOR/ ){				printf "%s\n",$3;			}		}' $SEP_CONF`;		if [ -z "$Esc_sep" ]; then			echo "ERROR:ESCAPE_INDICATOR is not defined";			exit 1;		fi	}	else	{		echo "ERROR: $SEP_CONF not exist";		exit 1;	}	fi}#Attr_Names--attribute names;#Attr_Types--attribute types;#Obj_Deleted--0:object is not deleted;1:object is deletedparseConf(){	Obj_Deleted=`awk -F":" '			BEGIN {deleted=0;}		/^ *\t* *#.*/ {;}		/^ *\t* *$/ {;}		/^[0-9]*/ {			if ($1==id ){				if (length($3)==0 ){					deleted=1;				}			}		}		END {printf "%d\n",deleted;}' id=$REPORT_ID $CONFIG_FILE_NAME`;		if [ $Obj_Deleted -eq 0 ]; then	{		Attr_Names=`awk -F":" '{			if ( $0 ~ /^ *\t* *#.*/ ){			}			else if ( $0 ~ /^ *\t* *$/ ){				}			else if ( $1 ~ /[0-9]*/ ) {				if ($1==id ){					printf "%s\n",$3;				}			}		}' id=$REPORT_ID $CONFIG_FILE_NAME`;				Attr_Types=`awk -F":" '{			if ( $0 ~ /^ *\t* *#.*/ ){			}			else if ( $0 ~ /^ *\t* *$/ ){				}			else if ( $1 ~ /[0-9]*/ ) {				if ($1==id ){					if (length($3)==0){					}					else if (length($4)==0){						printf "%s:INT\n", $3;					}					else {						printf "%s:%s\n",$3,$4;					}				}			}		}' id=$REPORT_ID $CONFIG_FILE_NAME`;	}	fi}generateEmptyReport(){	if [ -r "$REPORT_FILE_NAME" ]; then	{		OLD_REPORT_FILE=$REPORT_FILE_NAME.rt;		mv $REPORT_FILE_NAME $OLD_REPORT_FILE;				awk '{			if ( $0 ~ /^!RECORD_DESCRIPTION=\{.*\}/ )			{				printf "!RECORD_DESCRIPTION={";				printf attrs;				printf "}\n";				printf "!<EOF>\n";				exit;			}			else			{				printf "%s\n", $0;			}		}' attrs=$ATTRIBUTES $OLD_REPORT_FILE > $REPORT_FILE_NAME;		echo "Done";	}	else	{		if [ -z "$USER_ID" -o -z "$REQUEST_ID" ]; then			echo "ERROR: user_id or request_id is empty";			exit 1;		fi				file_time=`echo "$REPORT_FILE_NAME"|awk -F"_" '{printf "%s\n",$3;}'`;				echo '#*** Retrieve Data generated by ISN Agent ***' >> $REPORT_FILE_NAME;		echo "!USER_ID=$USER_ID" >> $REPORT_FILE_NAME;		echo "!REQUEST_ID=$REQUEST_ID" >> $REPORT_FILE_NAME;#		echo "!START_TIME=`date +%Y%m%d%H%M%S`" >> $REPORT_FILE_NAME;#		echo "!END_TIME=`date +%Y%m%d%H%M%S`" >> $REPORT_FILE_NAME;		echo "!START_TIME=$file_time" >> $REPORT_FILE_NAME;		echo "!END_TIME=$file_time" >> $REPORT_FILE_NAME;		echo "!RECORD_NUM=0" >> $REPORT_FILE_NAME;		echo "#***********" >> $REPORT_FILE_NAME;		echo "!RECORD_DESCRIPTION={$ATTRIBUTES}" >> $REPORT_FILE_NAME;		echo "!<EOF>" >> $REPORT_FILE_NAME;		echo "Done";	}	fi}transferReport(){	if [ -r "$REPORT_FILE_NAME" ]; then	{		echo "File exists, now transfer the report file";			OLD_REPORT_FILE=$REPORT_FILE_NAME.rt;		mv $REPORT_FILE_NAME $OLD_REPORT_FILE;		awk -F"$Field_sep" '{			req_attr_num=extractAttrs(required_attrs, req_attrs, ":");						split(additional_type, add_types, " ")			for (z in add_types)			{				zz=index(add_types[z], ":");				name=substr(add_types[z], 1, zz-1);				value=substr(add_types[z], zz+1);				attr_types[name]=value;			}						if ($0 ~ /^#.*/ )			{				printf "%s\n", $0;			}			else if ($0 ~ /^!RECORD_DESCRIPTION=\{.*/ )			{				pos_front=index($0, asm_begin);				pos_back=index($0, asm_end);				len=pos_back - pos_front-1;				old_attrstring=substr($0, pos_front+1, len);				for (attr_entry_tmp in attr_types)				{					sub(attr_entry_tmp":", "", old_attrstring);					sub(":"attr_entry_tmp, "", old_attrstring);				}				old_attrs_num=extractAttrs(old_attrstring, old_attrs, fld_sep);								printf "!RECORD_DESCRIPTION=%s",asm_begin;				first_attr_printed=0;				for (x=1;x<=req_attr_num;x+=1)				{					found=0;					for(y=1;y<=old_attrs_num;y+=1)					{						if (req_attrs[x]==old_attrs[y])						{							found=1;							req_old_map[x]=y;							if (x!=1){								printf fld_sep;							}							printf req_attrs[x];							first_attr_printed=1;						}					}					if (found==0)					{						req_old_map[x]=0;						if (length(attr_types[req_attrs[x]])!=0)						{							if (x!=1 && first_attr_printed!=0){								printf fld_sep;							}							printf req_attrs[x];							first_attr_printed=1;						}					}				}				printf "%s\n",asm_end;			}			else if ($0 ~ /^!.*/ )			{				printf "%s\n", $0;			}			else			{				real_fld_ix=0;				for (fld_ix=1;fld_ix<=NF;)				{					real_fld_ix+=1;					if (substr($fld_ix,1,1)==asm_begin)					{						fld_tmp="";						for(;fld_ix<=NF;fld_ix+=1)						{							if (substr($fld_ix,length($fld_ix),1)==asm_end &&length($fld_ix)==1 || 								substr($fld_ix,length($fld_ix),1)==asm_end &&length($fld_ix)>1 && substr($fld_ix,length($fld_ix)-1,1)!=esc_ch)							{								if (fld_tmp!=""){									fld_tmp=fld_tmp""fld_sep""$fld_ix;								}								else{									fld_tmp=$fld_ix;								}								real_fld[real_fld_ix]=fld_tmp;								break;							}							else							{								if (fld_tmp!=""){									fld_tmp=fld_tmp""fld_sep""$fld_ix;								}								else{									fld_tmp=$fld_ix;								}							}						}						fld_ix+=1;					}					else					{						real_fld[real_fld_ix]=$fld_ix;						fld_ix+=1;					}				}								first_attr_printed=0;				for (xx=1;xx<=req_attr_num;xx+=1)				{					if (req_old_map[xx]!=0)					{						if (xx!=1){							printf fld_sep;						}						printf real_fld[req_old_map[xx]];						first_attr_printed=1;					}					else if (length(attr_types[req_attrs[xx]])!=0)					{						type_real=attr_types[req_attrs[xx]];						if (type_real=="INT")						{							if (xx!=1 && first_attr_printed!=0){								printf fld_sep;							}							printf "-1";							first_attr_printed=1;						}						else if (type_real=="STR" || type_real=="ENUM")						{							if (xx!=1 && first_attr_printed!=0){								printf fld_sep;							}							printf "NotMeaningful";							first_attr_printed=1;						}					}				}				printf "\n";			}			}		function extractAttrs(in_string,array,seperator) {			ix=1;			attrs_string_tmp=in_string;			pos_sep_req=index(attrs_string_tmp,seperator);			while (pos_sep_req != 0 && length(attrs_string_tmp) !=0)			{				attr_str=substr(attrs_string_tmp,1,pos_sep_req-1);				array[ix]=attr_str;				ix+=1;				attrs_string_tmp=substr(attrs_string_tmp,pos_sep_req+1);				pos_sep_req=index(attrs_string_tmp,seperator);			}			if (length(attrs_string_tmp) !=0)			{				attr_str=substr(attrs_string_tmp,1);				array[ix]=attr_str;			}			else			{				ix-=1;			}			return ix;		}		' required_attrs="$ATTRIBUTES" additional_type="$Attr_Types" asm_begin="$Assemble_start" asm_end="$Assemble_end" fld_sep="$Field_sep" esc_ch="$Esc_sep" $OLD_REPORT_FILE >$REPORT_FILE_NAME;		echo "Done";	}	else	{		echo "ERROR: $REPORT_FILE_NAME not exist";		exit 1;	}	fi}parseConf;parseSepConf;if [ $Obj_Deleted -eq 1 ]; then{	echo "Object deleted, now try to generate the empty report";	generateEmptyReport;}else{	attr_num=0;	for conf_attr in $Attr_Names	do		attr_num=`echo "$attr_num+1"|bc`; 	done		type_num=0;	for conf_type in $Attr_Types	do		type_num=`echo "$type_num+1"|bc`; 	done		if [ "$attr_num" != "$attr_num" ]; then		echo "ERROR: config file error, attribute number is not equal to type number";		exit1;	fi		transferReport;}fi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -