Skip navigation

Category Archives: Linux

Presented as an exercise in figuring out what I was trying to accomplish:

sed -e 's/\([ ]*[0-9]\{4,4\}\)\([ ]*\)\([^ ]*\)\([ ]\)\(.*\)/\1,\5,\3/g'

Anyone (other than Dave) with an idea of what I’m doing here, leave your answers in the comments.

Advertisements

Like many other private schools, there have been rumblings at my workplace about going one-to-one (that is computers to students). At the moment, those rumblings point towards a pilot program rolling out iPads in the Middle School. We have a lot of smart teachers doing interesting things with iPads in their classrooms already, and I think that an iPad program could have a number of benefits in the classroom (though it will require a thoughtful digital citizenship curriculum), but that’s not what I’m most concerned with at the moment. I’m concerned with how the kids and teachers will print to all our legacy printers from their shiny new tablets.

A week or two ago, I was asked to research a product that is supposed to simply and seamlessly solve the problem of using legacy printers for AirPrint. The idea, which is a decent idea, is that you plug this box into your network and it discovers all your printers and makes them available for AirPrint. The problem, which I’ve heard from other sysadmins at schools in the area, is that this particular product doesn’t scale well–one of the boxes costs about $100, and each of them is “good” for about 7 printers, but even then, they apparently have a tendency to lock up and require frequent hard resets. There are software solutions available, but they cost money and run on Macs, and I don’t want to make us get a dedicated Mac to act as an AirPrint server if there’s a way to get the system running for free on open-source software.

Well, it turns out there is a way. At this point, I’ve only done a small-scale proof-of-concept, but as of this writing, I have printed a document from my iPad to my office’s HP 3505n, which is certainly a step in the right direction, and in the future, I’ll probably do a scaled-up test and then hopefully put the system into full deployment. In my test case, there was no noticeable lag between my submitting the print job from my iPad and the printer firing up compared to printing directly from my computer.

To get this running, I used my Linux workstation, which is running Ubuntu 12.04.1. Ubuntu comes with Avahi pre-installed, which is great, because you need that, too. (You may remember my mentioning Avahi before.) After installing your printer or printers, you need to make sure that they’re shared, then edit your CUPS configuration and create a configuration file for Avahi. Full instructions are on gyttja’s blog, though the original article referenced seems to be down, so you’ll want to go here to get it on the wayback machine.

You’ll want to restart CUPS and Avahi after you’ve put through all the changes, otherwise, you probably won’t get any results, and some swearing might ensue. To do that, just run

sudo service cups restart
sudo service avahi-daemon restart

Look for another post on this subject here once I get a solution working on our campus.

Sorry, couldn’t help it.

More to the point, what the hell does 755 mean when referring to file permissions? You may remember, way back in my first post, that I talked about the output of ls -l showing you the permissions for files. Well, that chain of permissions, rwxrwxrwx can be represented quite simply octal notation. Why octal? Because each permission can be represented as a single bit, each group of permissions is three-bits, an octal number is three bits, and, as far as I can tell, our UNIX fore-bearers liked to make things confusing and intimidating for non-users. So there.

Therefore, if we go back to 755, we can convert each digit to binary, giving us 111 101 101 which tells us that the permissions for a file with the mode of 755 is rwx for its owner, r-x for its group, and r-x again for all others.

Shell scripting!

I’ve been coding off and on in a number of different languages (around seven by my count) since I got my first graphing calculator back in 1999, but I’d never written a shell script before today. I mean, yes, I’d copied other people’s shell scripts from the internet when it’s suited me, but I’d never written my own before.

That all changed today when I was reading an article in Scientific American on the subject of internet comment threads. In the article, the author mentions the concept of disemvoweling trolls, which struck me as a good thing to automate. Thus, my first shell script was born.

I’ll give you the code in a moment, but before I do, in debugging the program (who would have thought that you needed to debug two lines of code?), I learned something useful: when writing shell scripts, your variable names should not have trailing whitespace before the = or they will not be seen as variables (something that will take some adjusting to for me, since aesthetically I prefer to have whitespace surrounding my equals signs). Maybe I should have twigged that something was off when Vim didn’t turn my variable a different color when I initialized it, but I’m new to this, so give me a break.

And now, the script (well commented and maybe not as elegant as it could be, but, again, it’s my first time, so give me a break):

#!/bin/bash

# This script will disemvowel its input and feed it into a new file

# Create a disemvoweled version of the file being read
DSMVLD=dsmvl.$1

# Disemvowel file with sed and feed into new file
sed -e 's/[AEIOUaeiou]//g' $1 >> "$DSMVLD"

Of course, I could easily make it so this could to batch disemvowelment, butI can always do that later if I actually see the need to disemvowel more than a few files at a time.

If you want and if you can, feel free to share your first scripts, or any fun scripts you’ve written in the comments.

Don’t get me wrong, Vim is very powerful and well worth knowing if you’re going to spend any time at all on the command line, but I’m far from an expert. You could read the man page, sure, but Vim is one of those things you need to learn by using it.

Here are two resources, the first platform-independent.

The Interactive Vim tutorial

On Linux, you can also just type in vimtutor on the command line. If you get an error and you didn’t mistype the command, you’ll probably need to install a couple of packages. On Ubuntu, run the following:

sudo apt-get install vim vim-runtime

Now go learn some Vim.

:wq

A little gem I learned today when needing to get a file from a remote system that I’d sshed into onto my local machine so that I could manipulate it while offline: the scp command.

There are a lot of switches for scp, but if you’re trying to get a file to or from a remote machine, or even between two remote machines, both of which you are able to ssh into, here’s the syntax:

scp -P 22 [user@source.machine.address.if.remote:]/directory/of/file/to/copy [user@target.machine.if.remote:]/target/directory

In the above example, the -P flag is indicating the port (in this case, we’re using port 22 because we’re logging in using ssh).

If you’ve typed everything correctly, you’ll then be prompted for the password of the user at the remote machine (or the first one, if you’re moving files between two remote machines). After entering everything correctly, you should see a new line indicating the current file being transferred, the percentage transferred (this will update live), number of bytes transferred, transfer speed, and elapsed time. If you’re just copying a small text file, this will flash by faster than you can really comprehend, and you’ll be deposited back at the command prompt before you know it.

If you’ve spent any amount of time in the terminal, you’ve probably had to look up some specific function of a command–if you’re like me and basically self-taught in the ways of Linux, you’ve probably spent a lot of time with man pages. If you’re like me, you probably also never knew that there are switches, though I suppose options is the more correct term on Linux systems, to go along with man.

Running man with the -k option (or --apropos) is something I wish I’d known about long ago. man -k, which has the same function as the apropos command (which I also wish I’d known about long ago) allows you to look up a command when you know what you want to do, but don’t know the command name. Simply type man -k followed by your search term to pop up a list of commands whose names or synopses match what you’re looking for.

In the case that you know the name of a command–maybe you heard some wizards tossing it around–but don’t actually know what it does and don’t want to read through the whole man page, there’s an option for that, too. man -f or whatis will give you the synopsis of the command. Now you don’t have to wonder anymore.

Finally, if you want to read up on a command but don’t want to spend any more time in front of a computer screen, there is a way for you to print out man pages. Using the -t option in conjunction with some things I don’t fully understand yet, you can send a man page to a printer or, if you do want to read off a screen after all, you can make a PDF version of a man page. The commands for this are man -t grep | lpr -P [some printer set up on your system] to print and man -t nslookup > nslookup.ps && ps2pdf nslookup.ps && rm nslookup.ps to create a PDF.

Soon, I will have a better understanding of what those last things I just typed mean, but I thought they were too interesting to pass up right now.

I’ve been using Linux casually for a number of years now running several Ubuntu variants, and I would put myself firmly in the category of self-taught user.  I’m comfortable using the command line, and there are a number of tasks that I would rather use it for than go through a GUI.  There are also some tasks I perform regularly at work that require me to use the command line, and I rarely flinch at diving into commands if that’s what I need to do to get a job done, but some of the more advanced things I’m doing I’ve learned by rote memorization.

I read the man pages a lot when I want to figure out how to do something, but those don’t always have examples, and sometimes there are things that a command can do that I didn’t know about.

Wildcards

I know about wildcards insofar as I know they exist, and most of the time I can remember that * means any character (including no characters at all), but ask me what ? means or how to use square brackets, and I may just plead the fifth.

So.  The ? wildcard.  It stands in for any one character.  Given a list of files:

butts.txt
butts1.rtf
butts2.rtf
butts3.rtf
walrus.jpg
narwhal.png

The code rm butts?.rtf would match butts1.rtf through butts3.rtf but wouldn’t touch butts.txt, walrus.jpg, or narwhal.png.

The [] wildcard matches a set of single characters (for example [23]) or a range of characters separated by a hyphen (such as [5-9]).

If I wanted to get at those other files without touching the rich-text files, I could use rm [a-z].* where the [a-z] will get any lowercase words not separated by capital letters, underscores, hyphens, etc. The . is just that–there’s nothing special, it just means that after that sequence of lower-case characters, the next expected thing is a dot. After that, there’s the * wildcard, which means anything. In the above example, then, butts1.rtf et al won’t be touched because after the string of letters, the next thing expected is a dot, not a number.

It’s worth noting that in the above example, if there had been a file named tricky.example_7.txt in the same directory, rm would have gotten that one, too, because of that * wildcard, which says “anything is game.”

Some Switches for ls

Recursion. If you’ve ever take a programming class, you’ve probably encountered the term before. For the ls command, recursion, signified by the -R switch, not only lists all files in a given directory, but also lists all files in every subdirectory and so on down the line. Turtles all the way down!

The other useful switch I want to talk about, because it’s one that I just recently came to fully understand, is the -l switch (that’s a lowercase L, not a one–that does something different), which lists, among other things, file permissions and ownership. Let’s look at a couple of examples from life, shall we? Below are a couple of examples of the output from running ls -l on a directory on my closest Linux box.

drwxrwxr-x 4 hilary hilary 4096 Sep 12 15:29 A+
-rw-rw-r-- 1 hilary hilary 1580414 Mar 8 2012 LFS-BOOK-7.1.pdf

There’s a lot of stuff going on there, right? Let’s start from the right and move left, just to be contrary. On the far right, we have the name of the file (on Linux systems, everything is a file, even directories). Next, we get the time and date that it was last modified. Moving left again, we see the size of the file in bytes. Since the top entry is a directory, we see how much space is taken up by the list of files the directory points to, in the bottom entry, we see that it’s a large PDF document. Left of that we have (moving from right to left still) the file’s group and its owner. Following that, we have a number. In the case of directories, this number tells you how many items the directory contains, and in the case of files, the number of hard links that exist for that file (I’ll cover hard links at a later time when I have a better understanding of them myself). Finally, we have a possibly confusing string of letters that tell you about the file’s type and permissions. Starting from the left of that string, the first character tells us what the file is. Regular files and executables are both indicated by -, a d tells you that it’s a directory, l is for link, s for socket, b for block device, c for character device, and p for named pipe.

After that information, you get the permissions for, from left to right, the file’s owner, the file’s group, and everyone else on the system. In the case of A+ we see that it’s a directory, and I, the owner, have r read, w write, and x execute permissions. The file’s group has the same permissions, and everyone else in the world of this system has read and execute, but not write. Given the above information, you should be able to suss out that the bottom file is a regular file to which the owner and group have read and write permissions, and everyone else has read only.

So what does it mean to have execute permissions on a directory? For directories, the execute permission means that a given user has the ability to access files within that directory for the purpose of running commands that access or otherwise use them.

That’s it for this time. Check back in next time for who knows what.