bashcommand-linephpmd

Take a comma separated string and add it into a command


Anyone know what I'm doing wrong here? DIFFS contains a comma-separated list of file paths after line 1 (foo/bar,baz/bat.php). If I just copy paste that into the rest of what's on line 2 in the actual terminal, I get what I expect. Doing it via the script I'm in just runs forever and then returns a new prompt, no error. I've tried a few things but my bash fu is super weak... :S

#!/bin/bash

...

function phpmd() {
    DIFFS=`git diff development --name-only | xargs | sed 's/ /,/g'`
    phpmd ${DIFFS} text phpmd.xml
}

...

case "$1" in
somecase)
   somecase ${@:2}
   ;;
phpmd)
   phpmd
   ;;
....
*)
   showUsage)
   ;;
esac

Even if I just do this in iTerm, it works.

prompt$ DIFFS=`git diff development --name-only | xargs | sed 's/ /,/g'`
prompt$ phpmd $DIFFS text phpmd.xml
/path/to/SomeController.php:58  The class SomeController has 1046 lines of code. Current threshold is 1000. Avoid really long classes.
...

Edit: It is being called as a function in a larger script as follows if this matters. Edited the above code block to reflect. I used these two lines in a standalone function and they worked perfectly so it's something I don't understand about the wider script obvs...

set -x output:

+ set -x
++ git diff development --name-only
++ xargs
++ sed 's/ /,/g'
+ DIFFS=app/Http/Controllers/TrackerController.php,docker/kubernetes/configmap-php-ini-worker.yaml,src/Packages/CoreFilter.php
+ phpmd app/Http/Controllers/TrackerController.php,docker/kubernetes/configmap-php-ini-worker.yaml,src/Packages/CoreFilter.php text phpmd.xml

just looping hundreds and hundreds of times until I Ctrl+C


Solution

  • The declaration of the function phpmd is "hiding" the phpmd program on your path. You should rename your function to something else:

    function runphpmd() {
    

    And update the call site as well:

    phpmd)
       runphpmd
       ;;
    

    Alternatively, you can use the command builtin to avoid recursively calling the function named phpmd:

    function phpmd() {
        DIFFS=`git diff development --name-only | xargs | sed 's/ /,/g'`
        command phpmd ${DIFFS} text phpmd.xml
    }
    

    From the documentation for command:

    Runs command with arguments ignoring any shell function named command.