h1

Create / Call a function

October 18, 2009

The function has to go at the top, or be in an external file. The rest of it just goes below the function, doesn’t need to be encapsulated into anything. (That feels a bit odd for me, there’s not even a “main” or anything..)

function_name()
{
  echo "neko equals $1"
  echo "kami equals $2"
}
 
echo "Hi! I'm doing stuff."
function_name $neko $kami

h1

Shell script if statement

October 18, 2009

Syntax. (Programming languages should not care about whitespace. I should be able to type [$neko="hi"], but unfortunately that doesn’t work.)

if [ $neko = "hi" ] && [ $kami = "there" ]; then
  # do stuff
elif [ $neko = "hi there" ]; then
  # do other stuff
fi

Greater than and less than for strings:
[ "$var1" \< "$var2" ] # true if var1 < var2
[ "$var1" \> "$var2" ] #true if var1 > var2

*Note: the quotes are needed around the variables if they have multiple words in them (that is, if there’s spaces). Seriously…wtf? ><

Integer comparisons (this page goes into detail on many different comparison operators for both strings and integers; I’ve copied most of them into here, in case it happens to disappear for whatever reason..):
[ num1 -eq num2 ] # true if num1 == num2
[ num1 -ne num2 ] # true if num1 != num2
[ num1 -lt num2 ] # true if num1 < num2
[ num1 -gt num2 ] # true if num1 > num2
[ num1 -le num2 ] # true if num1 <= num2
[ num1 -ge num2 ] # true if num1 >= num2

String comparisons:
[ str1 = str2 ] # true if str1 == str2
  # Note: [ str1=str2] assigns str2 to str1, I think.
[ str1 != str2 ] # true if str1 != str2
[ -n str1 ] # true if str1 is not null (length > 0)
[ -z str1 ] # true if str1 is null (length == 0)

File comparisons:
[ -f filename ] # true if the file exists & is a normal file
[ -d filename ]# true if the file exists & is a directory
[ -s filename ] # true if file is not empty
[ -r filename ] # true if file is readable
[ -w filename ] # true if file is writeable
[ -x filename ] # true if file is executable

Multiple conditions:
[ cond1 -a cond2 ] # true if cond1 && cond2 is true
[ cond1 -o cond2 ] # true if cond1 | cond2 is true
[ ! cond1 ] # true if cond1 is false

h1

Twitter tweet graph

October 18, 2009

http://tweetstats.com/

It’s kind of interesting.

h1

Python substring

October 17, 2009

This page goes into more detail about halfway down (search for ‘Strings can be subscripted’), but this will be a general overview so I don’t have to go to that when I need to get a substring in Python.

This is the string I’ll work with:
strKami = "Kami"
+---+---+---+---+
| K | a | m | i |
+---+---+---+---+
0   1   2   3   4
-5 -4  -3  -2  -1

So unlike C++, characters are not referred to as a particular index of a string (i.e., strKami[0] == ‘K’), but as the space between two indexes.

strKami[0:1] == 'K'
strKami[-3:-1] == 'mi'
strKami[2:] == 'mi'
strKami[:2] == 'Ka'
strKami[3:243] == 'i' # "an index too large is replaced by the string size" (i.e., 243 is replaced by 4)
strKami[3:2] == '' # if lower bound > upper bound, returns empty string
strKami[243:] == '' # there is nothing here, so empty string

h1

Saving standard output to a variable

October 16, 2009

If you want to save the standard output into a variable, rather than just printing it to the terminal or saving it in a file and then having to read it back in again:

# the output from command will be stored in variable_name
variable_name=`command`

# this will set temp to "Hello world"
temp=`echo "Hello world"`

h1

Robots.txt

October 16, 2009

This has to go in the root directory for the website, and each subdomain needs its own robots.txt.

To disallow all search engine spiders for the entire site:
User-agent: *
Disallow: /

Specific folders (for all robots):
User-agent: *
Disallow: /images/
Disallow: /users/avatars/

Crawl delay:
User-agent: *
Crawl-delay: 243 # in seconds

Allow specific pages only (not all search engines listen to this;  for compatibility, put allow before disallow for related folders/files):
User-agent: *
Allow: /images/index.html # a single file can also be disallowed
Disallow: /images/

Sitemap (only supported by some):
Sitemap: http://www.kamikode.com/misc/sitemap.xml # can be on hosted on an external website

h1

Save exit code / cmp command

October 15, 2009

I don’t use Linux as my operating system (I’m on Vista currently), but for one of my classes this quarter, I’m learning how to code shell script.

return_val=$?

This will get the exit code of the previously run command. 0 means exited successfully, and 1 and higher mean different errors depending on the command. (For example, for the cmp command used with the -s option, it returns 0 if the two files being compared matched, 1 if they didn’t, and 2 for other errors.)

cmp -s "$fileOne" "$fileTwo"

h1

Make a webpage editable

October 14, 2009

This javascript can be used in the address bar to make any webpage editable (the changes don’t show up for anyone else, obviously, and are lost once you leave the page). It’s like a WYSIWYG version of Firebug…

javascript:document.body.contentEditable ="true";document.designMode="on";void 0;

h1

Favicon for one page

October 14, 2009

This allows you to set a different favicon for one specific page, than the default favicon for the rest of the site.

<link rel="shortcut icon" href="http://www.google.com/favicon.ico" />

h1

Goodnight, moon

October 14, 2009

Welcome :)

The main purpose of this blog is to post useful code snippets, for all those times I finally figure out how to do something, and want to be able to reuse the same code later on. Hopefully at least some of these snippets can be helpful to others as well.

Anything unrelated to a specific programming language will be filed under ‘Miscellaneous’. (Unless I add a couple other non-programming language categories – I’m considering ‘geek humor’, ‘tech news’, and ‘favorite webcomics’.)

I don’t plan on getting into anything personal in this blog – the most I’m going to say is that I’m currently in my second year of college, working towards a computer science degree, and I wish I had a cat.

Follow

Get every new post delivered to your Inbox.