Print This Post

“The time has come,” the Walrus said, “to talk of many things.”

In my last blog entry I complained that my web site designs look very 1994. I spent Friday morning putting Damn Small Linux(4.4.10) on two computers that I have (a laptop and a desktop) and, after taking a look at this blog from one of them, I am encouraged to continue to get my site going using my own lo-bandwidth designs.

That and I have an artist friend who made a little mascot for the site. Her name is Aster (undoctored scan of the original sketch). Actually, I suspect I asked for and got a young male magician but the name Aster is too perfect for my site’s mascot.

Aster

I chose DSL because it will work on the old machines I have handy and I love the small footprint, lightly-fleshed-bones (not quite bare-bones, just lightly fleshed). It has enough stuff built-in that I could use it right off the bat but there is plenty of room to add packages on and install things. Whee! I’m sure my work on getting my DSL machines to where I want them will provide articles for my website. All I’ve really done so far is follow the directions for installing, upgrading to GNU Utilities and Enabling Apt. But that stuff is so easy, I’ll let anyone who wants to install DSL just use those instructions.

 Speaking of which… I don’t want this to just be a blah-blah-blog entry so I’ll run through something I have already done with DSL.

Changing the hostname

Changing the hostname, DSL defaults to box for the hostname, was actually pretty easy, though it took several steps. There were a couple other steps on some sites but these worked for DSL and might work for your version of Linux. You must be logged in as root to do these steps. 

First, I edited /etc/hosts with the command vi /etc/hosts. The first line of the file ran as follows:
127.0.0.1          box   localhost
I replaced the word box with the name I wanted and then saved the file.

I then edited /etc/hostname to contain the new hostname (the hostname is the only thing in that file).

I used the following command to see whether the machine name had changed in the kernel:
cat /proc/sys/kernel/hostname
As I expected, I got the default hostname. 

To correct that, I ran the command hostname mymachinename where mymachinename is the new hostname for my computer. I then re-ran the cat command above to make sure the hostname in the kernel had changed.

And that’s all for this episode :)

Print This Post

My Brain is Full

I am still not sure about this whole blog thing, I guess. On the one hand, WordPress and 1and1 (the blog software people and my provider, respectively) make it really easy for me to publish short articles without showing off my limited visual design skills. (If I could figure out a nice design, I could code it easily… its just that all my designs look very 1994… then again, maybe that would be so bad.)

Where was I? Oh yeah. I like the blog when I have a technical article to publish or a little computer related item to pass along. I actually feel like I am helping people (if anyone ever stumbled across my blog). But then sometime I don’t have the time or inclination (energy?) to write a helpful, technical article. And then you end up with one of these type of entries (but luckily nothing quite like this) where I just kind of do a diary-type entry.

But I know myself and if I tried to force myself to do something useful every time, I would lose interest and and up abandoning my blog, which I don’t really want to do.

Why don’t I have anything technical to say today? That would be because I just spent the last week doing an IBM course, AIX 6 Jumpstart for UNIX Professionals, and my brain is full. It was very informative and very intense (in the informative sense). I really enjoyed it and many of the instructors personal asides about UNIX people really resonated with me. To the extent that I think I may have found my calling: WalterAM - *IX Professional. (By *IX I mean UNIX, Linux and AIX. Saying UNIX-based gets a little boring.)

So, this blog and site might be shifting more toward *IX. I am still interested in programming so I am sure those topics will still show up. And databases. So… maybe nothing will change. ;P

Print This Post

Ch-ch-ch-changes with chmod

The chmod command is used to change permissions on a file on a Unix-based computer.

Unix permissions are specified in three sets: owner, group, and others. There are three types of permissions for each of these sets: read, write and execute. The are abbreviated as follows: r for read, w for write and x for execute. The permissions mean pretty much what you would suspect.

If we have a file called test.dat and we run the command ls -l test.dat, we might get the following:
-rw-r--r--     1 myid          staff             572 Mar  9 11:34 test.dat

The first dash denotes the file we are looking as a regular file. You might see a d for a directory. There are a couple less common entries you might also see in this first slot.

The next three columns, 2, 3, and 4 (in this case, rw-), are the permissions for the owner of the file (myid, in our example). The next three columns, 5, 6, and 7 (r--), are the permissions for the group listed (staff). The last three columns, 8, 9 and 10 (r--), are the permissions for other users of the machine.

There are essentially two ways to set or change permissions on a file: using an octal number or using a string.

First, I will explain how we figure out the octal numbers we would use. By the way, this is the way I learned how to change permissions. We build one of these numbers by thinking of a set of permissions such as rw- as a number derived from the following table. The table is actually derived from thinking of the permissions in terms of binary numbers… but that is a story for another day.

---   0
--x   1
-w-   2
-wx   3
r--   4
r-x   5
rw-   6
rwx   7

So, according to this table, rw- would be a 6. r--would be a 4. Thus, using octal numbers, the permissions for our example file would be 644.

We would have used this command to set the permissions of the example file using octal numbers:
chmod 644 test.dat

Using a string to set permissions is probably simpler. I say probably because using octal numbers is so ingrained in the *IX lobe of my brain that I often forget permissions can be changed with strings. When setting permissions this way you must keep in mind that while you might be the owner of a file, the system uses your userid to define to whom a file belongs. And also that otherusers can access your files. Thus, the permission sets are for the userid, the group and others.

Why am I making such a big deal about this? Because to set permissions using strings, we use the letters u, g and o to indicate which set of permissions we are changing.

After we define whose permissions we are changing, we need to know whether we are adding permissions (+) or taking them away (-).

Finally, we need to decide which permissions we want to twiddle: read, write or execute; and then we need to provide the single initial abbreviate such as r, w, or x.

Since the best way to show off changing permissions with strings is to actually change permissions, let’s imagine we want to change the permissions for the test file from rw-r--r-- to rwxr-xr-x. The command would be
chmod +x test.dat
since we are adding the execute permission to the user, group and other.

To then change these permissions (rwxr-xr-x) to rw-rw----, we would use this command
chmod -x,g+w,o-rw test.dat

chmod can also be used on multiple files and recursively step through directories (with the -R flag) but these are left as adventure discoveries for the reader. :)

Print This Post

Some Basic DELETE Queries

Basic assumptions

To review the basic assumptions made in the main article:

field refers to a field name.

table refers to a MySQL table. Tables are, of course, made up of records (also called rows) which are made up of fields. Tables house the fields that contain the data in which we are interested.

value is a value that is appropriate to the field we are discussing.

# is an integer such as 1 or 10.

If you are entering the queries at the mysql prompt, you will need to remember the semicolon (;). I leave them out because you do not need them when calling MySQL from PHP, for instance.

Delete queries

SELECT queries select (or collect) data. INSERT queries insert (or add) new records. UPDATE queries update existing records. DELETE queries delete existing records.

A few basic queries

The most basic of each of these queries does something to all of the rows in a table. A DELETE query is no different. In this case, the simple delete query DELETE FROM table deletes all rows from the table. For example, DELETE FROM tblCompanyCars deletes all the rows in tblCompanyCars.

To limit a DELETE query to only specific rows, you add a WHERE clause. The generic form of this query is DELETE FROM table WHERE field=value. A query to drop all company cars assigned to California offices might look like this: DELETE FROM tblCompanyCars WHERE state='CA'.

In the case of a long query, you might want to add a LIMIT clause. First the forms, then a little more explanation. Generic: DELETE FROM table LIMIT #. Specifically, if there are a lot of company cars, this query would be useful: DELETE FROM tblCompanyCars LIMIT 10. In our company car query, the application would delete 10 records then return control to the user or check to see if other processing needs to be done. In this way, the application is not just silently churning away, deleting the data; some feedback or interaction is allowed.

You can also order and limit the DELETE query, perhaps to delete the top (or bottom) so many items. Generically this query is written DELETE FROM table ORDER BY field LIMIT #. To delete the ten company cars with the lowest MPG (Miles Per Gallon) you might use the following: DELETE FROM tblCompanyCars ORDER BY mpg DESC LIMIT 10. The keyword DESC means order the data in descending order.

You can also combine the ORDER BY, LIMIT and WHERE clauses as follows: DELETE FROM table WHERE field=value ORDER BY field LIMIT #. An example of this might be DELETE FROM tblCompanyCars WHERE state='CA' ORDER BY mpg DESC LIMIT 5.

The examples again

DELETE FROM table 'all rows
DELETE FROM table WHERE field=value 'only specific rows
DELETE FROM table LIMIT # 'returns control in case of long query
DELETE FROM table ORDER BY field LIMIT # 'order by only useful with limit
DELETE FROM table WHERE field=value ORDER BY field LIMIT # 'only specific rows, ordered and limited

For another day…

I would still like to write posts on GROUP BY queries and WHERE clauses.

Print This Post

Still floating along

Well…fans of my blog (both of you), I’m still just floating along; as you can undoubtedly tell since I haven’t done anything useful on here in three weeks.

I’m still working major hours. I also read slashdot (the email digest version) and a couple other newsletters. I keep busy :)

We saw Star Trek last night. Awesome. The casting was awesome. The directing was awesome. The awesome was awesome.

and… that’s all I have to say for now.

Print This Post

Musta Jinxed Myself

Well… I had been going along pretty well there for a while, putting up a new blog entry every week or so. And then, as happens to me every so often, I sort of lose my way.

I love IT. I mean, I work in IT all day long and generally come home and play IT all night.

I work hard day and night on computery stuff but where am I really? For the knowledge and experience I have, I should be further than I am. I’ve decided that my problem is a lack of goals and the drive (?) to stick to those goals.

See, in part I don’t make goals because it seems like when I make them, I don’t stick to them. I suppose my goal of reading thousands of pages (and I do mean thousands) of computer books and be able to write small programs in 11 different languages (the majority of which I don’t know already) was a little much, what with being away from the house 11 hours a day for work.

But then I think, other people have done it and do do it. Some people have to work two or three jobs just to support themselves. But they have goals, even if unspoken.

 Well… anyway. I wanted you to know I am still out here and I’ll try to do something technical for next time. Or at least something about the IT stuff I’ve been doing. We’ll see :)

Print This Post

Some Basic UPDATE Queries

Basic assumptions

To review the basic assumptions made in the main article:

field refers to a field name.

table refers to a MySQL table. Tables are, of course, made up of records (also called rows) which are made up of fields. Tables house the fields that contain the data in which we are interested.

value is a value that is appropriate to the field we are discussing.

If you are entering the queries at the mysql prompt, you will need to remember the semicolon (;). I leave it out because you do not need them when calling MySQL from PHP, for instance.

UPDATE queries

SELECT queries select (or collect) data. INSERT queries insert (or add) new records. UPDATE queries update existing records.

A few basic queries

UPDATE queries, in their most generic form, will update all the rows in a table. This generic form is UPDATE table SET field=value. Notice there is no WHERE clause in this statement. As a specific example, we might say UPDATE tblArticles SET status=3. Thus, the status field in all the records in the tblArticles table would be set to 3.

If we only wanted to update certain records, we would have to use a WHERE clause to allow MySQL to select the records to update. In fact, you might use a SELECT query with a WHERE clause to test your query before actually running it as an UPDATE query.

A simple UPDATE query with a WHERE clause, generically would look like this: UPDATE table SET field=value WHERE field=value. Following on the example above, we might do UPDATE tblArticles SET status=3 WHERE id=31. This would change the status field of only the article with the id field equal to 31.

If we wanted to update several fields, we would seperate the information with commas, as in this generic example: UPDATE table SET field=value, field=value WHERE field=value. Specifically, we might do something list this: UPDATE tblArticles SET status=3, pubdate=CURDATE() WHERE id=31. This one again goes after the record where the id field equals 31. In this case, we update both the status field and the pubdate field. CURDATE() is simply a built-in MySQL function that returns the current date.

The examples again

UPDATE table SET field=value
UPDATE table SET field=value WHERE field=value
UPDATE table SET field=value, field=value WHERE field=value

For another day…

I still have DELETE queries to do to round out the basic query series.

I realized writing this entry that I should cover WHERE clauses. And, in looking over the articles in this series, GROUP BY clauses as well. Stay tuned! 

Print This Post

Some Basic INSERT Queries

Basic assumptions

To review the basic assumptions made in the main article:

field or fieldlistrefers to, respectively, a field name or list of field names (separated by commas).

table refers to a MySQL table. Tables are, of course, made up of records (also called rows) which are made up of fields. Tables house the fields that contain the data in which we are interested.

value is a value that is appropriate to the field we are discussing. For INSERT queries, we should also have a valuelist, which is also separated by commas like fieldlist.

If you are entering the queries at the mysql prompt, you will need to remember the semicolon (;). I leave them out because you do not need them when calling MySQL from PHP, for instance.

INSERT queries

When I discussed basic SELECT queries, I did not bother to explain that SELECT queries select or collect your data for you. In this case, however, it would probably be useful to mention that INSERT queries insert data into the database.

A few basic queries

The most basic INSERT query adds a record with the default values for each field. It looks like this in generic form: INSERT INTO table () VALUES(). A specific form might be INSERT INTO tblAddresses () VALUES().

In the next most complicated form, we want to specify some fields and values. If you specify fewer fields than are in a row of the table, the unspecified fields are given their default values. You could specify only one field such as INSERT INTO table (field) VALUES(value) or, a more specific example, INSERT INTO tblAddresses (zipcode) VALUES('13357'). By the way, the zipcode field in this table is not a number field, hence the single quotes.

You can also use SET with INSERT INTO. This form is easiest to see with a single field, as above. Generically: INSERT INTO table SET field=value. Following the specific example above, we would do: INSERT INTO table SET zipcode='13357'.

If you want to insert values for multiple fields, you would do something like this: INSERT INTO table (fieldlist) VALUES(valuelist). Continuing with our specific example, we might do INSERT INTO tblAddresses (city, state, zipcode) VALUES('Ilion', 'NY', '13357').

SET, though also valid for multiple field/value combinations, is a little uglier. Generically, it does not look too bad: INSERT INTO table SET field=value, field=value. When you actually convert the example above you realize that set would not work very well for a large number of fields: INSERT INTO tblAddresses city='Ilion', state='NY', zipcode='13357'.

The examples again

INSERT INTO table () VALUES()
INSERT INTO table (field) VALUES(value)
INSERT INTO table SET field=value
INSERT INTO table (fieldlist) VALUES(valuelist)
INSERT INTO table SET field=value, field=value

For another day…

I still am planning to UPDATE and DELETE queries.

Print This Post

Words to Live By

“When I get a little money I buy books; and if any is left I buy food and clothes” - Erasmus

Actually, I haven’t bought clothes in quite a while. We did, however, yesterday and today go to a local library used book sale. It was pretty cool. We got some 70 volumes for $67. I say volumes because several of the fiction books I got had more than one novel in them. Volumes, in this case, would refer to actual physical books.

So I have books waiting to be read. Very exciting. Also exciting is being able to add them to my online book database. I’ve decided to make it publicly available soon… just for fun. And I think I’ll also discuss the code here on the website.

Speaking of which, I’ve been thinking about redesigning the web site again. I like the slick look of the blog (I’m not much of a visual artist so I have difficulty coming up with a good look for the site) and the relative ease of providing content but I’d like to have more control over the look and structure.

I did just recently test out using nl2brto make posting content easier. nl2br is a PHP string function that inserts an HTML br tag wherever a line break occurs in some text. I use it like this:

$myFile = "somefile";

$fileHandle = fopen($myFile, 'r');
$myData = fread($fileHandle, filesize($myFile));
fclose($fileHandle);

echo nl2br($myData);

I have not gotten a chance to test it out with text that already has HTML tags. I shall have to try that.

Print This Post

GREP Options

Continuing on from my GREP basics entry, I am going to go over some of the options or flags for GREP. Some day I might try to tackle regular expressions but… then again, I may just chicken out. ;)

Basic assumptions

The most basic assumption I will make is that you have either read Beginner’s GREP Cheat Sheet or already know how to use the most basic form of GREP:

grep PATTERN FILE

PATTERN being what you are searching for in the file FILE.

Options go before the pattern. So, you would have:

grep options PATTERN FILE.

Context switches

-A # (lines after context)
-B # (lines before context)
-C # (lines on both sides of context)

These options will show you a number of lines after, before or on both sides of the match, respectively. For example:

grep -C 2 'not a number' funclib.php

looks for the text 'not a number' in the file funclib.php and, for each match, tries to display two lines on either side of each match. If we had used -A, we would have seen the two lines after the match, with -B, the two lines before the match.

Not the match

-v (show non-matching lines)
-L (list files that do not have matching line)

These options specifically refer to a non-matching line.

By using the -v option, you basically turn GREP inside out. For example, say you wrote a directory listing to the file dirlist. If you wanted to know which files were NOT .py files, you could use the following:

grep -v py dirlist

to return the lines in the file that do not contain the letters py.

The -L option lists only the names of the files that do not contain the text for which you are searching. So, keeping in mind that most Perl scripts start with #!/usr/bin/perl, you can use this information and the -L option to figure out which files in the current directory are not Perl scripts. Like so:

grep -L perl *

Information, please

-c (count matched lines)
-l (list files that have matching lines)
-n (show line number of matching lines)

The above listed informational options are each useful for different reasons. If you are interested in simply listing the files that match, use grep -l perl *.

The -n option prepends the line number of the match when it outputs. If you search multiple files, you will also see the file name prepended. If you are only searching in one file, only the line number is prepended.

The -c option also behaves in different ways depending on whether it is used against one or multiple files. When used as grep -c perl *, get a list of all the files in the directory with the number of matches listed. When used on a single file, grep -c will output only the number of matches in the file.

Making the match

-f (look in a file for the patterns to use to match)
-i (ignore case of pattern)
-w (match whole word)
-x (match entire line)

GREP normally matches the case of the pattern. Thus, the patterns Content and content are not the same. Unless, of course, you use the -i option.

Another default behaviour of GREP is to match part of a word. Thus, grep Walter index.html would match Walter and WalterAM (and WalterAM.com). However, grep -w Walter index.html would only match Walter.

You can use -x to match the entire line. In a Perl script, we need to use the statement require Exporter; when creating packages. If we want to find files containing this line, we could use: grep -x "require Exporter;" *.

The -f option allows you to list patterns in a text file, one per line, and use that to supply the patterns to be matched. If we have a file called toMatch.txt stored one level up (denoted by ../ in the command below) that contains the following:

bar.php
header.php
footer.php

and we used the command grep -f ../toMatch.txt *, we could check all the files in the current directory for those three file names.

Don’t be so greedy

-m# (stop after matching # lines in a file)

Normally, GREP would be termed “greedy.” That us, it tries to match the pattern as many times as it can. With the -m flag, GREP stops after # matches in a file. So, grep -m3 php * would find up to 3 matches in each file in the current directory but no more than 3 matches.

Using Multiple Options

Yes, Virginia, you can use multiple option flags. Here are a couple things you can do using multiple options.

  • Count the number of lines in a file that do NOT match the pattern: grep -cv pl dirlist
  • Count the number of lines in a file that match the whole word, case insensitive: grep -cwi walter index.html
  • Count up to 3 matches (and list the number of matches) using patterns from a file: grep -cm3 -f../list.txt *

And many more…

There are a lot more options to GREP and a lot more ways to combine them. And I still have not even mentioned how to use regular expressions as patterns.