Tuesday, January 20, 2009

More Solaris tips

  • Create a directory and move into it at the same time

Question: How often do you create a new directory and then move into that directory in your next command? Answer: Almost always.

I realized this trend in my own work habits, so I created a simple shell function to do the hard work for me.

md () {
mkdir -p $1 && cd $1
}

This is a Bourne shell function named "md" that works for Bourne and Korn shell users. It can be easily adapted for C shell users.

Taking advantage of the -p option of the mkdir command, the function easily creates multi-level subdirectories, and moves you into the lowest level of the directory structure. You can use the command to create one subdirectory like this:

/home/al> md docs
/home/al/docs> _

or you can create an entire directory tree and move right into the new directory like this:

/home/al> md docs/memos/internal/solaris8
/home/al/docs/memos/internal/solaris8>

  • Easily convert man pages to text documents

Have you ever wanted to convert a man page into a plain text document?
I do this occasionally when I want to share information via an email or other document format.

I used to think this was difficult, but then I discovered a simple way to do it. Here's the wrong way to write the man page for the ls command into a text file named ls.bad:

man ls > ls.bad

This keeps all of the formatting characters in your document, which is generally not what you want. Here's a better way that eliminates those formatting characters:

man ls | col -b > man.txt

The col command with the -b option removes the undesirable backspace characters from the text stream, so the only thing left in your document is the text you want, in the format you want.


  • How to page more than one command at a time

Have you ever wanted to group a bunch of commands into a paging program like "page" or "more", but didn't know how?

As a system administrator, I always worry about certain things, like who's doing what, what processes are running, what the network traffic looks like, etc. One day I decided to create a simple alias that would combine all the commands I wanted into one big chunk of information. Then I realized that it wouldn't all fit into one screen.

Fortunately I knew how to group all of the commands together, so the "more" command could handle them as one set of input.

First, here's the wrong way to try to page a sequence of four commands:

date; netstat -i; whodo; ps -ef | more

The only command that gets paged properly here is the "ps -ef" command
--the rest of them scroll off the screen before you can read them.

Here's the correct way to page four commands so they're all controlled
by "more":

(date; netstat -i; whodo; ps -ef) | more

Once you find the commands you want to group together, you can combine them into an alias or shell program. I recommend a shell program for this, because a few "echo" statements sure make it easier to see where one command ends and the next command begins!


  • Changing a file's owner and group at the same time

Did you know that in Solaris 2.x you can change both the user and group ownership on a file at the same time by running /usr/ucb/chown?

For example, suppose you have a file called /export/home/grahamc/myfile that is owned by user 'grahamc' and group 'users'. To change the ownerships, you could type the following as root:

/usr/ucb/chown grahamc:users /export/home/grahamc/myfile

rather than typing:

/usr/bin/chown grahamc /export/home/grahamc/myfile
/usr/bin/chgrp users /export/home/grahamc/myfile

You can also recursively change ownerships with the -R option.

For more information, try: man -s 1b chown

Leia Mais…

Sunday, January 18, 2009

Solaris Tips

As I start working again my OpenSolaris, I'm posting few tips i learned recently

  • Finding text strings in binary files

Ever wondered what's inside some of those binary files on your system (binary executables or binary data)? Several times I've gotten error messages from some command in the Solaris system, but I couldn't tell where the error was coming from because it was buried in some binary executable file.

The Solaris "strings" command lets you look at the ASCII text buried inside of executable files, and can often help you troubleshoot problems. For instance, one time I was seeing error messages like this when a user was trying to log in:

Could not set ULIMIT

I finally traced the problem down to the /bin/login command by running the "strings" command like this:

root> strings /bin/login | more

The strings command lists ASCII character sequences in binary files, and help me determine that the "Could not set ULIMIT" error was coming from this file. Once I determined that the error message I was seeing was coming from this file, solving the problem became a simple matter.

  • A vi macro to display line numbers

While the vi editor has been known to be a little rough around the edges, it still has some pretty nice features. One of those features is the ability to define cool macros. Here we'll show you how to create two macros--one to display line numbers of the file you're editing, and one to hide the line numbers.

First, create a file named .exrc in your home directory (or edit the current file if it already exists). This is the configuration file that vi reads when it is started. Put the following two lines into this file:

:map #1 :set number^M
:map #2 :set nonumber^M

(A very important note: create the ^M characters in this file by typing the key sequence [CTRL-V][CTRL-M]. This key sequence embeds an actual ^M character (the carriage return) into the file.)

Now, save this file and re-start vi. From vi's command-mode, you'll now be able to display line numbers beside your file contents by hitting the [F1] function key, and clear line numbers by hitting the [F2] key. If you like these macros, create your own powerful macros by following this same technique!

  • Use traceroute to find network problems

With the Internet and TCP/IP networks all around us, it's important to know a little bit about TCP/IP network troubleshooting. The Solaris "traceroute" command lets you trace the route your packets are taking to get from your current workstation to a remote workstation you're trying to reach.

For instance, suppose you're on the Internet and you're not getting an HTTP response from a remote server named www.zdtips.com. You try to "ping" the remote site like this:

ping www.zdtips.com

but get no answer. Is the remote server down, or is there a broken link between you and the remote site? Issue the following command to see where the problem lies:

traceroute www.zdtips.com

The traceroute command works its way through the network, and tells you the path it's taking to get to the destination site as it goes along. Watch for the point traceroute fails to learn more about the network segment that has failed. Of course you can also try this on working connections to learn more about how your Internet packets get from one site to another.

  • Using telnet's command mode

When you use the Solaris telnet command to log in from one site to another, don't forget that you can enter telnet's command mode at any time during
your session, usually by entering the following key sequence:

[CTRL] ] (the control key and right-bracket key at the same time)

When this is successful, you'll see the following prompt:

telnet>

Then, from the "telnet>" prompt, enter "?" to learn more about the available telnet commands. A few commands that will give you more help at the prompt are shown below:

display displays many current Telnet settings
send ? displays commands you can "send" to the remote site
set ? displays variables that can be set

One of my favorites (mostly when fooling around) is:

send ayt

which means "send this message: are you there?".

  • Keep commands running after you leave with nohup

It's 3 p.m., and you want to start a long job running. Unfortunately, you can't be sure that the job will finish by 5 p.m. when you need to leave, and the company is very strict about making sure you log off when you leave. However, if you log off the system, the job will be stopped. What can you do?

On Solaris systems you can use the "nohup" (no hang-up) command to keep jobs running long after you log off the system. Using nohup tells the system not to "hang-up" on your job after you've logged off the system.

Here's how to run the job, and keep it running after you log off:

root> nohup my-long-job &

This creates a file named "nohup.out" in the current directory that contains the standard output of the command ("my-long-job") you're running. Everyone is happy because the job keeps running, you get to leave at 5 p.m., and you're properly logged off the system.

  • Use rsh to run commands on somebody else's computer

Here's a hypothetical situation for you: On the "Sesame Street Network", you're logged into a Solaris workstation named "elmo", and you need to look at the man pages for the tar command. Unfortunately the man pages aren't installed on this workstation.

If you're on a network of Solaris computers like this, and you know that the man pages are installed on another workstation named "bigbird", the solution is simple. Simply run the man command on bigbird, like this:

elmo> rsh bigbird "man ksh" | more

This runs the "man ksh" command on bigbird, and pipes the output of the command into the "more" command on elmo.

Here's a few other "rsh" commands you can run on remote workstations:

rsh bigbird "who"
rsh ernie "ps -ef"
rsh grover "ls -al /home"

  • Use CDPATH to traverse file systems faster

If you're like many Solaris users and administrators, you spend a lot of time moving back and forth between directories in similar locations. For instance, you might often work in your home directory (such as "/home/al"), the /usr/local directories, web page directories, or other user's home directories in /home.

If you're often moving back-and-forth between the same directories, and you use the Bourne shell (sh) or Korn shell (ksh) as your login shell, you can use the CDPATH shell variable to save yourself a lot of typing, and quickly move between directories.

Here's a quick demo. First move to the root directory:

cd /

Next, if it's not set already, set your CDPATH shell variable as follows:

CDPATH=/usr/spool

Then, type this cd command:

cd cron

What happens? Type this and see what happened:

pwd

The result should be "/usr/spool/cron".

When you typed "cd cron", the shell looked in your local directory for a sub-directory named "cron". When it didn't find one, it searched the CDPATH variable, and looked for a "cron" sub-directory. When it found a sub-directory named cron in the /usr/spool directory, it moved you there.

You can set your CDPATH variable just like your normal PATH variable:

CDPATH=/home/al:/usr/local:/usr/spool:/home

  • Rearranging columns with awk

