SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-09-07 16:17:28

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

[Solved]How do I prevent a for loop from forking processes?

I was writing a secure-delete script (just felt like it big_smile) and was wondering if there was any way to prevent a for-loop (in bash) from forking the processes within.  I have a dd command inside the for loop.  Here's the script:

#!/bin/bash
SIZE=$(ls -la | grep $1 | sed 's/-.*chiraag chiraag //' | sed 's/[A-Z].*//' | sed 's/\ //');
#echo $SIZE
for i in {1..35}
do
dd if=/dev/urandom of=$1 count=$(($SIZE / 2)) bs=2 > /dev/null 2>&1&;
done
#for i in {1..1000}
#do
dd if=/dev/zero of=$1 count=$(($SIZE / 2)) bs=2 > /dev/null 2>&1&
#done
rm -rf $1

I decided to run the zero function only once because even if people recover the file, it will be junk big_smile

Thanks in advance!

- Chaanakya

Last edited by chaanakya (2011-09-07 21:28:13)


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

Help fund CrunchBang, donate to the project!

#2 2011-09-07 16:29:27

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: [Solved]How do I prevent a for loop from forking processes?

So you don't want dd to have it's own PID? I don't know where to start, how about you describe step by step what happens (system-wise) line by line in your program (and in relation to bash) and I shout stop? :-D

dd is not a function/procedure/object, dd is a program. If something should run inside bash without being forked, it must be something bash can interprete. Since bash does not interprete dd, but simply calls it in /bin/dd to accept the given parameters.

So the answer is: Reimplement dd in bash. What's the matter with the fork here?


I'm so meta, even this acronym

Offline

#3 2011-09-07 16:46:17

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: [Solved]How do I prevent a for loop from forking processes?

This isn't answering your question at all, but you can put those three sed calls together and only call it once, saving two subshells:

chaanakya wrote:

sed 's/-.*chiraag chiraag //' | sed 's/[A-Z].*//' | sed 's/\ //'

ie

 sed 's/-.*chiraag chiraag //;s/[A-Z].*//;s/\ //'

Also, I can't find a reference to that "i" in the do loop:

for i in {1..35}
do
dd if=/dev/urandom of=$1 count=$(($SIZE / 2)) bs=2 > /dev/null 2>&1&;
done

Last edited by johnraff (2011-09-07 16:47:16)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

#4 2011-09-07 17:24:05

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

Re: [Solved]How do I prevent a for loop from forking processes?

Yeah...the i is only there as the index number - I don't actually need it in my command.  I just want to run the dd command 35 times, and this is the easiest way to do it (I think)...is there an easier way which would wait for the first dd to complete before calling it again as the second iteration?


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

#5 2011-09-07 17:35:04

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: [Solved]How do I prevent a for loop from forking processes?

chaanakya wrote:

is there an easier way which would wait for the first dd to complete before calling it again as the second iteration?

while-loop maybe?

Offline

#6 2011-09-07 20:50:02

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

Re: [Solved]How do I prevent a for loop from forking processes?

But would the while-loop wait?  That's what I'm not sure of, and I surely do *not* want to have this thing nearly fork-bomb my comp again tongue  (although...35 writes isn't a lot, so it shouldn't hang up my laptop that badly...)


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

#7 2011-09-07 21:15:40

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

Re: [Solved]How do I prevent a for loop from forking processes?

Awebb: every time the for-loop executes, a new dd process is spawned.  This means that at one point, there were 1000 dd processes all writing to the same file...not a pretty sight.... the whole computer froze and I got errors with Resource Temporarily Unavailable etc.  I'm just trying to see if I can avoid that again...admittedly, with 35 passes, it shouldn't actually matter - there won't be many processes.  However, it would still be nice if I could get each iteration to wait until the previous iteration had finished writing to the file.

Thanks in advance!

- Chaanakya


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

#8 2011-09-07 21:27:47

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

Re: [Solved]How do I prevent a for loop from forking processes?

Okay...wow...I was being *really* stupid...I put an "&" at the end!!!!!  I should have put a ";".  I wanted to hide all output, so I used a part of a script I had made earlier...neglected to see that I had an "&" at the end... tongue tongue tongue  Well...I guess the topic's closed! big_smile


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

#9 2011-09-07 21:46:35

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: [Solved]How do I prevent a for loop from forking processes?

Haha, assuming the question was stupid, I didn't look at the code closely enough to see the missing semikolon. "Never assume..." :-D

But you had me for a moment. Wildly forking dd's... nah :-P


I'm so meta, even this acronym

Offline

#10 2011-09-08 05:15:35

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: [Solved]How do I prevent a for loop from forking processes?

Whoo-hoo - you caught us all out there vYTh5OQ

(actually even a semicolon isn't needed if you've got a linebreak)


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
#! forum moderator    BunsenLabs

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo