0

I have a script which works for buffering (in the code below I have it set for 5000 milliseconds) except that I do not know how to call the needed python script with an indefinite number of arguments. How would this be done?

EDIT:

I have updated the if logic in the while loop to reflect the "hack" which works with a for loop of the arguments, but I know there's a one-line solution to this.

NOTE: I suppose that I could loop through the $lines, create a string and eval() but that seems ugly NOTE 2: I am new to bash; comments on overall coding are appreciated after the primary question are answered.

#!/bin/bash
sent=0
interval=5000
lines=()
i=0;
while read line; do
    lines[$i]=$line
    ((i++))
    point=$(($(date +%s%N)/1000000));

    if [ $((point-sent)) -gt $interval ]
    then
        cmd="php LineProcessor.py";
        for arg in "${lines[@]}"
        do
            cmd="$cmd \"$arg\""
        done
        eval $cmd

        sent=$point
        # -- reset lines array --
        i=0
        lines=()
    fi


done
Oliver Williams
  • 5,966
  • 7
  • 36
  • 78
  • 1
    [`"${lines[@]}"`](https://www.gnu.org/software/bash/manual/bash.html#Arrays)? – Jonathan Leffler Mar 30 '20 at 13:57
  • Given what you had shown, it may be better to do everything in python. – Philippe Mar 30 '20 at 14:07
  • Having a bash script call the python script is a requirement unfortunately – Oliver Williams Mar 30 '20 at 14:08
  • 1
    There's a cure for that, @OliverWilliams: `exec python LineProcessor.py "$@"`. :D – Jonathan Leffler Mar 30 '20 at 14:22
  • Thanks @JonathanLeffler, So if the individual arguments have spaces or quotes, will the `"$@"` handle that? – Oliver Williams Mar 30 '20 at 14:46
  • Using `"$@"` preserves the number of arguments and the spaces within the arguments. Using `$@` or `$*` does not preserve spacing, and `"$*"` generates a single string. `"$@"` works if the list is empty; it generates no arguments. The analogous notation with arrays works 'the same'. See also [How to iterate over arguments in a Bash script?](https://stackoverflow.com/a/256225/15168), etc. – Jonathan Leffler Mar 30 '20 at 14:49
  • @JonathanLeffler, but what is $@ referring to? I have tried: `eval php LineProcessor.php "$@"` as well as using `..$@{lines}` and `..$@{lines[@]}` but get nothing as argv for the first, and just the string literal "{lines}" for the first and "{lines[@]}" for the second. – Oliver Williams Mar 30 '20 at 18:03
  • The `"$@"` is the set of command line arguments passed to the shell, as modified by `shift` and/or `set --`. I'm not clear how you mangled `"${lines[@]}"` into either `$@{lines}` or `$@{lines[@]}` but the shell is fussy and doesn't like the notation you attempted to use. The double quotes matter. The suggested `exec python LineProcessor.py "$@"` script would be the second line (after the shebang line) in a script where everything else was done in Python, as suggested by @Philippe. It sticks to the letter of "Bash script calling Python script" while the Python script does all the real work. – Jonathan Leffler Mar 30 '20 at 18:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210611/discussion-between-oliver-williams-and-jonathan-leffler). – Oliver Williams Mar 30 '20 at 18:38

1 Answers1

0

So in order to provide an answer as well as the comments below, the correct line above would be:

python LineProcessor.py "${lines[@]}"
Oliver Williams
  • 5,966
  • 7
  • 36
  • 78