Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newb commandline scripting and c/c++




On Aug 24, 2008, at 08:38 , Steve wrote:

Again forgive me if this is the wrong list.
I looked over all of the mailing list and there isn't one for:
	newbie shell scripting
	beginner commandline c/c++

This is not the place for learning either Shell Scripting or programming in C/C++, but it's fine for some of your questions. There are plenty of sites out there with this kind of support; mailing lists; etc. If you belong to (or Join) The ACM, they have online courses for such as this. Also, check out O'Reilly's website; they have many "in a nutshell"-type books on these subjects.


Also, join the Apple Developer Program. There are various levels of participation, from free to expensive, and there is a boatload of information available at
<http://developer.apple.com>


One more thing: it's a really bad idea to put your stuff in "system" directories (like /System and /usr). Two alternatives are:
- use /usr/local for your stuff (e.g., put 'll' in /usr/local/bin)
- make your own private 'bin' directory, and put it in your PATH:
- put 'll' in, say, ~/bin, and
- put ~/bin in your PATH variable (in your personal shell startup file)
The latter may be the best, because you don't need to fuss with permissions.


I then opened a new file with vi calling it myc and I put the following line in it:
cd /Developer/sandbox/myc


I then
	chmod +x myc
	sudo mv myc /usr/bin

however when I myc nothing happens, literally, no error, and no change in directory no matter where I am.

Ha! There's a trick here, intimately involved with the way Unix works.

First, 'cd' is not a command-line program; it is what's called a built- in function in the shell. There's a reason it's built into the shell, as you discovered. The 'cd' command (and its underlying system call) changes the "current working directory" for the process making the call. The sole purpose for this is to permit "relative file naming": you can use "a/b/c", to locate the file 'c' relative to the working directory for the process in question, instead of having to know the path to that working directory.

The tricky part is this: you want to do this in the shell as well as in a program, so you can say, for example,
cat foo
instead of the laborious and error-prone
cat /very/long/path/to/the/file/foo


But: if you execute the 'cd' command/system call in a process, the process is spawned, the directory is changed *for that process*, the process exits, and things are as they were before (i.e., the working directory for the shell is the same as before your "myc" command was executed.

Therefore, as a special case, shells will implement 'cd' directly, so that the effect of the 'cd' lingers on.

Hope that is clear; if not ask more.

I use to get into my .profile file on an AIX machine and I used
alias ll 'ls -al'
alias myc 'cd /Developer/sandbox/myc'

Is there a similar way to use aliases on the mac so I can have typing short-cuts when on the commandline?

Same trick should work here. The only thing to be wary of is what shell you are using. The default for Mac OS X is 'bash'. The man pages will describe what to do for whatever shell you choose.


HTH

Justin

--
Justin C. Walker, Curmudgeon-At-Large, Director
Institute for the Enhancement of the Director's Income
--------
The path of least resistance:
it's not just for electricity any more.
--------



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden
References: 
 >newb commandline scripting and c/c++ (From: Steve <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.