Moving data at the command line.

Ryan Hennig rhennig at cs.washington.edu
Mon Aug 20 02:29:41 PDT 2001


I've added a few simple examples to help you out.  

Why am I writing an email about shell commands at 2am on a Sunday night, you ask?  Well, there's work to be done, and I'm avoiding it!

Ryan

On Sun, 19 Aug 2001, Russell Power wrote:

first of all:
stdin = standard input: where programs look for input once they've started
stdout= standard output: where programs print stuff, usually the screen.
stderr= standard error: where programs spew error messages, also usu. the screen.
 
> command1 | command2     -    pipes the result of the first command to the
> second

zsh$ grep "foo" file | sort
finds all lines containing foo in the file, then sorts them before printing to screen. Basically, command1's stdout becomes command2's stdin.
Get to know the pipe well, its your friend.

> command1 || command2     - runs command2 iff command1 returns a non-zero
> result

zsh$ mount /mnt/cdrom || echo "Error: couldn't mount cdrom"
You'll see this type of thing in shell scripts all the time.  For those of you who haven't dabbled in the black arts of programming, the || means "or" in many languages, so you can think of this line saying "mount the device, or print an error" (programs return 0 on success, nonzero on failure)

> command1 && command2 - runs command2 iff command1 returns zero

zsh$ mount /mnt/cdrom && echo "Mounted cdrom"
Again, this is something used in shell scripts, but prob. not in an interactive shell.  This would only print the message if the cdrom mounted successfully.

> command1&    -    execute the command asynchrously (in the background)
zsh$ xterm &
This is useful when starting a GUI program, so that the shell doesn't wait for the program to finish before displaying another prompt.

> command1 > file    -    redirect the output of the command into file
zsh$ ls > file_list
save the directory listing into a file.  > redirects stdout to a file.
to redirect stderr, use "2>" instead.  ">" is the same as "1>".

> command1 < file    -    read the file as the commands input
zsh$ cat <foo.txt | grep "bar"
cat gets the lines from foo.txt on its stdin, and copies them to its stdout, which is piped to the grep command.  Grep then gets the lines on its input, and prints the lines which contain bar.  Of course, you could do this with:
zsh$ grep "bar" foo.txt

but where's the fun in that?

Here's another useful tidbit.  Say you're starting mozilla or some other program from the command line that spews all kinds of debugging output to the screen, and you don't want to see it.  You could use;
zsh$ mozilla 2>&1 >/dev/null &

Let's dissect that: the 2>&1 means "redirect (>) the standard error stream (2) to the same place (&) that the standard output stream (1) is going." Then you redirect stdout to /dev/null.  Which, if you're not familiar with it, is like a black hole:  what goes in, does not come out.  Note that the & to run the command in the background comes after the redirection.

Whew!  I went a little overboard there, time to get back to work.

HTH, 

Ryan

"Unix is user-friendly.  It's just picky about who its friends are." - Anon
> 
> HTH,
> 
> --russell
> 
> 
> ----- Original Message -----
> From: Nathan Flint
> To: UW Linux Group
> Sent: Sunday, August 19, 2001 8:55 PM
> Subject: Moving data at the command line.
> 
> 
> Hey,
> 
> I'm looking for information on using the command prompt thingys, > < & &&
> and |, and what they actually do.
> 
> -Nate
> 
> 



More information about the Linux mailing list