我试图最初从一个包含作业列表的文件中填充一个数组。然后,我尝试从包含有关这些作业的信息的日志文件中读取。使用此信息,我希望将提取的数据附加到数组中。但是我遇到了一个问题。最初我的代码输出应该是“{Job}, {Type}, {Log}, {Process}, 'String data from log'”。然而,在我的代码中发生了一些事情,现在输出:“'String data from log'og}, {Process}”。本质上,它是用提取的字符串数据替换数组的第一部分,而不是在最后附加它。这是代码:
#!/user/bin/ksh93
log="./20130603.log"
list="./working_joblist.dat"
line="./linenum.dat"
# Populate JobArray (TESTING DONE)
i=0
listLines=$(wc -l < $list)
while [[ $i -lt $listLines ]]
do
JobArray[$i]=$(awk 'NR>=j+2 && NR<j+3 {print $0}' j=$i $list)
let i=$i+1
done
#READ BEGINS HERE
# Loop through this logic for each index in array
i=0
#server will be a parameter
server="MVS1"
# Grab line number based on the type of server the log came from
linenum=$(awk -F"," '/'"$server"'/{print $2}' $line)
while [[ $i -lt ${#JobArray[@]} ]]
do
# Assign job to first field in array
job=$(echo ${JobArray[$i]} | awk -F"," '{print $1}')
# Initialize startTime if it already exists
startTime=$(echo ${JobArray[$i]} | awk -F"," '{print $8}')
# Set up conditional variable
state=""
if [[ $state = "" ]];then
# Try to get ended ok data for job
data=$(awk 'NR>='"$linenum"' && /'"$job"'.*ENDED OK/{
status = "GREEN, "
date = $1", "
endTime = $2", "
if (startTime == ""){
# Calculate start time using the ending time and elapsed time
startTime=endTime-$14", "
}
data=status date startTime endTime
print data
}' $log)
# Assign status data to state. Will determine if match was found
state=$(echo $data|awk -F"," '{print $1}')
#if [[ $state = "GREEN" ]];then
# Remove the job from the working_joblist
#grep -v "$job" $list > ./truncated
#fi
fi
if [[ $state = "" ]];then
# Try to get ended notok data for job
data=$(awk 'NR>='"$linenum"' && /'"$job"'.*ENDED NOTOK/{
status = "RED, "
date = $1", "
endTime = $2", "
if (startTime == ""){
# Calculate start time using the ending time and elapsed time
startTime=endTime-$14", "
}
data=status date startTime endTime
print data
}' $log)
# Assign status data to state. Will determine if match was found
state=$(echo $data|awk -F"," '{print $1}')
fi
if [[ $state = "" ]];then
# Try to get start data for job
data=$(awk 'NR>='"$linenum"' && /'"$job"'.*STARTED/{
status = "YELLOW, "
date = $1", "
startTime = ", "
endTime = ", "
data=status date startTime endTime
print data
}' $log)
# Assign status data to state. Will determine if match was found
state=$(echo $data|awk -F"," '{print $1}')
fi
if [[ $state = "" ]];then
#Try to get waiting data for job
data=$(awk 'NR>='"$linenum"' && /'"$job"'.*WAITING/{
status = "BLUE, "
date = $1", "
startTime = ", "
endTime = ", "
data=status date startTime endTime
print data
}' $log)
# Assign status data to state. Will determine if match was found
state=$(echo $data|awk -F"," '{print $1}')
fi
if [[ $state = "" ]];then
data="WHITE, , , ,"
fi
# Append data to array
JobArray[$i]=${JobArray[$i]}$data
print ${JobArray[$i]}
# Iterate
i=$i+1
done
我尝试过多次修复。我可以将数据变量附加到自身并输出 $data$data。如果我使用 ${JobArray[$i]}${JobArray[$i]},JobArray 也会自行改写。这让我相信我的 Array 的某个方面是关闭的。此外,此打印命令输出的最后一行是正确的,之前的所有行都有此错误。