打印

[SH]shell script problem

[SH]shell script problem

This is a Bourne shell script. I try to read words from a file $1(which has just two words a line) and store to another two files respectively. My script is following:
while read x y < `cat $1`
do
   echo $x >> $input
   echo $y >> $output
   done
But it  doesn't work as expectation. Could anybody tell me what's the problem?
Thanks a lot.

TOP

not sure what you are trying to do.
try something like this:

awk '{print $1}' $inputfile >$outputfile1
awk '{print $2}' $inputfile >$outputfile2
※※※※※※※※※※※※※※※在UNIX版发贴必读!否则关帖※※※※※※※※※※※※※※※ http://www.itpub.net/showthread.php?s=&threadid=38248

TOP

A file with exactly two words per line, the first word is used as stdin to a program, the second is the expected output of the program. I'm try to separate this file into two files, one contains only the inputs for the program, the other has the expected outputs. I can use only read, cat,echo,ls,cut,[ ],cp,rm.cmp.diff, mv expr, eval, exit, wait,cd,shift commands.
input=echo $1.$$
output=echo $1.$$
while read x y < `cat $1`
do
echo $x >> $input
echo $y >> $output
done
The result of this script should be: file input/ouput contains only the first /second word, one per line, each word has a newline appends to it.
The question is after I run the above script, the input/output is NULL.

Another question is, I have to caculate how many outputs of the program differ from the expected outputs.
I use cmp or diff $1.output  $output,  I don't know how to caculate the different lines between $1.output and $output. Could you give me an idea?

Thank you very much for your help.

TOP

the right answer is :

cat $1 | while read x y
do
echo $x >> $input
echo $y >> $output
done

and you will find it works

TOP

But the input and output file are still empty. How come?

How can I check how many lines are different between $1.output and $output with cmp or diff?

I try many ways but can't understand why it doesn't work as expection.

Help me please! Many thanks.

TOP

Now I change this
input=echo $1.$$
output=echo $1.$$
while read x y < `cat $1`
do
echo $x >> $input
echo $y >> $output
done

to this

cat $1 | while read x y
do
echo $x >> input
echo $y >> output
done
input and output file are not empty. But I don't know why the first script doesn't work.

TOP

Try this one.

input=echo $1.$$
output=echo $1.$$
while read x y
do
echo $x >> $input
echo $y >> $output
done < `cat $1`

And you can add the comman "set -x" at the first line to trace the script. At the last line add "set +x" to switch off the trace setting.

TOP

cat $1 | while read x y
do
echo $x >> $input
echo $y >> $output
done

input和output文件为空只是因为你还没有定义输入和输出文件,你可以从别的地方把input和output的文件名传递过来,也可以在这个文件最前面从输入界面读入

TOP

Thanks guys. I get through it.

TOP


感谢一直以来您对我们的支持!
当前时区 GMT+8, 现在时间是 2008-10-16 12:25 京ICP证060528 号

Designed By 17DST