Problems passing strings within an array | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Problems passing strings within an array

UNIX for Dummies Questions & Answers


Tags
arrays, mac os x, macintosh

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-13-2013
hungryd hungryd is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 27 October 2014, 6:42 PM EDT
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Display Problems passing strings within an array

i have a list of apps that i need to forcequit and, from time to time, that list changes. perfect excuse to manage a single array! however, my strings with spaces aren't passing as i'd like them to. here's the simple script:


Code:
#!/bin/sh

#-----Array
apps=( firefox-bin firefox JavaApplicationStub groupwise "Google\ Chrome" "Microsoft\ Word" )

for i in "${apps[@]}"
do
	killall $i
done
exit 0

when i generate verbose feedback, i'm seeing each app in question quit EXCEPT for those with spaces. on a mac, in the command line, i have to type the following to forcequit word and chrome:


Code:
killall Microsoft\ Word
killall Google\ Chrome

but i can't seem to find a way to paste the correct syntax into the array to get these applications to quit as the others do. in the above script example, the result just says:

"+ for i in '"${apps[@]}"'
+ killall 'Microsoft\' Word
No matching processes were found"

any thoughts on what i might be missing?
Sponsored Links
    #2  
Old 03-13-2013
verdepollo's Avatar
verdepollo verdepollo is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 7 November 2014, 1:54 AM EST
Location: Mexico
Posts: 725
Thanks: 11
Thanked 106 Times in 102 Posts
Use single quotes when creating the array:

Code:
apps=( firefox-bin firefox JavaApplicationStub groupwise 'Google Chrome' 'Microsoft Word' )

Using pkill may also be a better choice instead of killall .
Sponsored Links
    #3  
Old 03-13-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 November 2014, 1:46 PM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts
This is the actual problem:


Code:
killall $i


Code:
killall "$i" # Prevent variable with spaces from splitting

Though really, I think your program could be rewritten as
Code:
killall "${apps[@]}"

People are understandably leery of 'killall', since on some other systems it has a far more, shall we say, literal meaning.
    #4  
Old 03-13-2013
hungryd hungryd is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 27 October 2014, 6:42 PM EDT
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
verdepollo: single quotes didn't work. tried that before i posted.
Corona688: many thanks, for the fix. didn't think to quote the variable.

cheers, folks,
d
The Following User Says Thank You to hungryd For This Useful Post:
Corona688 (03-18-2013)
Sponsored Links
    #5  
Old 03-14-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 22 November 2014, 10:54 PM EST
Location: Simplicity
Posts: 2,053
Thanks: 134
Thanked 603 Times in 547 Posts
With pgrep|pkill one does not need an array (and not a loop)

Code:
apps="firefox-bin|firefox|JavaApplicationStub|groupwise|Google Chrome|Microsoft Word"
pkill -x "$apps"

Sponsored Links
    #6  
Old 03-18-2013
hungryd hungryd is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 27 October 2014, 6:42 PM EDT
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
didn't know about pkill so thank you. otherwise, i'm all about elegance. if i can do something in two lines of code, i don't care what those two lines are. yours is great but as i'm already using arrays and not bothered by them, so is the earlier example.
Sponsored Links
    #7  
Old 03-18-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 November 2014, 1:46 PM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts
As suggested, instead of a loop,
Code:
killall "${apps[@]}"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing full path as argument when it contains variable strings KidD312 Programming 1 06-27-2012 06:28 AM
Passing full path as argument when it contains variable strings KidD312 UNIX for Advanced & Expert Users 0 06-25-2012 02:21 PM
Passing by value a char array rkraj Programming 5 08-12-2008 11:30 AM
array of strings davidcreston Programming 1 09-14-2007 01:12 AM
passing strings as arguments iago UNIX for Dummies Questions & Answers 1 08-22-2007 11:04 AM



All times are GMT -4. The time now is 03:31 PM.