Mac OS X Command Line 101 by Richard Burton Of Pipes & More
This series is designed to help you learn more about the Mac OS X command line. If you have any questions about what you read here, check out the earlier columns, write back in the comments below, or join us in the Hardcore X! forum. In the last column, we looked at how input and output can be redirected to and from files. However, what if you want to run several commands on your data? Well, you could do something like this: That is a lot of typing; it discourages laziness and impatience. Worse, you might erase the wrong file along the way. And even worse, it is slow. Not only do you have to spend a lot of time typing and waiting, but constant I/O to a drive is generally the worst drag on speed.If you think about it, you really don't need those tempfiles. Oh, a crusty old mainframer might hit you with a slide rule if you say this out loud, but think about it: all you need is a mechanism that will take the output from command1 and make it the standard input for command2, and so on. In Unix shells, this is accomplished with a pipe, '|'. We could have performed the previous task all in one line. Recall the example from the previous column where we typed:That can now be replaced with:What this does is that, when tcsh is parsing the command line behind the scenes, it sees the '|' character and says 'What ho! A pipe. The Unix genius who wrote this wants me to take the output from the command on the left, who -Hu, and make it the input for the command on the right, tr a-z A-Z.' So it does.The beauty of this is that you can take very simple tools, string them together in whatever order you wish, and create complex tools. This gives you great power. Given the many options that Unix commands can have, and the number of Unix commands, this also gives you great flexibility. And what's more, it makes things very simple for you. Anyone who has ever written code to do this in a language like C can tell you how great the shell is at this. (Actually, once they start they'll never shut up; so don't ask, just take my word for it.) One of the more common uses of the pipe on the command line is to take a large amount of output and present it only one screen at a time using the more command (or page or less). Recall from an earlier column that we took a quick look at the /usr/bin directory where a lot of commands reside. First, using a pipe to connect commands, we can determine how many files exist in the directory: That's a lot of files, far more than is standard for a Terminal.app console. So, what if we want to see all of the files, but not have them fly by at breakneck speed? Why, pipe the output through more (or page or less).(I hit 'q' at this point to quit out of more.)The pipe is only one way to put more than one command on one command line, because there are different ways that you might want to combine them. For example, we've already seen the use of backticks, `, to expand the results of a command into another command. To show the difference between the pipe and the backtick, let's look at how we can combine the ls and wc commands. Consider my home directory: [Consult local listings for home directories in your area.] Using backticks will take the results of ls above and use them as the arguments for wc, thus:The pipe, on the other hand, takes the output from ls and makes it as the standard input for the wc command.Thus, the backticks give you the line count for each non-hidden item in your home directory, whereas the pipe builds a command that tells you how many non-hidden items there are in the home directory. If this seems slightly confusing at first, don't worry. Try using pipes and backticks on your own to see the differences; after all, doing is the best way to learn.A few other ways to combine commands exist. Generally, they are not used on the command line; they are more common in shell commands. However, they do exist, and we'll eventually want to write shell scripts anyway, so we may as well be aware of them. The first, and simplest, uses the ';' character to separate commands: command1 ; command2 The command line simply executes the commands sequentially. command1 is run, and when it's finished, command2 is run. If you enclose commands in parentheses, they form a subshell. The commands are treated as a command group:(command1 ; command2) The idea behind this is to force commands to be grouped together so they are treated as one entity. We saw this when we looked at I/O redirection: When we use a command group by itself on the command line, the shell behaves as if the parentheses weren't thereTwo related forms also allow you to string together commands in a conditional sense. (Yes, Ye Editor, this is getting close to shell scripting, but this is where it fits.) The first is known as the AND form:command1 && command2 The shell will run command1. If command1 fails, the whole command ends and command2 is not run. However, if command1 succeeds, command2 is then run. First consider an example where both succeed. tcsh finds the ls command, runs it, then runs the who -Hu command. As a counterexample, consider the following, which tries to run the non-existent command fred:tcsh cannot find the command fred, so it doesn't run the who -Hu command.The second form is known as the OR form: command1 || command2 The shell will run EITHER command1 OR command2. If command1 succeeds, the whole command ends and command2 is not run. However, if command1 fails, command2 is then run. First consider an example where command1 succeeds. tcsh sees the command line ls || who -HU and, behind the scenes, splits the command line into the first command (ls) and the second command (who -Hu). It successfully runs the first command, notes that the two are connected by an OR (||), and ends without running the second command. And if the first command were to fail?Here, tcsh could not find the command fred, which results in a failure. So it looks at the second command, who -Hu, and attempts to run it (in this case, successfully).--- [A final note for people looking ahead: the shell's definition of success and failure may not be quite the same as yours and mine might be. Consider: So we would think that is an error; after all, the file not_there is not found. So you and I might think that ls not_there && who -Hu will not run the who -Hu command. However:What happens is that ls runs, reports the relevant information about the list of files passed in the arguments, and exits successfully. So the shell says 'What ho! The first command says it ran successfully; off to the second command, then.' That may seem confusing. In fact, it is at first. You can probably get a good handle on how a command reacts from its man page or from considering how it would handle different situations. Examine the following:In this case, ls did find the file who_list but not fred, so ls did succeed in listing all of the existing files in its arguments. Clear as mud? Good.[And as always in unix, there are ways around it. For this particular quirk, using File Inquiry Operators aka File Test Operators can do the trick. These are covered later.] Again, just be aware of how things are supposed to work, and don't be afraid to try little things before building those methods into bigger things. This will teach you a lot about the command line, and indeed about anything for which you take that approach.]--- Destroyer worm mac os. So far we've seen different ways to combine several commands into one, using many of the Unix shell's multitasking tools. But what if you want to run separate commands simultaneously? Tune in next week, same geek channel, same geek time. You are encouraged to send Richard your comments, or to post them below. Most Recent Mac OS X Command Line 101 Columns Command Line History & Editing Your Commands Pico: An Easy To Use Command Line Editor Understanding The 'grep' Command In Mac OS X Command Line History & Editing Your Commands Mac OS X Command Line 101 Archives Back to The Mac Observer For More Mac News! Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor's cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1. |
Movey Pipe Mac Os Catalina
I'm looking at building a Cocoa application on the Mac with a back-end daemon process (really just a mostly-headless Cocoa app, probably), along with 0 or more 'client' applications running locally (although if possible I'd like to support remote clients as well; the remote clients would only ever be other Macs or iPhone OS devices). Get an unrestricted 30-day free trial of FreshBooks at 10% and Free Worldwide Shipping at Ridge Wallets by using offe. Aug 28, 2007 Mac Keyboard shortcuts: How write a pipe (or backslash) on MacBook. Few shortcuts for my Apple MacBook: The pipe ( ): (It's very useful for this command: cat file.log grep find-string) Shift + Alt + L. For the backslash ( ) Shift + Alt + /. And a link for other KEYBOARD SHORTCUT with description: Like this.