Have you ever had a column-oriented text file, similar to a spreadsheet, but the columns weren't in the order you wanted? For instance, suppose you had the following information in a file named "checkbook.orig":

COST DATE BALANCE
10.00 040198 1000.00
20.00 040298 980.00
30.00 040298 950.00

The information is good, but you'd prefer to have the DATE column first, followed by the COST information in the second column, and the BALANCE column third.

Using awk, you can easily rearrange the columns. The following command reads the data from the file named "checkbook.orig", and writes the data to a file named "checkbook.new":

awk '{print $2, $1, $3}' checkbook.orig > checkbook.new

This brief awk command reads each line of the original file, and for each line it reads, it writes an output line to the "new" file. As it writes each record to the new file, it rearranges the order of the columns, so that the columns now appear in the desired order!

If you prefer a little more control of the printed output, awk also has a "printf" function that's very similar to printf in the "C" programming language. Here's the same example, with a tab character in-between each column of the output:

awk '{printf ("%s\t%s\t%s\n", $2, $1, $3) }' checkbook.orig > checkbook.new

The awk command is a powerful programming utility that takes care of things like opening files and reading each line automatically, so all you have to do is tell awk how to process each line as it goes by.

Leia Mais…

Saturday, January 17, 2009

Amazing audio applications for Linux

Ardour:

Ardour is a digital audio workstation. You can use it to record, edit and mix multi-track audio. You can produce your own CDs, mix video soundtracks, or just experiment with new ideas about music and sound.

Ardour capabilities include: multichannel recording, non-destructive editing with unlimited undo/redo, full automation support, a powerful mixer, unlimited tracks/busses/plugins, timecode synchronization, and hardware control from surfaces like the Mackie Control Universal. If you’ve been looking for a tool similar to ProTools, Nuendo, Pyramix, or Sequoia, you might have found it.


Audacity:

Audacity is a free, easy-to-use audio editor and recorder for Windows, Mac OS X, GNU/Linux and other operating systems. You can use Audacity to:

Record live audio.
Convert tapes and records into digital recordings or CDs.
Edit Ogg Vorbis, MP3, WAV or AIFF sound files.
Cut, copy, splice or mix sounds together.
Change the speed or pitch of a recording.
And more!


Grip:

Grip is a cd-player and cd-ripper for the Gnome desktop. It has the ripping capabilities of cdparanoia builtin, but can also use external rippers (such as cdda2wav). It also provides an automated frontend for MP3 (and other audio format) encoders, letting you take a disc and transform it easily straight into MP3s. Internet disc lookups are supported for retrieving track information from disc database servers.Grip works with DigitalDJ to provide a unified “computerized” version of your music collection.


Hydrogen:

Hydrogen is an advanced drum machine for GNU/Linux. It’s main goal is to bring professional yet simple and intuitive pattern-based drum programming which is able to be used either by itself, emulating a drum machine based on patterns, or via an external MIDI keyboard/sequencer software. Hydrogen compiles on Linux/x86 and MAC OSX, although the latter is still experimental.


Jokosher:

Jokosher is a simple and powerful multi-track studio. Jokosher provides a complete application for recording, editing, mixing and exporting audio, and has been specifically designed with usability in mind.

The developers behind Jokosher have re-thought audio production at every level, and created something devilishly simple to use.


LMMS:

LMMS aims to be a free alternative to popular (but commercial and closed- source) programs like FruityLoops/FL Studio, Cubase and Logic allowing you to produce music with your computer. This includes creation of loops, synthesizing and mixing sounds, arranging samples, having fun with your MIDI-keyboard and much more.


MusE:

MusE is a MIDI/Audio sequencer with recording and editing capabilities written by Werner Schweer. MusE aims to be a complete multitrack virtual studio for Linux.

Midi sequencing
Record/Playback/Import
Input filter
Audio sequencing
Record/Playback several mono/stereo inputs/outputs.
AudioGroups


soundKonverter:

soundKonverter is a frontend to various audio converters.

The key features are:
Audio conversion
Replay Gain calculation
CD ripping


Streamripper:

With the emergence of file sharing protocols such as Napster, Gnutella, and now Mojonation and Freenet, the average Internet user can download nearly any mp3 he wants in a matter of no time, but many times people don’t know what they want. Streamripper allows you to download an entire station of music. Many of these mp3 radio stations only play certain genres, so you can now download an entire collection of goa/trance music, an entire collection of jazz, punk rock, whatever you want.

Leia Mais…

Frozen-Bubble: Addictive Game for Linux


Frozen-Bubble has blissfully stolen hours and hours of my life with its addictive gameplay and flippin' awesome soundtrack. It's an easy game with a simple premise: shoot colors bubbles onto the game board in an attempt to match up three or more similarly colored bubbles. Doing so will cause them to fall from the board, taking connected bubbles with them. If you clear all of the bubbles, you move on to the next level. If the bubbles pile up and cover the entire screen, you lose the game and restart the entire level.

Because my description is probably dense and hard to grasp, here's a video. By the way, no one gets to make fun of my mad Frozen-Bubble skillz.



The developer website lists the features well:

Colorful 3D rendered penguin animations, 100 levels of 1p game, hours and hours of 2p game, nights and nights of 2p/3p/4p/5p game over LAN or Internet, a level-editor, 3 professional quality digital soundtracks, 15 stereo sound effects, 8 unique graphical transition effects, 8 unique logo eye-candies.

Let me emphasize again: the soundtrack is amazing. It sounds really, really good. And the multiplayer is a lot of fun, especially over the LAN.

Install frozen-bubble using Synaptic, the Add/Remove... dialog, or by typing the following into the Terminal:

sudo apt-get install frozen-bubble



Leia Mais…

Linux Shell Editing Shortcuts


One of the secrets power shell users know is that they don't really have to be great typists. They take advantage of the typing shortcuts the shell offers.

Fixing Mistakes

You probably know you can erase the last character by hitting the backspace key. But you can correct mistakes in a lot more ways than that.

First trick: Ctrl-U deletes everything back to the beginning of the line. No need to hold the backspace key down and wait while it repeats; a single Ctrl-U is much faster. It's great for when you change your mind about a command or want to clear the line and think about what to do next. Think of U as Undoing the line you were typing.

Next -- and my personal favorite -- is Ctrl-W to erase the last word. Like Ctrl-U, it's a lot faster than holding the backspace key down and waiting. But unlike Ctrl-U, it lets you keep some of what you already typed. Try it -- you'll be amazed how handy it is.

You may have noticed that you can use the left and right arrow keys while you're typing a line in the shell. But Home and End don't work to go to the beginning and end of a line -- how do you do that? The secret: type Ctrl-A to go to the beginning of the line, Ctrl-E to go to the end. To remember those two, think of A as being at the beginning of the alphabet while E just stands for "End".

If you move the cursor back while you're editing a line, with the left arrow or Ctrl-A, you might also be interested in Ctrl-K. It deletes from the cursor position to the end of the line.


Enabling editing shortcuts in other apps

Most of these editing shortcuts will work in lots of other Linux apps besides the shell, though not every program supports all of them. Try them in editors, mail programs, browsers.

In some programs and on some Linux distros, they aren't enabled by default. If you find them useful, you can turn them on for Firefox and any GTK-based applications by setting your Gnome key theme to "Emacs". To do that, either add the line

gtk-key-theme-name = "Emacs"

to a file named .gtkrc-2.0 in your home directory, or run this command:

gconftool-2 --set /desktop/gnome/interface/gtk_key_theme Emacs --type string

Leia Mais…

Saturday, January 3, 2009

Test C++, Perl, PHP, Ruby or Python Code Online

Just go to codepad.org and enter your code in any of the following languages: C, C++, D, Haskell, OCaml, Perl, Plain Text, Python, Ruby, Scheme, and Tcl. Once you’ve entered your code you then have the option of either pasting and running your code to test it or just pasting it. If any errors are found in the code that you have written, Codepad will point out the error and give you an explanation of what you have done wrong along with the line in which that error occurs. Also, after entering your code, Codepad will create a page where you and others clan go to test and collaborate.


This is a simple free tool that allows both advanced and beginner-level programmers to test their code. Most appealing is the potential it offers for group collaboration.

It's usage looks like that:

C++:

int main(){
char hostname[] = "blogspot.com/";
printf("http://mubarakworld.%s", hostname);
return EXIT_SUCCESS;
}

Output:
1 
http://mubarakworld.blogspot.com/ 

http://codepad.org/

Leia Mais…

Wednesday, December 31, 2008

Perl Myths


Perl Myths








Perl programming has it's share of myths. This presentation debunks a few popular ones with hard facts. Surprise yourself with the realities.




Leia Mais…