Entries Tagged 'Entry Level Programming' ↓

Fantastic Objective-C Courses

If you are interested in Objective-C programming, or just plain old C programming, you should check out any books written by Stephen Kochan.  I recently took his Objective-C online course (live streaming), an 8-lecture course, with a fantastic forum, for an astounding $75.  If you are new to programming in general, don’t fret.  Stephen walks you through it in an easy to understand way.  Knowing the libraries, on the other hand, will take time.  Nothing but your own ambition to learn those will get you there, though.  You can find Stephen’s courses here: classroomm.com

His accompanying book, necessary for the course, can be found here: Objective-C 2.0

Moving Around in Vim

I’ve written a couple of beginning UNIX for OS X entries, such as creating a .bash_profile and .bashrc file using vim.  Here is another vim tutorial for moving around in the screen editor, so you can see just how powerful it’s commands can be.

This post will deal with mostly command mode, the default mode when you open vim (or an existing file with it.)  To move the command marker around the file without entering INSERT mode consists of four basic keys.  Moving to the next/previous word, to the beginning of a paragraph, etc. will require a bit more memorization.

You want to first familiarize yourself with the h, j, k, and l keys.  The outer two (on a standard keyboard) move your cursor left or right one-character;  h, to the left, and l to the right.  j will move your cursor up to the next line, while k moves it down one line.  The fact that you are in command mode means no changes are made to your text at this point.  You can use the arrow keys for the same functionality, but getting used to the keys will help you keep your hands where they belong–typing and editing text.

Moving to the beginning of the current line is done by pressing O (zero).  To move to the end of the current line press $+ moves your cursor to the beginning of the next line, and – moves you to the beginning of the previous line.

Next are some basic ideas for commands.  Some will take a number argument, followed by a command.  Some can be written with a number argument followed by another command.  For example, x in command mode means delete a character.  To delete a word, or the rest of a word if you are in the middle of one, you type dw.  If you precede x with a number, n, vim will delete the following n characters from the line.  The command to delete the next four characters would be written, simply, as 4x. To delete the next four words, you would type 4dw.

Vim is a very capable text editor once you learn more about its commands, and get used to bouncing between INSERT and command modes.  Below you can find a table of common commands and their function, at least enough to get you on your way as a skilled vim user.

Movement Command

Function

h

Move Left One Character

j

Move Down One Line

k

Move Up One Line

l

Move Right One Character

0

Move to First Character Of Current Line

$

Move to Last Character Of Current Line

+

Move to First Character Of Next Line

w

Move to Next Word or Punctuation Mark

W

Move to Next Word

e

Move to End of Current Word

E

Move to End of Next Word

b

Move Back to Beginning of Word or Closest Punctuation

B

Move Back to Beginning of Word

)

Move to End of Current Sentence

(

Move to Beginning of Current Sentence

}

Move to Start if Next Paragraph

{

Move to Start of Previous Paragraph

Delete Command

Function

x

Delete Current Character

X

Delete Character Immediately Left Of Cursor

dw

Delete Current Word

10dw

Delete Ten Words

D

Delete To End of Current Line (also d$)

dd

Deletes Current Line

20dd

Deletes Twenty Lines

dG

Deletes From Cursor to End

u

Undo (doesn’t work for single character deletion)

Cron Jobs on OS X

Since OS X Tiger, cron has been replaced by a utility called launchd, and three separate launch daemons. Rather than having to run crontab  to manipulate scripts, they are run by launchd according to three separate directories inside of /etc/periodic.
The scripts contained in these folders are run at specified intervals by three preference files, in XML format, found in the /System/Library/LaunchDaemons folder. You can manipulate the .plist files with a text editor or Apple’s Property List Editor (if you’ve installed Developer Tools.) They are named, simply enough, com.apple.periodic-daily.plist, com.apple.periodic-weekly.plist, andcom.apple.periodic-monthly.plist. They are, by default, set to run at the same time as the old cron jobs, in the middle of the night. If you happen to shut down your Mac, it might be a good idea to change these intervals to a time when you’re sure the computer will not be shut down, as these jobs are important.

You will find a script in each of these folders called 999.local.  This file is set to read-ony by default, and is for  ”backwards compatibility  with the old /etc/daily.local” according to the comments in the file.  (I’m running Snow Leopard.)  I’d recommend not modifying the scripts in the /etc/periodic folders, and creating a /etc/daily.local, /etc/weekly.local, and /etc/monthly.local file for your cron jobs, as you have the 999.local script in each of the daily, weekly, and monthly sub-directories of /etc/periodic to tell your scripts to run.  Any future system updates could change the default files in those three directories, so if you modify those you may end up losing your scripts. Avoid that by setting up your own.

If you are running aTiger, the 999.local file may not exist, and you will have 500.daily, 500.weekly, and 500.monthly files inside their appropriate /etc/periodic directories.  You should still create a daily.local, weekly.local, and monthly.local script file and place them in the /etc directory, and they will be called  from the respective 500.* file.

Setup Your .bash_profile On OS X | A Sample Bash Profile

This is another addition to my entry level OS X shell category.  This one covers setting up a more detailed bash profile, so certain things are done by default when you open your Terminal.

We start by opening the Terminal in your ~/Applications/Utilities folder. If you don’t know what that means please start with this post, as it will show you a very basic beginning to the Terminal in OS X.  Be sure you are in your home directory by typing pwd at the bash prompt and hit enter.  It should look similar to this:

ssBashProfileSetup1

Next, we start vim opened to your .bash_profile.  At the bash prompt type vim .bash_profile and hit enter.  If the file exists, it will open it, if not it will create one for you.     Your Terminal window should now look like this:

vim .bash_profile

My screen shows one alias I created in my last tutorial. I like that one but technically it doesn’t belong in your .bash_profile, it belongs in a file called .bashrc. The .bash_profile is for your login shell options, and the .bashrc file is read for subsequent interactive shells;  meaning a shell opened to type commands, not just to run a script from a file automatically.  You can launch another shell on top of your login shell by typing the shell name at the prompt, i.e., bash, and then hitting enter.

This lesson will focus on login defaults, such as the PATH variable in your .bash_profile. In the process, we will be using vim, so if you are unfamiliar with that, practice makes perfect!

The PATH variable is where the system searches for binaries (shell scripts, programs, etc.) to execute.  All UNIX systems provide a default path, but you can add to it.  As you add scripts of your own creation and such, you probably want to create a directory in your home folder to store them.  This way if you screw something up, it will affect you and not the system.  For example, an erroneous rm command if you are in the /bin directory could be really bad news.

Let’s go ahead and first create a new directory called bin in your home directory.  Create a new interactive shell by pressing ⌘-N.  Type mkdir bin and hit enter.  Now type ls and hit enter.  You should see the new directory listed with your other folders:

mkdir Command

This is where you can store any shell scripts that you create, as they are easy to find in this folder.  In vim, type “i” without quotes to get you into INSERT mode.  You will see –INSERT– at the bottom of your Terminal window.  Now you can begin typing text.  You should comment your script files, and your other various profiles so you know what something does if you ever need to edit.  It’s just good programming practice to get the comments done as you progress with coding, not when you’re done!

To make a single line comment, the first character on the line needs to be the pound sign, #.  Type in #Additional binary folders, and hit enter.  Next, we set the PATH like so, to *add* to the default PATH: export PATH=$PATH:/Users/yourusername/bin

You can see in my screenshot below what the file should look like.  Replace the yourusername with your actual username for your account.  Mine is Tech, so that’s how it shows in my screenshot.  Also keep in mind, that UNIX is case-sensitive:

Bash Profile PATH

Now you can save this by hitting the escape key, and typing :w then pressing enter.  Let’s add some more commands to the profile.  We can set a welcome message to the login, and set the shell’s timer to check for new mail.  This really only matters if you use a text-only mail client, such as PINE or Alpine.  It will not affect the OS X Mail client.

Set a message to display when you login with the echo command.  This is a good command to know for the command line as well, as you can see certain system variable settings, such as your current PATH.  You can do this by typing echo $PATH at the bash prompt and hitting enter.  To use echo in your .bash_profile to set a welcome message, type echo followed by the message:

echo Welcome back, Mr Awesome!  Your present working directory is: $PWD

looks like this when you log in:

Welcome Message SetTo set the mail check timer, write a line in your .bash_profile like so:  export MAILCHECK=30

The time set is in seconds, and OS X’s bash shell by default is set to 60 seconds.  Your .bash_profile should now look similar to this:

Finished Profile Example

Next time I will cover creating a simple shell script, and changing file permissions to run them.  Also, I will try to cover some common aliases, as well as creating your .bashrc file, which is where we store the aliases.  Thanks for reading, and if you have any suggestions or questions, please feel free to ask!