LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-26-2012, 07:49 AM   #1
super user
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
Smile dd output redirection


hello folks,
I am trying to write a shell script in which I use the "dd" command to perform some measurements on a hard disk , I want to redirect the output message of the "dd" command to a file as the following:
Code:
(dd if=/dev/zero of=/dev/sda bs=1M count=128) >> myFile.log

that did not work, nothing is written in the file.
any suggestions ?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-26-2012, 08:29 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The output from dd goes to stderr so change to (dd if=/dev/zero of=/dev/sda bs=1M count=128) 2>> myFile.log

The ( ... ) used that way does not achieve anything so it could be, more simply dd if=/dev/zero of=/dev/sda bs=1M count=128 2>> myFile.log
 
2 members found this post helpful.
Old 03-26-2012, 08:36 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
The messages output by dd are going to standard error (STDERR) rather than standard output (STDOUT). When you do ">" or ">>" you're redirecting STDOUT. You need to redirect STDERR instead.

Code:
dd if=/dev/zero of=/dev/sda bs=1M count=128 2>> myFile.log
What is commonly done is to redirect both STDERR and STDOUT and there is shorthand for doing that:
Code:
dd if=/dev/zero of=/dev/sda bs=1M count=128 >> myFile.log 2>&1
The 2>&1 tells it to send STDERR (file descriptor 2) to same location as STDOUT (file descriptor 1)
Also note that when doing this it is important to define STDOUT redirect BEFORE doing the 2>&1. If you did it the opposite way it would redirect STDERR to wherever STDOUT was before STDOUT was redirected (typically the display).

Last edited by MensaWater; 03-26-2012 at 08:45 AM.
 
2 members found this post helpful.
Old 03-27-2012, 05:20 AM   #4
super user
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
thank you very much
 
Old 03-31-2012, 07:45 AM   #5
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!

I'm curious about something. Why the decision to use 'dd' rather than the output of 'fdisk -l /dev/sda'?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] >>> and Output redirection mmhs Linux - Newbie 11 10-03-2010 04:43 AM
[SOLVED] Redirection output kma07 Linux - Newbie 3 05-12-2010 10:13 PM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
Output redirection??? ed_po Linux - Newbie 3 06-13-2006 12:17 PM
Output redirection legtester Linux - General 4 07-07-2003 02:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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