Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am currently writing some convenience methods for my terminal in my bash_profile and am sure if what I am writing is "the best way". I figure a good way to verify whether what I'm doing is right or not would be to find some source code of more established programs and see how they do it.

My question then is, where can I find this code on my Mac? An example is, with Macports installed, where is the source code that opens the port interactive console when I type nothing but "port" in my shell?

(I added Linux in the title even though I am on a Mac because I assume the answer would be the same for both)

Edit: The answer I am looking for is in terms of which directory relative to the programs will I find their unix scripts.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Many of the commands are compiled C code (or other languages such as C++ or Objective C) rather than shell scripts (some could be Perl, AWK, Python, etc.). You can use the file command to find out which they are.

$ file /bin/grep
/bin/grep: ELF 32-bit LSB executable ...
$ file /bin/which
/bin/which: POSIX shell script text executable

You can use something like this to find shell scripts on your system:

find /bin -executable -type f -exec bash -c 't=$(file {}); if [[ $t =~ shell\ script ]]; then echo {}; fi' \;

Substitute different starting directories to search various parts of your filesystem. Use -perm -111 instead of -executable if your find doesn't have the latter.

share|improve this answer
    
Just tried it out! Worked perfectly! Thanks! –  AndrewKS Jan 6 '11 at 19:30
add comment

Bash scripts are well, in bash, aka plain text, you can open them in a text editor.
However, most *nix tools are written in C/C++.
I highly recommend reading BASH Programming - Introduction HOW-TO, Advanced Bash-Scripting Guide and this bash4 guide.

And as a personal preference, I recommend checking python too.

share|improve this answer
    
I know they are in plain text, but where? I'm not worried about learning the syntax, but rather would like to be able to read the source so I can get a good idea of how the scripts are written and compare them to how they run. That Advanced Bash-Scripting Guide seems very useful though - thanks for that. –  AndrewKS Jan 6 '11 at 19:21
    
If they are installed and in path, you can just type 'which scriptname', if they aren't executable/in path try 'locate scriptname'. –  OneOfOne Jan 6 '11 at 20:47
add comment

If I read this correctly you are looking for the "master" or .bashrc files?

The main bashrc is located here /etc/bashrc and your own will be hidden in your home directory ~/.bashrc the home directory copy comes from /etc/skel/.bashrc

Now the "port" command is just a link to the /usr/bin/port as you can see by going into /usr/bin and scrolling though the files. Each one will work because of your $PATH vaiable(which can be set in bashrc) If you want to see what your $PATH is simply type it in the terminal.

These instructions are for Linux and may not be the same on your Mac.

I also suggest looking into oneOfone's links.

regards

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.