open application with spaces in name [bash][OSX]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting open application with spaces in name [bash][OSX]
# 1  
Old 07-27-2012
Question open application with spaces in name [bash][OSX]

Hi guys, I'm new here and new to shell scripting so don't be hard on me

I'm trying to create a bash script to restart a process by name in Mac OSX.

I have no problem killing the application, the problem comes when launching it again.
I managed to store the path in a variable lets say

Code:
 /Applications/Google Chrome

later when I try to open the app I do this:

Code:
open $path.app

this works good for applications with no spaces on their names, however with spaced ones, like Google Chrome I get this as output:

Code:
The file /Users/myuser/Chrome.app does not exist.

I tried several quoting configurations and I get stuff like

Code:
The file /Users/myuser/   /Application/Google Chrome.app does not exist.
The file /Users/myuser/"/Application/Google Chrome.app" does not exist.

If someone can give me a hand, I'll appreciate it
# 2  
Old 07-27-2012
Bug try quoting the path in double quotes..

Code:
1)  
cd "/Users/myuser/Application/Google Chrome.app"

2) 
df_p="/Users/myuser/Application/Google Chrome.app"
cd "$df_p"

# 3  
Old 07-27-2012
Or escape the spaces:
Code:
cd /Users/myuser/Application/Google\ Chrome.app

# 4  
Old 07-27-2012
Sorry, I should have said this, the directory I try to access is not

Code:
/Users/myuser/Applcations/

but the root one:

Code:
/Applications/

that's why my path is /Applications/Google Chrome



tried double quoting but still get this:

Code:
The file /Users/myuser/Chrome.app does not exist.

---------- Post updated at 12:02 PM ---------- Previous update was at 11:39 AM ----------

I'll try to explain better myself

my path variable do a command that with a query of "Google C" outputs this:
Code:
   /Applications/Google Chrome.app

when I do:
Code:
open $path

works like a charm with names with no spaces like:
Code:
/Applications/Burn.app

But when the name have spaces it seems like it misleads the search to my user application directory:
Code:
The file /Users/myuser/Chrome.app does not exist.

when I use the doble quotes even the single named Applications get mislead to my user directory

Code:
open "$path"
The file /Users/jonathanwiesel/   /Applications/Burn.app does not exist.
The file /Users/jonathanwiesel/   /Applications/Google Chrome.app does not exist

# 5  
Old 07-27-2012
I don't think the contents of your variable are what you think they are.

1) Show your code, please, all of it.
2) Don't use 'path' or 'PATH' as variable names, PATH is a special variable controlling where commands are found, changing it will cause problems.
# 6  
Old 07-27-2012
Code:
p={query}
number=$(ps aux | grep "${p}" | grep -v grep | wc -l)

path=$(ps -A | grep "${p}" | grep -v grep | awk '{$1=$2=$3=""; print  $0 }' | awk -F.app '{ print $1 }' | uniq )

if [[ $number -gt 0 ]]
then
	sudo killall "${p}"
	sleep 1 
	open $path.app
	echo ""${p}" - Process Restarted";
else
	echo ""${p}" - Not Running"
fi

{query} is argument I pass thru an app launcher called Alfredapp, it's like Quicksilver.

I tried changing the variable's name but no luck
# 7  
Old 07-27-2012
The way you're setting path is clearly injecting leading spaces. So when an application without spaces, using unquoted expansion, the variable containing path=" someapp" would expand from open $path to open someapp. because of word splitting the leading/trailing whitespace are gone.

now you want to preserve spaces in your variable, and you quote the expansion.... those spaces are still there as i can tell from
Code:
The file /Users/jonathanwiesel/   /Applications/Burn.app does not exist.

So the fault is with how you are obtaining that variable.

I do not use Mac OS X but I assume ps takes BSD options. You can have it print just the command line! Is pgrep available? I'd suggest using pgrep to get the pids of matching processes, then use ps -p $pid -o cmd=

Last edited by neutronscott; 07-27-2012 at 02:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

OSX bash & expect

I have a script that must perform a 'sudo' operation on each of a number of hosts. I'm trying to get expect working so I only have to enter it once, and have run into a couple of issues. First, several examples suggest to use: /usr/bin/expect <<EOD spawn ssh -t $host /usr/bin/sudo -v... (7 Replies)
Discussion started by: jnojr
7 Replies

2. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/myscript.sh <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/myscript.sh #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

3. OS X (Apple)

bash script for dseditgroups in OSX

Hi there, Hope this isn't too complex of a script to try to make but this is what I'm trying to do. Create a new group and add local users that aren't admin to the group. So let's say the new group is 'cats' and 'dog' is the only admin on the machine. Let's say most machines only have one... (4 Replies)
Discussion started by: stop.the.stupid
4 Replies

4. Shell Programming and Scripting

#!/bin/bash and #1bin/sh command not found error on mac osx terminal/shell script

i am having a weird error on mac os x running some shell scripts. i am a complete newbie at this and this question concerns 2 scripts. one of which a friend of mine wrote (videochecker.sh) a couple weeks ago and it's been running fine on another machine. then last week i wrote capture.sh and it... (2 Replies)
Discussion started by: danpaluska
2 Replies

5. OS X (Apple)

Check for open application

Hi there, Is there any command to check if a particular application is open on a mac? thanks :) (3 Replies)
Discussion started by: davewg
3 Replies

6. Shell Programming and Scripting

Open file with correct application

Can someone please help me with my bourne shell script. I am a struggling newbie. I need create a script that will read an argument from a command line, access a config file with application file types, and open the file with the correct application. The file needs to be able to handle file... (3 Replies)
Discussion started by: 3dtiger
3 Replies

7. UNIX for Dummies Questions & Answers

Upgrading bash on Darwin (osx)

Hi, I have installed bash 3.2 via darwin ports, however when I try and change the shell i.e. chsh -s /opt/local/bin/bash is says its a non-standard shell? but if i run ./bash i get a new bash prompt with version 3.2? Thanks (3 Replies)
Discussion started by: c19h28O2
3 Replies

8. UNIX for Dummies Questions & Answers

Which application has a TCP socket open

If I do a netstat -a I can see all the sockets currently open, is there a way that I can tell which application is holding open these sockets ? (3 Replies)
Discussion started by: murphyboy
3 Replies

9. OS X (Apple)

Help! What is best language to use to build OSX application?

Hi, We need to build an application for work that will run on OSX. It will be fairly simple, in which the user will use a mouse to grab objects on the screen and move them to a target location. The program will need to record information about accuracy and speed, and output that data at the end. ... (1 Reply)
Discussion started by: binarybabe
1 Replies
Login or Register to Ask a Question