LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-22-2007, 08:19 PM   #1
1veedo
Member
 
Registered: Dec 2004
Location: WV, USA
Distribution: Gentoo, Debian
Posts: 186

Rep: Reputation: 34
Bash Script -- run two processes at once?


I think this might be forking but I read the wiki and it says a fork is a copy of the parent, which isn't really what I want...

Basically I want two chunks of code running at the same time. I'm sure it's something really simple -- eg kind of like how there's process1 && process2. Or even better would be the ability to directly share variables in the same bash script --

#!/bin/bash

chunk1{

}
chunk 2{

}

Either way works.
 
Old 12-22-2007, 08:30 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Something like this?

Code:
#!/bin/bash

f() { for((i=0;$i<10;i++)); do echo $i; sleep 1; done; }
g() { for((j=100;$j>90;j--)); do echo $j; sleep 1; done; }

f &
g
teddy@toshiba~$ ./two.sh
0
100
99
1
98
2
3
97
4
96
5
95
94
6
93
7
92
8
91
9
 
Old 12-22-2007, 09:40 PM   #3
1veedo
Member
 
Registered: Dec 2004
Location: WV, USA
Distribution: Gentoo, Debian
Posts: 186

Original Poster
Rep: Reputation: 34
Yeah, this is what I was looking for. & just puts a process in the background (forgot all about that trick). Is this the same thing as forking or different?

It's weird though I had this bash script and arranged the code in the two functions. I have some image processes running in f() and when I run f& (eg f & g) I get these png errors. But if I run f (eg g & f) it doesn't error. Why would some programs need to be ran "directly"? Why cant they be put in the background?

edit -- Hm for some reason it works now. I never touched the code. Weird.

Thanks for the info that was exactly what I was looking for.

Last edited by 1veedo; 12-22-2007 at 09:42 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 03:28 PM
run files using BASH script kkpal Programming 2 11-02-2007 08:21 AM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
perl/bash script to monitor all processes running in my machine pudhiyavan Linux - Security 4 07-19-2005 02:09 PM
how to run one bash script from within another? babag Programming 9 04-28-2005 12:12 AM

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

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

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