LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-25-2006, 04:32 AM   #1
Ander
Member
 
Registered: Apr 2003
Location: Vancouver
Distribution: Debian
Posts: 37

Rep: Reputation: 15
A quick bash question


I'm trying to understand pipes.

Why does 'kate mytextfile' work, but 'mytextfile | kate' doesn't?
 
Old 03-25-2006, 04:39 AM   #2
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
http://sig9.com/articles/unix-pipes-intro

The reason that doesn't work is obvious but I can't think of a decent way of explaining them so here's a link, it'll do a beter job than I would
 
Old 03-25-2006, 05:29 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Ander
I'm trying to understand pipes.

Why does 'kate mytextfile' work, but 'mytextfile | kate' doesn't?
A pipe connects the output of one command to the input of the next.
"mytextfile" does not produce any output---it actually does nothing. to prove this, just type it in at a prompt.
The first example works because the kate command is set up to handle what are called "command-line arguments"

If you like to experiment, try entering:
"cat mytextfile | <somecommand>" This reads the content of mytextfile and gives it to <somecommand>

A common usage would be "cat mytextfile | grep hello"
This prints any line from the file that contains the word "hello"
 
Old 03-25-2006, 06:48 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Pipes are often used with a type of program called a filter. Grep, sed, tr & awk are common filters. They are placed after a pipe, taking the standard input from the pipe and printing the result to the standard output.

Another type of program like kate, or kghostview expect a filename argument. Some of these will accept the "-" dash character argument as a signal to take the input from standard input.

For example, try:
man -t bash | kghostview -

Then read page 5 to learn about pipes!

Or suppose that you want a text file output:
man bash | cat >bash.txt

A program such as "enscript" will be setup to write a file instead of sending the output to stdout.
With enscript and others you can use the output filename of "/dev/stdout" to generate a stdout stream.
examples:
man bash | enscript -o /dev/stdout >bash.ps
man bash | enscript -o /dev/stdout | kghostview -

A handy command to learn is "xargs". This command will take the standard output of a pipe and use them as arguments to another command.

Just minutes ago, I used pipes to take the contents of the unziped .k3b file to get the filelist of files I backed up to cdrom.
unzip name.k3b.
The resultant maindata.xml contains the filenames that were backed up. So I can delete them like this:
sed -e '/^<url>/!d'-e 's/<url>\(.*\)<\/url>/\1/' -e 's/\&/\\&/' maindata.xml | tr '\n' '\000' | xargs -0 rm -v

The sed commands extract just the filenames from the xml file and escape an "evil" character in the filenames. The tr command replaces newlines with nulls. The -0 option to xargs tells it to use nulls instead of returns. This helps with filenames that contains spaces. These filenames are used as arguments to the "rm" command and are deleted.

Last edited by jschiwal; 03-25-2006 at 06:51 AM.
 
Old 03-25-2006, 12:44 PM   #5
nopicexpert
LQ Newbie
 
Registered: Mar 2006
Posts: 1

Rep: Reputation: 0
Hi does anyone know how to merge three two photos into one by chance. If anyone can help mea dn give me suggestions i would greatyl appreciate it thanks a ton.

Thansk
 
Old 03-25-2006, 03:37 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
ImageMagick is a package of several programs. One of them will combine photos.
I think the program you want is called mogrify.
Quote:
NAME
mogridy (sic) - resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. Mogrify overwrites
the original image file, whereas, convert(1) writes to a different image file.
...
For more information about this command, point your browser to file:///usr/share/doc/packages/ImageMagick/index.html.

Run `mogrify -help' to get a summary of the mogrify command options.

Last edited by jschiwal; 03-25-2006 at 03:39 PM.
 
Old 05-05-2006, 02:13 AM   #7
Ander
Member
 
Registered: Apr 2003
Location: Vancouver
Distribution: Debian
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
cs-cam > The reason that doesn't work is obvious but I can't think of a decent way of explaining them so here's a link...
Yup, thanks... It is indeed obvious to me too, now that I've been using Linux a month longer (and having taken this long to check this thread).

That's good, though... If such originally-confusing stuff seems so simple now, it means there's hope for all of us as we confront the truly hard stuff. (Er, if anybody understands what I just said, explain it to me, okay?)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
quick bash script to convert flac to mp3 zuralin Programming 8 08-19-2011 03:50 AM
Quick bash help Aphex_Twin2 Linux - General 5 05-23-2005 10:59 AM
Quick bash question neocytrix Programming 8 07-30-2004 10:23 PM
really quick bash question fibbi Linux - Software 3 06-15-2004 10:14 PM
quick bash help! frost33 Programming 2 11-14-2002 06:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration