LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   dd output redirection (https://www.linuxquestions.org/questions/linux-general-1/dd-output-redirection-936484/)

super user 03-26-2012 07:49 AM

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
:banghead:
that did not work, nothing is written in the file.
any suggestions ? :scratch:

catkin 03-26-2012 08:29 AM

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

MensaWater 03-26-2012 08:36 AM

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).

super user 03-27-2012 05:20 AM

thank you very much :D

xeleema 03-31-2012 07:45 AM

Greetingz!

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


All times are GMT -5. The time now is 12:20 AM.