LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   An example from the dd manual. (https://www.linuxquestions.org/questions/linux-newbie-8/an-example-from-the-dd-manual-929897/)

stf92 02-17-2012 08:50 AM

An example from the dd manual.
 
Kernel 2.6.21.5, Slackware 12.0

Hi:
Code:

      Sending a USR1 signal to a running `dd' process makes it print I/O sta-
      tistics to standard error and then resume copying.

              $ dd if=/dev/zero of=/dev/null& pid=$!
              $ kill -USR1 $pid; sleep 1; kill $pid

              18335302+0  records  in  18335302+0 records out 9387674624 bytes
              (9.4 GB) copied, 34.6279 seconds, 271 MB/s

How does 'pid=$!' fit into the dd syntax? Is the first line a single command? The meaning of 'pid=$!' is clear to me. As a second question, why to sleep 1, then kill $pid? 'kill -USR1 $pid' would do exactly what is intended that is, print and resume copying.

jhwilliams 02-17-2012 08:56 AM

Hey STF,

I use this approach myself sometime. Anyway, here's my clarification of that example (which is exactly that, an example of little practicality.)

Code:

# Write from /dev/zero into /dev/null, and do it in the background (note the &.)
dd if=/dev/zero of=/dev/null &

# Get the pid of whatever the last backgrounded process was (see bash man page.)
# Note that this is a new, separate statement, not an argument to dd,
# as could be mistaken from the original text.
pid=$!

# Now, print out the progress using USR1 interrupt.
kill -USR1 $pid;

# The sleeping makes the behavior more clear in the example, but probably isn't necessary.
sleep 1;

# Okay, examples over. Actually kill (terminate) the process.
kill $pid


stf92 02-17-2012 10:21 AM

Oh thanks, jhwilliams. I knew the meaning of pid=$! as I had looked for ! in the bash man. pid is an arbitrary name, and $! extracts the value of !. But in the first line of the example, there is not a semicolon (';') to separate the two commands. But bash interpreted the right way! It seems as if, after an ampersand, the semicolon is superfluous.

By the way, what is the meaning of USR1 or a reference to it. I have a demo board, able to run linux (embedded) with two LEDs (light indicator) , USR0 and USR1.

jhwilliams 02-17-2012 10:32 AM

Checkout the entry for signal in section 7 of the man pages on your platform:

Code:

man 7 signal
[quote] there is not a semicolon (';') to separate the two commands[/code]

This has got me before, too. It's more confusing in a for loop syntax:
Code:

for item in $list; do background_job $item & done
Which just looks wrong, but isn't.

stf92 02-17-2012 10:35 AM

Thanks a lot!

jhwilliams 02-17-2012 10:35 AM

NP :-)

stf92 02-18-2012 11:44 AM

I rushed to consider the thing solved: What makes ';' redundant?

catkin 02-18-2012 11:50 AM

Quote:

Originally Posted by stf92 (Post 4605937)
I rushed to consider the thing solved: What makes ';' redundant?

http://www.gnu.org/software/bash/man...ref.html#Lists

stf92 02-18-2012 12:05 PM

Ahhh, '&' acts as a separator too,and with the same precedence as ';'! Most kind of you.


All times are GMT -5. The time now is 11:26 AM.