Difference between revisions of "Bash processes"
From Teknologisk videncenter
(Created page with "=example= <source lang=bash line> #!/bin/bash count() { NAME=$1 COUNT_TO=$2 COUNTER=0 echo -e "My name is $NAME my PID is $BASHPID" # $$ does...") |
m |
||
Line 8: | Line 8: | ||
COUNTER=0 | COUNTER=0 | ||
echo -e "My name is $NAME my PID is $BASHPID" # $$ does not show subshell PID | echo -e "My name is $NAME my PID is $BASHPID" # $$ does not show subshell PID | ||
− | while test $COUNTER - | + | while test $COUNTER -lt $COUNT_TO |
do | do | ||
sleep 1 | sleep 1 | ||
Line 37: | Line 37: | ||
Hans countet to 3 | Hans countet to 3 | ||
Hans countet to 4 | Hans countet to 4 | ||
− | |||
Ulla signing off | Ulla signing off | ||
Hans countet to 5 | Hans countet to 5 | ||
− | |||
Hans signing off | Hans signing off | ||
Main thread signing off | Main thread signing off | ||
</source> | </source> | ||
[[Category:Linux]] | [[Category:Linux]] |
Revision as of 08:20, 16 December 2022
example
1 #!/bin/bash
2
3 count() {
4 NAME=$1
5 COUNT_TO=$2
6 COUNTER=0
7 echo -e "My name is $NAME my PID is $BASHPID" # $$ does not show subshell PID
8 while test $COUNTER -lt $COUNT_TO
9 do
10 sleep 1
11 let COUNTER=COUNTER+1
12 echo -e "$NAME countet to $COUNTER"
13 done
14 echo -e "$NAME signing off"
15 }
16
17 #Main
18 echo -e "I am the main function and my PID is $BASHPID"
19
20 count "Hans" 5 &
21 count "Ulla" 3 &
22
23 wait # Wait for subshells to finish
24 echo -e "Main thread signing off"
Gives the following output
My name is Hans my PID is 813980
My name is Ulla my PID is 813981
Hans countet to 1
Ulla countet to 1
Ulla countet to 2
Hans countet to 2
Ulla countet to 3
Hans countet to 3
Hans countet to 4
Ulla signing off
Hans countet to 5
Hans signing off
Main thread signing off