因为项目从AIX系统迁移到了RedHat系统,之前写过的一段Shell脚本不能成功运行,经过分析,锁定了关键代码如下:
readFileContent(){ currentFile=$1 fileShowRows=0 fileRealRows=0 fileBusinessDate=$2 fileCountStartTime=0 fileCountEndTime=0 echo $1 echo $2 cat $1 | while read line do if [ "$fileRealRows" -eq "0" ] then echo "$line" fileShowRows=`echo $line | awk ‘{split($0,a,"|"); print a[1]}‘` fileCountStartTime=`echo $line | awk ‘{split($0,a,"|"); print a[2]}‘` fileCountEndTime=`echo $line | awk ‘{split($0,a,"|"); print a[3]}‘` echo "$fileShowRows" echo "$fileCountStartTime" echo "$fileCountEndTime" fi echo ‘fileRealRows‘"$fileRealRows" let fileRealRows+=1 echo ‘fileRealRows‘"$fileRealRows" done echo "$fileShowRows" echo "$fileRealRows" echo "$fileCountStartTime" echo "$fileCountEndTime" }
该段代码是用于解析一下一段文本的
1|20170322000001|20170322235959 1|00206342|020602|37906|20170322|083934|21205967090|21205967090|01|01|10002930|
其中用到了出现差异的一段代码是以while管道的方式读取文件内容,在AIX和Linux系统中执行的结果分别如下
AIX执行结果: 20170322 1|20170322000001|20170322235959 1 20170322000001 20170322235959 fileRealRows0 fileRealRows1 fileRealRows1 fileRealRows2 1 2 20170322000001 20170322235959 Linux执行结果: 20170322 1|20170322000001|20170322235959 1 20170322000001 20170322235959 fileRealRows0 fileRealRows1 fileRealRows1 fileRealRows2 0 0 0 0
从执行结果中可以看出来,Linux在子Shell中赋值的父变量对父Shell无效,而AIX系统是有效的。修改的方法如下
readFileContent(){ currentFile=$1 fileShowRows=0 fileRealRows=0 fileBusinessDate=$2 fileCountStartTime=0 fileCountEndTime=0 echo $1 echo $2 while read line do if [ "$fileRealRows" -eq "0" ] then echo "$line" fileShowRows=`echo $line | awk ‘{split($0,a,"|"); print a[1]}‘` fileCountStartTime=`echo $line | awk ‘{split($0,a,"|"); print a[2]}‘` fileCountEndTime=`echo $line | awk ‘{split($0,a,"|"); print a[3]}‘` echo "$fileShowRows" echo "$fileCountStartTime" echo "$fileCountEndTime" fi echo ‘fileRealRows‘"$fileRealRows" let fileRealRows+=1 echo ‘fileRealRows‘"$fileRealRows" done<$1 echo "$fileShowRows" echo "$fileRealRows" echo "$fileCountStartTime" echo "$fileCountEndTime" }
时间: 2024-10-14 17:53:47