LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with bash arrays and quotes (PvPGN) (https://www.linuxquestions.org/questions/linux-software-2/help-with-bash-arrays-and-quotes-pvpgn-827920/)

BeaverusIV 08-23-2010 01:09 AM

Help with bash arrays and quotes (PvPGN)
 
I am trying to get PvPGN working... I have installed all the files, the only thing now is that bntrackd wants to use 'logs/bntrackd.log' for it's logfile, but seeing as it's in /usr/sbin, I want it to use /var/log/bntrackd. This is done with 'bntrackd --logfile=/var/log/bntrackd.log' but when I put that in the script it won't process it as a command (instead splitting it and complaining that I don't have --logfile=/var/log/bntrackd.log installed)

/etc/rc.d/pvpgn:
Code:

#!/bin/bash
...
[ -z "$PVPGN_DAEMONS" ] && PVPGN_DAEMONS=(bnetd d2dbs d2cs)
...
for d in ${PVPGN_DAEMONS[@]}; do
  PID=`pidof -o %PPID /usr/sbin/$d`
  [ -z "$PID" ] && /usr/sbin/$d
...

/etc/conf.d/pvpgn:
Code:

PVPGN_DAEMONS=(bnetd d2cs d2dbs 'bntrackd --logfile=/var/log/bntrackd.log')
What combination of quotes should I be using? This stuff always confused me, but I want to learn it now.

berbae 08-23-2010 04:29 AM

Try to change
from
[ -z "$PID" ] && /usr/sbin/$d
to
[ -z "$PID" ] && eval "/usr/sbin/$d"

Single quotes or double quotes don't seem to import here.

David the H. 08-23-2010 04:39 AM

I don't think it's quite that simple. You're quoting of the array is pretty much correct. The problem is that the word splitting is being done here in the rc.d script:
Code:

for d in ${PVPGN_DAEMONS[@]}; do
${PVPGN_DAEMONS[@]} outputs the full array as a string of words, which are then each processed separately by the loop. Since there's a space in your array element, it's treated as two separate words by the loop.

I thought you could probably simply embed a further set of escaped quotes or an escaped space when setting the array, but that doesn't seem to be working for me.

What you really need is to modify the loop so that it handles the strings better. You could, for example, put quotes around the array like this:
Code:

for d in "${PVPGN_DAEMONS[@]}"; do
This will cause each of the array elements to be output as if quoted separately, which should preserve your string. However, I don't know how that would affect the pidof command in the next line. Can it handle the input with the extra string attached? It should probably also be modified to something like this:
Code:

PID=`pidof -o %PPID /usr/sbin/${d% *}`
...which should strip everything after the first space off the variable before processing it.

BeaverusIV 08-23-2010 05:16 AM

Wow, thanks. Mega thanks. I might even try to get this sorted into a PKGBUILD and put on the AUR instead of the now defunct one there right now :)
Adding the quotes and the little extra bit for pidof worked.

David the H. 08-23-2010 10:01 AM

Glad to help.

Incidentally, I noticed one small thing that should be corrected. Please change the pidof command to:
Code:

PID=`pidof -o %PPID /usr/sbin/${d%% *}`
The version I posted earlier works fine with your string, but the second % is necessary in order to handle strings with multiple spaces in them.

By the way, $(..) is generally recommended over `..` these days for embedded commands like the above, but I'll leave it up to you whether it's worth the effort fixing it in this script.


All times are GMT -5. The time now is 12:38 PM.