代码因SED中的stdin而失败

问题描述 投票:1回答:1

我有一个bash脚本,它找到具有特定扩展名的文件,然后将文件传递给一个函数,该函数检查文件中的每一行是否只包含导入库的文件。例如:

function testing() {
while IFS='' read -r line; do
    if [[ "$line" =~ .*log\" ]]; then
        echo "log is imported in the file" $1
        break
    else
        echo "log is not imported in the file" $1
        break
    fi
done < <(sed -n '/import (/,/)/p' "$1")
}

function main() {
for file in $(find "$1" -name "*.go"); do
    if [[ $file == *test.go ]]; then
        :
    else 
        var1=$(testing $file)
        echo "$var1"

    fi
done;
}

main $1

问题是脚本在没有testing函数中的else块的情况下工作但是在testing函数中引入了else块它只是默认回显log is not imported in the file blah,即使在某些文件中使用了log

关于什么是问题的任何想法?

谢谢。

这是一个示例输入文件:

package main                                                           

import (
    "fmt"
    "io/ioutil"
    logger "log"
    "net/http"                                                           
)                                                                        
type webPage struct {
    url  string
    body []byte
    err  error                                                              
}                                                                         
...

如果导入或不导入log,输出基本上是回声。

sed pattern-matching
1个回答
0
投票

您需要重写testing函数的逻辑,因为它只会测试文件的第一行。实际上,if [[ "$line" =~ .*log\" ]]的每个分支都有一个break语句,因此在实践中,每当读取第一行时都会达到break

© www.soinside.com 2019 - 2024. All rights reserved.