0

I am new to bash, trying to put together a little helper for myself.

I have a function and it seems it swallow new lines in the inner loop.

#!/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

I get this output:

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

What I want is to have all the functions in new line, what is exactly what I get in the outer for loop.

rrd
  • 1,441
  • 2
  • 15
  • 36
  • 1
    Fix all your missing quotes. Run your code through http://shellcheck.net/ -- I'd suggest deleting your question, fixing what shellcheck identifies, trying to repro the issue, and then undeleting if you still have a problem. – Charles Duffy Nov 02 '21 at 17:04
  • 1
    In particular, `echo $variable` converts all whitespace characters or runs of whitespace in `variable` to a single regular space each. And your code is _full_ of `echo`s with unquoted arguments. – Charles Duffy Nov 02 '21 at 17:05
  • `IFS=$'\n'` _mitigates_ the problem, but it does not by any means _solve_ it -- newlines are still in IFS, so they still get split on. Also, changing IFS doesn't modify other side effects of unquoted expansion (most particularly, globbing). – Charles Duffy Nov 02 '21 at 17:06
  • BTW, as another aside -- all-caps variable names are reserved; you should be using variables with at least one lower-case character in your own code when not trying to interact with a variable meaningful to the shell or other operating-system-provided tools. See the relevant POSIX standard at https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html establishing this convention. – Charles Duffy Nov 02 '21 at 17:07
  • ...to quote: *The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities* -- read that keeping in mind that setting a shell variable implicitly overwrites any like-named environment variable (`export` is only needed to update the environment if no environment variable by the same name exists already). – Charles Duffy Nov 02 '21 at 17:08
  • Thanks, quoting solved the issue. – rrd Nov 02 '21 at 17:15
  • (also, `$@` should be `"$@"` -- otherwise it behaves just like unquoted `$*`) – Charles Duffy Nov 02 '21 at 17:19

0 Answers0