PDA

View Full Version : Unix and Xcode question




mdeh
Feb 11, 2009, 11:04 PM
I am trying to call the executable of a program ( ObjC) ( Very simple ) called 16.1 from the command line with these commands. ( Given that the following is in a terminal window)

MB-Pro Debug m$ pwd
/Users/m/Xcode_practise/16.1/build/Debug
MB-Pro Debug m$ ls -F
16.1*
MB-Pro Debug m$ /Users/m/Xcode_practise/16.1/build/Debug/16.1
2009-02-11 20:51:09.918 16.1[499:10b] Hello, World!
MB-Pro Debug m$ 16.1
-bash: 16.1 command not found

So, if I am correct, I am in the PWD when calling the executable, but it only responds to the full - path. Could someone perhaps explain why this is so, in other words, why the invocation "16.1" will not work?

Thanks as usual in advance.



lee1210
Feb 11, 2009, 11:24 PM
I can't remember the defaults in OS X, but it is safest to exclude the current directory (.) from your path. This is more of a concern on multiuser systems, where someone can put a file called ls somewhere that actually deletes everything in your home directory. It's still a good idea on any system, so when you need to run an executable in your current directory, just run ./progname. In this case
./16.1

-Lee

mdeh
Feb 11, 2009, 11:40 PM
I can't remember the defaults in OS X, but it is safest to exclude the current directory (.) from your path. This is more of a concern on multiuser systems, where someone can put a file called ls somewhere that actually deletes everything in your home directory. It's still a good idea on any system, so when you need to run an executable in your current directory, just run ./progname. In this case
./16.1

-Lee


Lee...could you elaborate just a little ( and thanks for that reply) what exactly I am calling with ./16.1

I assume??:) that . is the current directory ( as you say), the slash just separates the directory from the executable? If so, why can the shell not find the executable in the current directory when it is called alone? For example, cal just calls the calendar.
Thanks in advance.

plinden
Feb 12, 2009, 12:17 AM
lee1210 is correct in that the current directory is omitted from the $PATH environment variable (unless you're in a directory which is in $PATH).

You can see $PATH by typing: echo $PATH - notice that's it's a series of directories. When typing a command, OS X (and any other *nix OS) looks through the contents of $PATH from start to end and executes the first matching command it meets.

Not including the current directory is a security measure. Commands such as ls or cd are in /usr/bin, which is in $PATH, but if someone fooled you into downloading an executable called cd that is in the path before /usr/bin, it would execute before the correct cd command.

The PATH could include the current directory at the end (and you can set PATH to include it) but t
yping ./command specifically tells the OS to run command that's in the current working directory, which is something you have to think about so is not something you would do accidentally.

Doctor Q
Feb 12, 2009, 12:19 AM
When you type a command, the shell notices the command is just a name or if it is a path, which means a string with a / in it.

If it has a /, it uses that path to find the file.

If it doesn't have a slash, and it's just a name, the shell looks in a collection of folders for a file with that name. It executes the first matching file. This collection of folders is called your path, and can be see by typingecho $PATH
You can set the value of PATH to be any collection of folders, although the way you set it depends on which shell you are using. Yes, there's a choice of shells.

But back to the case where you type a command with a path. If the path starts with a /, it is an "absolute" path and tells the shell exactly where on disk your program file is, from the top of the disk structure on down.

If it has a / in the middle, but not at the beginning, then its a "relative" path, and tells the shell where to find the program file starting in the folder you are currently in. The commandpwdtells where you currently are.

Now we can explain your case.

When you type16.1the shell is looking in a collection of folders from your path, but the folders apparently don't include the current folder. If you changed your path to include the current folder, it would find your "16.1" program.

If you type./16.1 as lee1210 suggests, you are giving a relative path, so the shell will look exactly one place: where you said. By convention, "." means "the folder I'm in right now, so ./16.1 means the program file named 16.1 in the current folder!

mdeh
Feb 12, 2009, 07:19 AM
When you type a command, the shell notices the command is just a name or if it is a path, which means a string with a / in it.

thank you for that wonderful explanation. Much appreciated.

mdeh
Feb 12, 2009, 07:20 AM
lee1210 is correct in that the current directory is omitted from the $PATH environment variable (unless you're in a directory which is in $PATH).

You can see $PATH by typing: echo $PATH -

Thank you for that great info.
Michael.

mdeh
Feb 12, 2009, 07:22 AM
I can't remember the defaults in OS X, but it is safest to exclude the current directory (.) from your path. This is more of a concern on multiuser systems, where someone can put a file called ls somewhere that actually deletes everything in your home directory. It's still a good idea on any system, so when you need to run an executable in your current directory, just run ./progname. In this case
./16.1

-Lee

Lee...thank you again. Your answer makes (more) sense to me now...and, btw, thanks for always trying to answer my queries. It's people like you and the others that make this foray into programming more pleasant.