|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 0when 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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
As suggested, instead of a loop, Code:
killall "${apps[@]}" |
| Sponsored Links | ||
|
![]() |
| Tags |
| arrays, mac os x, macintosh |
| Thread Tools | Search this Thread |
| 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 05:28 AM |
| Passing full path as argument when it contains variable strings | KidD312 | UNIX for Advanced & Expert Users | 0 | 06-25-2012 01:21 PM |
| Passing by value a char array | rkraj | Programming | 5 | 08-12-2008 10:30 AM |
| array of strings | davidcreston | Programming | 1 | 09-14-2007 12:12 AM |
| passing strings as arguments | iago | UNIX for Dummies Questions & Answers | 1 | 08-22-2007 10:04 AM |
|
|