![如何在bash中使用多线程](https://linux22.com/image/138913/%E5%A6%82%E4%BD%95%E5%9C%A8bash%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B.png)
我试图在后台放置一个 while 循环并增加 count 的值,但后台 while 循环不会更新 count 变量。
#!/bin/bash
count=0
((count++))
echo " External $count"
while [ $count -ne 0 ]
do
echo "Inside $count.. sleeping 5"
sleep 5
done &
sleep 10
((count++))
echo " External $count"
sleep 20
((count--))
((count--))
wait
输出:
External 1
Inside 1.. sleeping 5
Inside 1.. sleeping 5
External 2
Inside 1.. sleeping 5
Inside 1.. sleeping 5
Inside 1.. sleeping 5
预期输出:
External 1
Inside 1.. sleeping 5
Inside 1.. sleeping 5
External 2
Inside 2.. sleeping 5
Inside 2.. sleeping 5
Inside 2.. sleeping 5