xuraax
Posts: 3
Joined: Sat Feb 16, 2013 7:31 am

Using the dd command in scripts

Sat Feb 16, 2013 7:50 am

I am new to linux so please be patient with me.
I need to import data to the RPI via a usb to RS485 converter

I am using a script which includes the line:
dd if=/dev/ttyUSB0 of=/tmp/output bs=1 count=9 &
this is working fine and a string of 9 bytes is being correctly received and stored into the file "output".
However when this line executes I get a screen message as follows:

9 + 0 records in
9 + 0 records out
etc.....

I would like to suppress the above messages.

I read somewhere that if I do:
dd if=/dev/ttyUSB0 of=/tmp/output bs=1 count=9 &> /dev/null
the system would suppress the messages but instead an error message results and the dd command does not work at all.

Can anyone help please?

Regards

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Using the dd command in scripts

Sun Feb 17, 2013 5:22 am

I'm terrified of dd but I need to use it for shrinking a partition. So if any one finds an idiots guide with loads of working examples, I'm very interested too !

:? :? :?

Tim

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Using the dd command in scripts

Sun Feb 17, 2013 6:26 am

xuraax wrote:I am using a script which includes the line:
dd if=/dev/ttyUSB0 of=/tmp/output bs=1 count=9 &
this is working fine and a string of 9 bytes is being correctly received and stored into the file "output".
However when this line executes I get a screen message as follows:

9 + 0 records in
9 + 0 records out
etc.....

I would like to suppress the above messages.

I read somewhere that if I do:
dd if=/dev/ttyUSB0 of=/tmp/output bs=1 count=9 &> /dev/null
the system would suppress the messages but instead an error message results and the dd command does not work at all.
The ">/dev/null" on the end means "send all normal messages (STDOUT) to null". The messages you see from dd aren't being sent as normal messages, they are being sent as error messages (STDERR - but this doesn't mean they are errors!).

STDOUT is output channel 1, the default for messages.
STDERR is output channel 2, the default for errors.

So what you need to do is redirect all STDERR output to /dev/null with "2>/dev/null".

The "&" means "run in the background while the script continues". Not quite sure what that is doing there, but it has to be on the end of the line, after any redirects.

User avatar
jojopi
Posts: 3271
Joined: Tue Oct 11, 2011 8:38 pm

Re: Using the dd command in scripts

Sun Feb 17, 2013 9:55 am

"&> /dev/null" is shorthand for "> /dev/null 2>&1" (put stdout in /dev/null, put stderr in stdout). But the shorthand is non-standard and best avoided in scripts. It works in bash but not in /bin/sh.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Using the dd command in scripts

Sun Feb 17, 2013 11:09 am

jojopi wrote:"&> /dev/null" is shorthand for "> /dev/null 2>&1" (put stdout in /dev/null, put stderr in stdout). But the shorthand is non-standard and best avoided in scripts. It works in bash but not in /bin/sh.
Ah, thank you. I didn't know that. :)

I originally started scripting in csh, then moved on to ksh and eventually bash, so tend to stick to what I'm used to from the early days (except when bash handles things differently).

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Using the dd command in scripts

Sun Feb 17, 2013 3:59 pm

I use dd and I love shell scripting, but dd is just too bl**dy dangerous to use in a shell script IMO. I avoid using it unless I'm 'sharp'. (Old dinosaur - the brain doesn't work 100% sometimes). Putting a dumb shell script in charge is asking for it.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Using the dd command in scripts

Sun Feb 17, 2013 4:16 pm

pluggy wrote:I use dd and I love shell scripting, but dd is just too bl**dy dangerous to use in a shell script IMO. I avoid using it unless I'm 'sharp'. (Old dinosaur - the brain doesn't work 100% sometimes). Putting a dumb shell script in charge is asking for it.
I dunno. If you're very careful with the params, then it's safe to use. Even for a senile old codger like me :)

User avatar
socialdefect
Posts: 110
Joined: Mon Jun 25, 2012 9:02 pm
Location: Tilburg, the Netherlands
Contact: Website

Re: Using the dd command in scripts

Sun Feb 17, 2013 4:33 pm

The way dd is used in the example; writing FROM a device TO a temp file can't do any harm to your system exept for filling all free space on your root or home. If that happens you might not be able to log-in or boot your system until you delete the file from a live cd or something similar.
When dd'ing data the other way around you can never be too carefull though!! I also recommend writing/testing such scripts in a virtual machine or chroot jail and also try not to hard-code device paths into your script for not all devices get the same name on different occasions/distributions.
== If it's not broke... I'm not done fixing it! ==

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Using the dd command in scripts

Sun Feb 17, 2013 5:22 pm

rpdom wrote: If you're very careful with the params, then it's safe to use. Even for a senile old codger like me :)
If....

As to the reading from device being safe, it is, but there's only on letter difference between 'if=' and 'of='
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

xuraax
Posts: 3
Joined: Sat Feb 16, 2013 7:31 am

Re: Using the dd command in scripts

Mon Feb 18, 2013 4:02 am

Thank you guys, I achieved what I wanted perfectly. The command becomes:

dd if.................count=9 > /dev/null 2>&1 &

Regards

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Using the dd command in scripts

Mon Feb 18, 2013 12:56 pm

pluggy wrote:I use dd and I love shell scripting, but dd is just too bl**dy dangerous to use in a shell script IMO. I avoid using it unless I'm 'sharp'. (Old dinosaur - the brain doesn't work 100% sometimes). Putting a dumb shell script in charge is asking for it.
I totally agree with that !

:D :D :D :D :D

Tim

Return to “General discussion”