bash sed fail in while loop

后端 未结 2 669
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 20:33
#!/bin/bash
fname=$2
rname=$1
echo \"$(<$fname)\" | while read line ; do
    result=`echo \"$(<$rname)\" | grep \"$line\"; echo $?`
    if [ $result != 0 ]
    the         


        
2条回答
  •  遇见更好的自我
    2021-01-27 21:28

    If I understand your need correctly, you want a file newaks which contains the lines in $fname which are also in $rname.

    If this is what you want, using sed is overkill. Use fgrep:

    fgrep -x -f $fname $rname > newkas
    

    Also, there are problems with your script:

    • you capture the output of grep in result, which means it will never be exactly 0; what you want is executing the command and simply check for $?
    • your echoes are convoluted, just do grep whatever thefilename, or while...done ;
    • finally, you take the line as is from the source file: the line can potentially be a regex, which means you will try and match a regex in $rname, which may yield to unexpected results.

    And others.

提交回复
热议问题