首页 > 解决方案 > bash函数在循环内吞下新行

问题描述

我是 bash 的新手,试图为自己组装一个小帮手。

我有一个函数,它似乎在内部循环中吞下了新行。

#!/bin/bash

collect_results(){
    egrep -o 'public function[ \t][ \t]*[a-zA-Z_]*' $@
}

get_function_name(){
    cut -d' ' -f3 <<< $1
}

get_file(){
    cut -d' ' -f1 <<< $1 | cut -f1 -d":"
}

IFS=$'\n'

RESULTS=( $(collect_results src/Model/Table/*) )

for RESULT in "${RESULTS[@]}"; do
    FUNCTION=$(get_function_name $RESULT)
    if ! [[ "$FUNCTION" =~ ^(beforeFind|buildRules|initialize|validationDefault|'')$ ]]; then
        FILE=$(get_file $RESULT)
        TMP=$(echo $FILE | sed "s/src/tests\/TestCase/")
        TESTFILE=$(echo $TMP | sed "s/\.php/Test\.php/")
        
        echo $TESTFILE
        
        TESTRESULTS=$(collect_results $TESTFILE)

        for TESTRESULT in "${TESTRESULTS[@]}"; do
            echo $TESTRESULT
        done
    fi
done

我得到这个输出:

tests/TestCase/Model/Table/SuggestionsTableTest.php
public function setUp public function tearDown public function testGetSuggestionsOneWord public function testGetSuggestionsMultipleWordsOneMatch public function testGetSuggestionsMultipleWordsMultipleMatches

我想要的是在新行中拥有所有功能,这正是我在外部 for 循环中得到的。

标签: bash

解决方案


推荐阅读