LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2011, 06:07 AM   #1
hanansela
LQ Newbie
 
Registered: Oct 2011
Distribution: UBUNTU 11.04
Posts: 2

Rep: Reputation: Disabled
how to direct stdout into a file


Hello
I have gust started to play with Linux scripts. i know that for simple commands i can add ">outfile" or ">>outfile" to direct stdout into outfile. However, i could not figure how to do that with a bit more complex line like this:
Code:
for k in $(ls *.jpg); do b=`basename ${k%.jpg}`; convert $b.jpg -colors 265 -format "%c" histogram:info: ; done
This gives an output of many lines of data describing the histogram of colors of a list of jpg files

Any help would be appreciated. Please be gentle and either give examples or detailed explanation. I am a newbie.
Thank you
 
Old 10-03-2011, 06:39 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Exactly the same: you can add >> outfile after the convert command or even after the entire for loop:
Code:
for k in $(ls *.jpg)
do
  b=`basename ${k%.jpg}`
  convert $b.jpg -colors 265 -format "%c" histogram:info:
done >> outfile
If you want a different output for each processed file, use the loop variable or the $b variable:
Code:
for k in $(ls *.jpg)
do
  b=`basename ${k%.jpg}`
  convert $b.jpg -colors 265 -format "%c" histogram:info: > $b.out
done
 
Old 10-03-2011, 07:13 AM   #3
hanansela
LQ Newbie
 
Registered: Oct 2011
Distribution: UBUNTU 11.04
Posts: 2

Original Poster
Rep: Reputation: Disabled
Solved

Thank you colucix for the replay
I did what you suggested and it works fine. The >> appending signal is what i needed since it collects all the results together.
 
Old 10-04-2011, 12:56 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Commands can also be grouped together with "{..}".

Code:
{ commandA && commandB || loop C ; whatever ;} > file
Notice that when everything is on a single line the final bracket needs to be separated with a semicolon.

In addition, you can also use "(..)" to run everything in a subshell, but you should really only use that when you actually need a subshell.


PS: $(..) is highly recommended over `..`

Edit: Actually, instead of basename, why not simply use another parameter substitution? Doing such things in-shell is nearly always more efficient than calling an external process.
Code:
b=`basename ${k%.jpg}`  #b=$(basename "${k%.jpg}") would be better

#change to:

b=${k%.jpg}
b=${b##*/}

Last edited by David the H.; 10-04-2011 at 01:05 AM. Reason: addendum
 
  


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
redirect stdout to file produces blank file kissaki Linux - Software 5 01-06-2009 03:28 AM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
stdout to file Machiaveli Programming 8 09-04-2005 08:53 AM
how to output stdout to a file SciYro Linux - Newbie 4 02-25-2004 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 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