Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bash script that creates some file using dd. The problem is dd throws a great amount of output that is going to mess with the output of my script. Searching around I've found a solution:

dd if=boot1h of="/dev/r$temp1" >& /dev/null

Is there an alternative, or is redirecting to /dev/null the only way?

share|improve this question
3  
I'm lost on your last sentence, "the script should not request for privileges". That doesn't seem to have anything to do with redirecting to /dev/null -- you're sudoing because dd needs write access to /dev/r$temp1 (I assume). You're going to need to do that no matter how you suppress dd's output; redirecting output to /dev/null doesn't require root –  Michael Mrozek Jan 31 '11 at 17:40
    
I'm an idiot. It was giving operation not permitted because of the use of /dev/r$temp1/. I'm really sorry. I'm editing a script that's not mine and didn't noticed it. Really sorry. –  dierre Jan 31 '11 at 17:54
    
so the problem is regular user cannot write to /dev/r$temp1 or to dev/null –  jet Jan 31 '11 at 18:05
    
Don't worry about it; I edited it a bit to remove the confusing part –  Michael Mrozek Jan 31 '11 at 18:59
1  
If you're not using any of dd's advanced features, use cat, head or tail instead. –  Gilles Jan 31 '11 at 19:46

7 Answers 7

up vote 11 down vote accepted

From the dd(1) man page:

   status=noxfer
          suppress transfer statistics

thus:

dd if=boot1h of="/dev/r$temp1" status=noxfer

This still outputs the

0+1 records in
0+1 records out

garbage when dd exits, so redirecting to a data sink really is your only option.

share|improve this answer
    
that was I'm afraid of. –  dierre Jan 31 '11 at 16:59

If I understand correctly what you are trying to do, are you putting that sudo command into the script and expecting the script to prompt for your password when it runs there? In that case you are just doing things the complicated way.

A cleaner solution is to write the script in the usual way (i.e without sudo) and run it as the superuser. The reason behind this is, if the script needs superuser access, then just give it the access (why wait until a certain command?). In the script, to check if it is being run as root do something like this:

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi
share|improve this answer
    
nope. That's exactly what I don't want to do. I don't want the script to be run as root only because I don't want dd to display the output. Your check would correct if I want the script to be run as superuser. –  dierre Jan 31 '11 at 16:59
    
@dierre So why did you need sudo in the first place? Normal users should be able to redirect things to /dev/null just fine. –  phunehehe Jan 31 '11 at 17:50
    
I'm using Ubuntu and it's giving me operation not permitted...uhm... –  dierre Jan 31 '11 at 17:52
    
I'm sorry. I didn't notice I was creating something under /dev/. The script is not mine, I was editing it. I'm really sorry. –  dierre Jan 31 '11 at 17:55

you can redirect the output to an regular file i.e.:

 dd if=boot1h of="/dev/r$temp1" >& /tmp/dd.log
share|improve this answer

Something like this should also work for you with recent versions of BASH and ZSH:

dd if=/path/to/file of=/path/to/another_file bs=1M count=1 &> /dev/null

P.S. This is just an example I ran...

share|improve this answer

With any Unix application or command, you can suppress all output with

cmd >/dev/null 2>&1

The first bit redirects the standard output (unit number 1) to /dev/null. But you need the second part to ALSO redirect the error output (unit number 2) to the same place as number 1.

In UNIX, STDIN=0, STDOUT=1 and STDERR=2


share|improve this answer
    
Actually, stderr has the file descriptor 2. (I assume "STDERR=3" is just a typo.) –  n.st Jun 21 '14 at 13:29
dd if=boot1h of="/dev/r$temp1" status=none

The status= flag controls which info to suppress outputting to stderr; 'noxfer' suppresses transfer stats, 'none' suppresses all

dd (coreutils) 8.21

share|improve this answer
    
@roaima - just curious, why editing a quote from the man page ? –  don_crissti Apr 23 at 11:50
    
@don_crissti I felt the (lack of) context in extracting the quote from the man page mean that it required a slightly different form of words. As it stands in the man page it's fine; here it looked strange. –  roaima Apr 23 at 12:02
    
@roaima - all right - curiosity satisfied :) - but note that you link to the info page; the initial answer here had the exact quote from the man page. I would have copy/pasted directly from the info page if the man page is ambiguous but really, up to you... Oh, btw, thanks much for your other comment on my answer related to sorting by week day. –  don_crissti Apr 23 at 12:46
    
@don_crissti I couldn't find a man page online that had the quote; thank you for that one. (die.net has the older dd without status=) –  roaima Apr 23 at 12:50
    
Doesn't seem to be available in dd (coreutils) 8.13: dd: invalid status flag: `none' Try `dd --help' for more information. –  Per Lundberg May 23 at 18:44

For future reference:

To suppress dd output completely redirect stderr to /dev/null like so:

dd if=/dev/urandom of=sample.txt bs=5MB count=1 2> /dev/null

This works nicely if you want to, for example, time the process using the time command in bash and assign the result to a variable, without getting any of the output that dd produces.

reference: http://www.unix.com/shell-programming-and-scripting/131624-how-suppress-dd-output.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.