免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: cs221313
打印 上一主题 下一主题

跳出循环,变量值消失. SOS [复制链接]

论坛徽章:
0
5 [报告]
发表于 2003-08-03 12:37 |只看该作者

跳出循环,变量值消失. SOS

下面是整个代码: 是一个将input 传给一程序, 然后和一个output文件比较. 列出不一样的test case.

INDEX=1
LAST_INDEX=1
CASENO=1
LINENO=1
if [ $# -ne 3 ]
then
        echo "Usage: tester executable_file_name input_file_name output_file_name."
        exit 0
fi
if [ ! -x $1 ]
then
        echo "$1 is not an executable file."
fi
if [ ! -r $2 ]
then
        echo "$2 is not a readable file."
fi
if [ ! -r $3 ]
then
        echo "$3 is not a readable file."
fi
TOTALLINE=`wc -l $3 | sed "s/ $3$//" | sed "s/^       //"`
readonly TOTALLINE
for TOKEN in `cat $2`
do
        EXP="$EXP $TOKEN"
        if [ $TOKEN = "=" -o $TOKEN = "==" ]
        then
                if [ ! -w /tmp/tmp2$$ ]
                then
                        echo "=========" > /tmp/tmp2$$
                else
                        echo "=========" >> /tmp/tmp2$$
                fi
                echo $EXP | $1 >> /tmp/tmp2$$
                echo "RC=$?" >>/tmp/tmp2$$
                #read one output of test case and put it in /tmp/tmp$$ file. Then cut
                #these lines.
                while [ 1 ]
                do
                        LINE=`sed -n -e "$LINENO p" $3`
                        echo "$LINE"
                        LINENO=`expr $LINENO + 1`
                        export LINENO
                        echo "line number = $LINENO"
                        if [ ! -w /tmp/tmp1$$ ]
                        then
                                echo $LINE >/tmp/tmp1$$
                        else
                                echo $LINE >>/tmp/tmp1$$
                        fi
                        THREECHAR=`echo $LINE | cut -c1-3`
                        #echo $THREECHAR
                        if [ $THREECHAR = "RC=" ]
                        then
                                break
                        fi
                done < $3
                unset EXP
                if [ "`diff /tmp/tmp1$$ /tmp/tmp2$$`" ]
                then
                        echo $CASENO
                fi
                CASENO=`expr $CASENO + 1`
                /usr/bin/rm /tmp/tmp1$$ /tmp/tmp2$$
        fi
done

调用格式为: tester program_name input_file output_file

论坛徽章:
0
4 [报告]
发表于 2003-08-03 12:28 |只看该作者

跳出循环,变量值消失. SOS

你这段代码是干什么用的?

论坛徽章:
0
3 [报告]
发表于 2003-08-03 12:21 |只看该作者

跳出循环,变量值消失. SOS

还是不行. 我改成如下:
LINENO=1
export LINENO
while [ 1 ]
do
LINE=`sed -n -e "$LINENO p" $3`
echo "$LINE"
done < $3
echo "line number = $LINENO"


或把他放到循环了还是不行. thanks anyways.

论坛徽章:
0
2 [报告]
发表于 2003-08-03 11:51 |只看该作者

跳出循环,变量值消失. SOS

原帖由 "cs221313" 发表:

        LINE=`sed -n -e "$LINENO p" $3`
        echo "$LINE"
done < $3
echo "line number = $LINENO"


当跳出循环, LINENO值变为1. 求教如何能保留值在循环外.
   

shell处理重定向的循环时把循环代码用子shell来执行 ,export一下应该可以了。
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP