LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2012, 11:21 AM   #1
jheengut
Member
 
Registered: Sep 2006
Location: Providence, Moka Mauritius
Distribution: Slackware, Lubuntu
Posts: 352
Blog Entries: 16

Rep: Reputation: 51
Cool make code in bash go parallel


Hi

I have a script that that needs the for loop to work on several items in a directory alas using

Code:
 command *
does not work.

I want to make the code more parallel


ie

from

Code:
for i in `seq $start  $finish` ; do
     command _something_ending_in_$i
done
to something like this

Code:
for j in ` seq $start $increment $finish ` ;  do 
  for i in `seq $I_start $j ` ; do
     command _something_ending_in_$i

Quote:
some code needs to be added here
done
Quote:
and here also
done
Quote:
$j should always be $increment greater than $I_start
I experimented with
(( )) ARITHMETIC EVALUATION
to perform the job but it gets hard to understand and maintain.

Is my algorithm bad ??
 
Old 11-08-2012, 01:14 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
#!/bin/bash
for i in `seq 1 10`
do touch test.$i.txt
done
ls -al *.txt
exit 0
#EOF
and "command _something_ending_in_$i" should be "command _something_ending to $i"

See http://mywiki.wooledge.org/BashPitfalls

Here's some light reading that may interest you:
Various Linux Guides I have collected:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.grymoire.com/Unix/Sed.html
http://tldp.org/LDP/abs/abs-guide.pdf
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://rute.2038bug.com/index.html.gz
http://mylinuxbook.com/linux-shell-environment/
http://www.subsignal.org/doc/AliensB...0Of%20Contents
http://www.rigacci.org/docs/biblio/o...TLCL-09.12.pdf

Where is David the H: when I need him.

Last edited by Habitual; 11-08-2012 at 01:17 PM.
 
Old 11-08-2012, 01:26 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
to make something run multithreaded you can try something like:
Code:
#!/bin/bash

i=0
while [ $i -le 10 ]
do
 i=`expr $i + 1`
 echo thread 1: i = $i
done &

j=100
while [ $j -le 110 ]
do
 j=`expr $j + 1`
 echo thread 2: j = $j
done &

Last edited by schneidz; 11-08-2012 at 01:28 PM.
 
Old 11-08-2012, 06:12 PM   #4
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Please have a look at http://www.gnu.org/software/parallel/

Lyle.
 
1 members found this post helpful.
Old 11-09-2012, 05:11 PM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by jheengut View Post
I have a script that that needs the for loop to work on several items in a directory alas using
On one and the same machine – how many cores are available? Or on several machines?

Depending what and how often you want to do it, you can also look into a small queuing system like GNUbatch.
 
Old 11-25-2012, 11:45 AM   #6
jheengut
Member
 
Registered: Sep 2006
Location: Providence, Moka Mauritius
Distribution: Slackware, Lubuntu
Posts: 352

Original Poster
Blog Entries: 16

Rep: Reputation: 51
Cool It answers some questions?

Quote:
Originally Posted by lyle_s View Post
Please have a look at http://www.gnu.org/software/parallel/

Lyle.
It answers some questions?
 
Old 11-26-2012, 12:38 AM   #7
propofol
Member
 
Registered: Nov 2007
Location: Seattle
Distribution: Debian Wheezy & Jessie; Ubuntu
Posts: 334

Rep: Reputation: 60
I am not sure I follow exactly what you want to do. Is xargs and option?

Code:
seq 1 10 | xargs -P 4 -n 1 -I{} echo "Testing {}"
Regards,
Stefan
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Slackware parallel make (make -j?) question. Daedra Slackware 4 02-19-2012 08:14 PM
Parallel Port Interrupt problem with C code? James Scrymgeour Linux - Kernel 15 11-17-2010 03:26 PM
Return code for parallel running threads jitendriya.dash Programming 2 05-13-2009 12:12 AM
how to open files in a parallel code??? shreya.hiflying87 Programming 2 05-30-2008 02:53 AM
how to make my code to work on bsd (pptp script on bash) jabka *BSD 1 05-01-2007 12:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration