Redirect hdparm output to grep?

Alan Dayley adayley at gmail.com
Wed May 14 05:48:43 UTC 2008


I am running Busybox 1.10.1, no patches, on an ARM embedded system
with a Linux 2.4.x kernel and the ash shell.

In a shell script I want to do something based on the presence or not
of a USB mounted partition.  hdparm can indirectly report this
information with a partition rescan like this:

hdparm -z /dev/sda

I want to then parse the output of this command to determine if a
partition was found or not.  My proposed command is:

/bin/hdparm -z /dev/sda | grep -c sda1

This should return a count of the string "sda1" in the output from
hdparm,  But, it does not work.  First it appears that most of the
hdparm output goes to stderr instead of stdout.  Secondly the various
piping combinations I have tried don't produce output to grep.

Below are examples of what I have tried, exposing my shell scripting
ignorance.  Note that the command lines with a pipe to grep end with a
"0" which is grep's contribution to the output.  It is always "0" even
if the sought text is present.  The attempted pipes to a file are even
worse syntax, I'm sure.

This question is not necessarily a Busybox issue even though both
hdparm and grep in use here are from Busybox.  I may just not have the
shell scripting chops to know the right syntax.  Anyone have any
thoughts or education to give me?

Alan

------------------
---[attempted pipe to grep]---
------------------
# /bin/hdparm -z /dev/sda | /bin/grep -c "sda: sda1" -
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
0
# /bin/hdparm -z /dev/sda | /bin/grep -c sda1
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
0
# /bin/hdparm -z /dev/sda

/dev/sda:
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# /bin/hdparm -z /dev/sda | /bin/grep sda1
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1

------------------
---[attempted pipe to a file named "parts"]---
------------------

# /bin/hdparm -z /dev/sda > parts
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# cat parts

/dev/sda:
# /bin/hdparm -z /dev/sda >> parts
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# cat parts

/dev/sda:

/dev/sda:
# /bin/hdparm -z /dev/sda 2>&1 > parts
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# cat parts

/dev/sda:
# /bin/hdparm -z /dev/sda &> parts
#
/dev/sda:
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# cat parts
[1] + Done                       /bin/hdparm -z /dev/sda
# /bin/hdparm -z /dev/sda 2>&1

/dev/sda:
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# /bin/hdparm -z /dev/sda

/dev/sda:
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# /bin/hdparm -z /dev/sda 2>&1 > parts
SCSI device sda: 1999872 512-byte hdwr sectors (1024 MB)
sda: Write Protect is off
 sda: sda1
# cat parts

/dev/sda:



More information about the busybox mailing list