Oct 8, 2011

The Tilde (~) in Unix and URLs

A tilde (Punctuation mark)
Image via Wikipedia
UNIX

In the Unix shell, the tilde (~) is an abbreviation for the path to your home directory. (It has other expansions but this the meaning when the tilde is used as a separate word.)

Tilde expansion is the process of converting the abbreviation to the path to the user's home directory.

Example (not using ~)

/home4/susanQ/public_html/111/ is the Unix path to Susan Queue's 111 folder on shell.uoregon.edu.

Example (using ~)

~/public_html/111/ is the Unix path to Suzie's folder on shell.uoregon.edu

Example

$ pwd
/tmp

$ cd ~

$ pwd
/home4/susanQ/

$ echo $HOME
/home4/susanQ/

(~ is a synonym for the value of the shell's HOME variable)

URLs

In a URL, the tilde (~) has a related but different meaning: it stands for the path to your public_html folder on the web server.

Example: 

URL:
http://uoregon.edu/~susanQ/

Corresponding Unix path on the server:
/home4/susanQ/public_html/

Example: 

URL:
http://uoregon.edu/~susanQ/111/
Corresponding Unix path on the server:
/home4/susanQ/public_html/111/

Enhanced by Zemanta

Sep 1, 2011

1-Armed Bandits && Demorgan's Law

When simulating the spin of a three-wheel slot machine, how do we detect three of a kind?





The following condition recognizes three of a kind:

var s1 = 'lemon', s2 = 'lemon', s3 = 'lemon';
console.log((s1 == s2) && (s1 == s3) && (s2 == s3));

We can modify it to control a loop that spins the wheels:

       s1 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s2 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s3 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);

       while (!((s1 == s2) && (s1 == s3) && (s2 == s3))) {
           //spin again...
       }

Applying Demorgan's Law we get:

       while (!(s1 == s2) || !(s1 == s3) || !(s2 == s3)) {
           //spin again...
       }

Equivalently:

       while ((s1 != s2) || (s1 != s3) || (s2 != s3)) {
           //spin again...
       }

To hit the jackpot, we must spin the wheels at least once. This leads to a final, simpler version using a do-while:

do {
       s1 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s2 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
       s3 = RandomOneOf(['lemon', 'cherry', 'bar', 'donut']);
} while ((s1 != s2) || (s1 != s3) || (s2 != s3));

The do...while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested.

It is a post-test loop; the while loop and the for loop are pre-test loops.

=> Use a do-while when an action must be performed at least once.


Jul 1, 2011

Web Apps w/o Programming: If this, then that (IFTTT)

IFImage by alex drennan via Flickr
IFTTT. Put the internet to work for you by creating tasks that fit this simple structure:
if this then that.

 A Web Tool That Lets You Automate the Internet. (NYT)

7 Ways to Automate Your Life With ifttt. (NYT)

Mar 1, 2011

SSH and the Unix Shell's Erase Key

Polished Turtle Shell
SSH sometime uses the Delete key to erase characters, but I prefer to use the Backspace key. How do I set it up?

You have several options, as follows:

  • Windows: Start SSH, select Edit > Settings > keyboard, then select Backspace sends Delete (or whichever key you want to use), then click OK.

  • OS X: Start Applications > Utilities > Terminal, the select Terminal > Window Settings..., then select Keyboardin the drop-down list, then check Delete key sends backspace.

  • Set your Unix shell's Erase key--

    Use Pico or Emacs to edit your login shell's startup file, .bash_profile:

    $ pico .bash_profile

    Add this line to the .bash_profile:

    stty erase '^h'

    save the file and exit pico.

Customizing your Unix Command Line Prompt

UnixImage by p373 via FlickrHow can I get the path for the current directory displayed in my Unix prompt?

Read Generic UNIX Interactive Prompts