LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-23-2010, 02:09 AM   #1
BeaverusIV
Member
 
Registered: Oct 2004
Location: New Zealand
Distribution: Arch Linux 2010.05
Posts: 136

Rep: Reputation: 18
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.
 
Old 08-23-2010, 05:29 AM   #2
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
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.

Last edited by berbae; 08-23-2010 at 05:32 AM.
 
Old 08-23-2010, 05:39 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

Last edited by David the H.; 08-23-2010 at 05:50 AM. Reason: typo and minor clarification
 
Old 08-23-2010, 06:16 AM   #4
BeaverusIV
Member
 
Registered: Oct 2004
Location: New Zealand
Distribution: Arch Linux 2010.05
Posts: 136

Original Poster
Rep: Reputation: 18
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.
 
Old 08-23-2010, 11:01 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 12:17 PM
Quotes in bash Mr. Alex Linux - Newbie 4 08-19-2010 03:12 AM
howto: bash.org quotes at logon jbarelds Linux - General 6 03-26-2010 03:34 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
How to get rid of quotes in bash? CodeWarrior Slackware 7 05-21-2003 10:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration