Scala, Java, Unix, MacOS tutorials (page 336)

MySQL database FAQ: How do I restore a MySQL backup? (Also written as, "How do I restore a mysqldump file?")

Assuming that you’ve created a MySQL backup file as shown in my “MySQL backup - How to Backup a MySQL database” tutorial, you can use the commands shown here to restore your MySQL database.

A lot of times when I'm asked to debug a Unix, Linux, or Mac OS X system, I'll hear a complaint like "The network seems slow", or just "It seems slow", followed by the usual "What is it doing?"

I actually think that last question is a wonderful one: What is this computer doing?

You can see a lot of information about Unix processes using the ps command or the top utility, but it seems like many system administrators don't know how to find networking information, at least not without a network sniffer.

Should you ever run into a situation where you want to use the Linux cut command by specifying both a field number and field delimiter, I hope this example is helpful for you.

I was just working on a problem where I wanted to list all the fonts on a Mac OS X (Unix) system, and needed to use the cut command this way. A straight listing of all the filenames in the Mac font directory gave me a long list of names like this:

If you're ever working on a Unix or Linux system, and have a list with duplicated items in it, and want a smaller list of only the unique items in the list, the sort command is your friend.

I just ran into a situation where I generated a list of fonts on a Mac OS X (Unix) system, and my list ended up with a bunch of duplicated names, like this:

NOTE: This contest has ended, but you're still welcome to join our mailing list, where you'll automatically be eligible for future prizes.

Background: Founded way back in 1998, devdaily.com has over 2,000 online tutorials, and receives millions of page views every year, and in 2011 I'm giving away more prizes than ever before.

For December, instead of giving away the usual book, I'm giving away something we can all use: A $50 Amazon.com OR Barnes & Noble gift card.

Drupal debugging FAQ: How can I debug my Drupal form and module development process?

I should know more about this over the next few weeks, but if you're developing Drupal forms and modules, I've found the PHP error_log function and the Drupal watchdog function to be very helpful. They both let you log errors 'somewhere', and the error_log output shows up in your Apache logs, and the watchdog output shows up in your Drupal reports.

Here's how you call the PHP error log function:

Once you get into serious Drupal module development, you'll realize that it would be really helpful if you could do several things during the module development process, including clearing the cache, rebuilding the menus, error logging (logging errors to a file, in particular), and so on. Fortunately, with Drupal's really large developer ecosystem, other Drupal programmers have already created some great modules to help you along in the coding process.

Here's a list of the Drupal developer modules I currently use, or have used.

I was just working on a Drupal form, and I wanted to include a list of states (the United States) in a dropdown field (an HTML select/options field). Knowing that I'll need this list of states for other Drupal applications, I decided to put the "states" code in a separate file.

To that end I put this list of states in a file named states.inc, in the same directory with the rest of my Drupal module code:

If you ever need to debug a Unix or Linux shell script, you can just add the -x option to the interpreter when you invoke it. You can add the shell's debug option on the command line like this:

sh -x misbehaving_script.sh

or at the end of the first line in your shell script (the she-bang line), like this:

#!/bin/sh -x

I was just working on a shell script related to fonts on a Mac OS X (Unix), system, and when I tried to run it normally, all it gave me was this error message:

Java String formatting FAQ: How can I format Java String output?

Java String formatting solution

One way to format Java string output is with the format method of the String class, which works like a “Java sprintf” method. Here are some examples:

Summary: Here are some notes on how to configure a Name Virtual Host (NameVirtualHost) on an Apache web server. In particular, this is from the httpd.conf configuration file that I use with MAMP on one of my Mac OS X development systems.

In short, as I’m developing two different applications, one named "cato" and another named "zenf", these are the important name-based virtual host lines from my Apache configuration file:

I just finished moving the alvinalexander.com website to a newer, faster web server, and in the process I had to install the APC Opcode Cache on the new server. After installing APC, I like to be able to test that it's working, so I wanted to be able to display the APC information web page (APC test page).

To display the APC test/information page, just:

Warning: The following Unix sed commands are very powerful, so you can modify a lot of files successfully — or really screw things up — all in one command. :)

Yesterday I ran into a situation where I had to edit over 250,000 files, and with that I also thought, “I need to remember how to use the Unix/Linux sed command.” I knew what editing commands I wanted to run — a series of simple find/replace commands — but my bigger problem was how to edit that many files in place.

A quick look at the sed man page showed that I needed to use the -i argument to edit the files in place:

Nginx FAQ: How do I gracefully restart Nginx?

This used to be harder, but these days all you have to do is run this command:

$ sudo service nginx restart

Note that you will probably want to make sure your configuration is correct with this command before running that command:

$ nginx -t

As I mentioned in my clean, minimalist Drupal themes article, I've been thinking about remodeling my website about living in Alaska (OneMansAlaska.com) to a much cleaner and lighter theme, and to that end, I've been browsing the internet, looking for clean, minimalist web design examples.

I really like some of these website design ideas, and in my copious spare time I'm trying to figure out how each one might work with the OneMansAlaska.com website.

There currently isn't much in the way of good Scala Lift Framework documentation, particularly for creating web forms, but I thought I'd share whatever good links I can find.

Currently there are only two good links about creating Lift forms, and both of those are on the official liftweb.net website:

Just a quick note here today on the inline CSS syntax, which I can never remember. To add CSS to your web pages using the inline syntax, just add a style tag to your HTML tag, including your CSS styles within the quotes of the style tag as shown here:

<div style="border-style: dotted; border-color: #ccc;">

The CSS styles you define within your quotes are defined just as you would in an external style sheet, but they are all on one line, as shown in that example.

Java String array FAQ: Can you share some Java array examples, specifically some String array examples, as well as the new for loop syntax that was introduced in Java 5, and is still used in Java 8, 11, 14, 17, etc.

Solution

Sure. In this tutorial, I’ll show how to declare, populate, and iterate through Java string arrays, including the for loop syntax that was introduced with Java 5. Because creating a String array is just like creating and using any other Java object array, these examples also work as more generic object array examples.

I’ve been working a lot on the next generation web server for the alvinalexander.com website lately, and as I’m looking at different HTTP accelerators, I keep wanting/needing to look at the headers returned by my web pages. A simple way to look at the HTTP headers from the command line is with the curl command, like this:

curl -I http://example.com/

Running this command against the main Google website, I see output like this:

CentOS Linux FAQ: What commands can I use to show what version of CentOS Linux I'm using?

There are at least two different ways to show what version of CentOS Linux you're using. First, you can use the Linux cat command on the /etc/redhat-release file. Here's the command, and sample output:

# cat /etc/redhat-release

CentOS release 5.6 (Final)

You can also use the lsb_release command with the -a option. Here's that command, and its output: