Command Line 1: Commands and arguments
(Posted: April 26th, 2010 | Author: Adam Morris | Filed under: | No Comments »)We are going to write a program, er … actually a “script” that will actually do something pretty useful for you. To get there, however, we’re going to take a detour. Actually, it’s a major detour. It’s like trying to go to California from the east coast by taking the road through Mexico. I mean, who wants to go through Mexico first? But. It’s worth it. Believe me, I’ve taken the same road with teenagers and not only is it scenic but you have a deeper appreciation of just how cool CA is. (So I hear.)
The detour we’re taking is through something called the command line. Yikes. I know. You’ve spent all this time working with computers and they’ve finally gotten to the point where we Get Things Done With Pictures. And now we’re going to strip that completely aside and use the Scary Command Line. The thing is, once you get the hang of it, it’s pretty addictive.
Or you’re thinking, “NOW, I’m scared.” Oh. So you’ve never heard of the command line.
If you’re going to summarize all of Computer Science history, you would start with the adage that, people used to use paper with holes in them to make the computer do anything. (At the time, mostly just plain old math.) Then there was a revolution and things like the keyboard were invented and people could type things into them. It’s at that level of things that the command line was born. Instead of clicking on a file you wanted, you had to type the file name.
open filename
That particular command there isn’t all that historically accurate. But the idea is that typing “open” followed by one (or more) spaces … after that has to be the name of the file. And it will do what it says it will do. The “open” is a “command” and the everything else after it is an “argument.” But you know, historically, that line actually looked closer to this:
$ blehck filename
And it was a terrible vomit green color as well on a black background. But you get the idea. Here, blehck would be the command, and filename is an argument to blehck, so it knows which file you want to blehck. If the command line is like a TV remote, (the computer being a TV) then you could imagine sending the following command which would change the channel:
$ channel MSNBC
But what about that dollar sign then? That, as long with a lot of zany symbols, has particular meaning as well, which will be discussed and explained later. First, we’re going to learn what that means by running our first command from the Scary Command Line.
Open up Terminal. It’s in the Utilities folder of your Applications Folder. When you launch it once, you might want to right-click on the Terminal icon on the dock, choose “Options” and then “Keep in dock.” That way you don’t have to go hunting for it every single time. It’s not the nicest of icons or anything, but it’s a very servicable app that you’ll miss once you get the hang of it.
So, we got the whole open filename thing going for us. You can actually try that yourself. Go ahead, look at the Terminal window that comes up. There’s that dollar sign! Also, your name will be there too. Mine looks like this:
[adam-morriss]$
So what does that dollar sign mean? It means, “Go on.” Well not really. But that’s what you should be thinking. It’s waiting for you to type in a command. And we’re going to try it too. Let’s type in our favorite command line of the day:
$ open filename
WHAT HAPPENED?
You know, it’s funny. Nearly every computer tutorial known to man always asks the user to try something out on the computer that works 100% of the time every time. I’ve done the exact opposite here. This one WILL (almost) certainly fail with an error. But that’s not a problem. It just gives you an error. And, like with most errors, it’s a useful one that tells you a lot of information.
Let’s look at the error it gives. I should tell you that my “user name” is inspired by little blue smurflings, and so obviously you’re allowed to snicker to yourself but keep in mind that your user name will be different:
The file /Users/brainysmurf/filename does not exist.
Make yourself like a detective and be in awe at just how much information is contained there. First, we see that the computer is expecting the file “filename” to be living in a place called “/Users/brainysmurf/” This is easy enough to unpack what that means. That “/” indicates a folder … right … because those are actually folders on my hard drive. So I know how to get at folders now. Is it as simple as just typing the name of the folder and then a slash?
Yes it is. Let’s try this out for real now.
$ open /Users/brainysmurf/filename
It still doesn’t work! Oh no! And it’s the exact same error as before! Oh no! Think for a moment. Think! Have you actually got a file called “filename” living there? No? So no kidding it got you an error.
Let’s do this for real now. Open up TextEdit and type up something real quick. Type up “Hello World” just for laughs. Then save it as “filename.txt” in your documents folder and then close it. Now type:
$ open /Users/brainysmurf/Documents/filename.txt
And guess what, it opens. Before moving on, there’s another vital piece of information that the error code gave us that we shouldn’t overlook. The important piece of information is that our original command didn’t fail with this error:
$ open filename
Did NOT fail with this error:
-bash: open: command not found
You will get this error if for example you misspelled “open” (there’s no spell check or wiggly lines in the Terminal). That other stuff? Ignore for now. That you did NOT get a “command not found” error means that “open” truly and legitimately is a command. Which means you can find out more information about how to use that command, just by typing:
$ man open
Man? That’s short for “manual.” It’s a help command! This time, open is an argument for the command “man,” which is saying, show me the manual information for the command “open.” Taking a gander at the information, you’ll see it’s chock full of indecipherable gibberish. Never mind that stuff. But do get used to it because that’s just the way things roll with sciences in general. You’ll see a lot of crap that will make no sense, but the secret is to make lemonade when given indecipherable gibberish. I think that’s how the idiom goes, doesn’t it? Anyway, whenever you’re doing anything that requires rigor, you end up looking at stuff that looks like the following, which is what we’re going to focus on:
-a Opens with the specified application. -b Opens with the specified application bundle identifier. -e Opens with TextEdit. -t Opens with default text editor. -f Reads input from standard input and opens with TextEdit. -R, --reveal Selects in the Finder instead of opening. -W, --wait-apps Blocks until the used applications are closed (even if they were ... --args All remaining arguments are passed in argv to the application's ... -n, --new Open a new instance of the application even if one is already ... -g, --background Does not bring the application to the foreground. -h, --header Searches header file locations for headers matching the given ...
And when you’re done admiring, type ‘q’ for quit. That gets you out of the “man page” as they are called. You could also close the window, and that wouldn’t hurt anything, but just typing ‘q’ means you don’t have to re-open another Terminal window.
So what all these hyphens for? Well, they’re options. That’s right command option. You type in those doohickeys and you can change (slightly) the way that the open command operates. Imagine you have a remote control in front of you and you want to tell the TV that when watch Fox to turn the volume up higher than you would when you watch CSPAN. There’s no button for that you say? Well, why not? Fortunately, with computers we can make it do anything we want. In command-line-nese, if we wanted to tell the TV to do that we would have to type the commands like this:
$ channel --volume 25 Fox
$ channel --volume 32 MSNBC
So do we get the idea of commands and arguments now. Because it looks a lot fancier but the idea here is pretty simple. Those double hyphens are ways of telling the TV command to do something slightly different. In both cases, it turns the channel to the provided channel, but because I said –volume it does something else as well. That’s the idea of options.
The open command has several options, most of which we probably don’t give a hoot about, but one of them, the -a “open with specified application” one, we do. Oh yes. Because what if you want to open that text file you created with Microsoft Word? That’s easy in the command line. All we need to do is find the name of the application it gives itself in the Finder, and type that in after the -a option (one hyphen) like we imagined with the –volume option (two hyphens).
$ open -a Microsoft Word /Users/brainysmurf/filename.txt
Right? Oh no. I did it to you again. This fails on purpose. It complains that it can’t find the file Word, right? What happens if I just type in word without the Microsoft bit:
$ open -a Word /Users/brainysmurf/filename.txt
Huh. Same error. If you have Pages, you can try that:
$ open -a Pages /Users/brainysmurf/filename.txt
That worked just fine! What’s going on here?
Silly you, you confused the computer, that’s what.
Remember how I described how the computer knows the difference between a command and an argument? I said that the command goes first, then the argument split by a one (or more) spaces. You also recall that I said that when you’re being rigorous that you have to put up with a lot of crapoola? That applies here too, because that space actually means something. So when you typed in:
$ open -a Microsoft Word /Users/brainysmurf/filename.txt
There’s a space between Microsoft and Word. Heh. Isn’t that a wonderful thing? That’s how the computer got itself confused. Is the program name “Microsoft” or “Word.” It just has no way of telling. So it guessed and chose “Word.” But what we have to say is “Microsoft Word.” Notice all those quote characters I’m using? Because you have to use them too to get this right. Here’s the answer:
$ open -a "Microsoft Word" /Users/brainysmurf/filename.txt
Now, after it takes about five minutes to load, and after you close the thing that says it has to update some critical piece of software (MS products are always updating itself, apparently), note that if you were really clever you could probably figure out that, if you changed the name of the Microsoft Word file in the Finder, and typed in “Word” for the -a option, that it would work, right? Well, it doesn’t, so don’t bother trying. The reason it doesn’t work that way is because I glossed over the idea that “all you have to do is find the name of the application in the Finder.” Doesn’t quite work that way. But how it actually works is not the issue, just rest assured that the name in the Finder is the same name used in the super secret sauce software that actually finds applications and launches them, so you don’t gain anything by changing your applications’ names.
Which leads us to another point about commands and arguments. There are conventions. You’ve seen two so far. One is, don’t change the names of files to get commands to use them (use quotes instead), and the other you may have missed. Commands take on options, and if the option is a single letter, like with the -a then you use a single hyphen. If the command is an actual word, like our imaginary –volume command, then it needs two hyphens. Why. This is where you meet the term “historical reasons.” At first, all the options were single letters with a single hyphen, but then people realized that we should be able to use full words too. So anyway, that’s that.
Now, you’ve learned some of the basics of the command line, but you still don’t have anything too useful. That’s reserved for the next lesson.