0

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

  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Oct 02 '19 at 19:21
  • If i understand correctly, the two lines you show work correctly when executed at the command line but not within a shell script. Assuming that is correct, you could add an `echo $DIFFS` in your shell script between the two lines to see if the results are not what you expect. – Sean Bright Oct 02 '19 at 19:24
  • One of the first steps in debugging shell scripts is to put `set -x` at the beginning. This will make it show every line as it's executing, with variables expanded. – Barmar Oct 02 '19 at 19:34
  • BTW, get out of the habit of using uppercase variable names. These are conventionally reserved for environment variables. – Barmar Oct 02 '19 at 19:34
  • There is no sample input, it's pulling a list of files from `git diff` and then should be using them as the first argument of `phpmd`. When I do this manually, the last line in my above code block starting `/path/to/SomeController...` is the desired output. When I use `set -x` it gives exactly what I'd expect but it's looping hundreds if not thousands of times. – Peter DeMarco Oct 02 '19 at 19:44

1 Answers1

2

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.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146