
Aquí hay un script escrito para bash:
#!/bin/bash
sleep 1 &
sleep 2 &
sleep 3 &
for t in {10..0}
do
# jobs
echo "Waiting("$t")..."
JCNT=`jobs | wc -l`
echo "JCNT="$JCNT
if [ $JCNT -eq 0 ]; then
break
fi
sleep 1
done
exit 0
Funciona durante 10 segundos, pero no durante 3 segundos como se esperaba. Pero si se descomenta el comando "trabajos", funciona bien. No tengo idea de por qué. ¿Me puedes ayudar?
La salida del script con "trabajos" comentaba:
Waiting(10)...
JCNT=3
Waiting(9)...
JCNT=2
Waiting(8)...
JCNT=1
Waiting(7)...
JCNT=1
Waiting(6)...
JCNT=1
Waiting(5)...
JCNT=1
Waiting(4)...
JCNT=1
Waiting(3)...
JCNT=1
Waiting(2)...
JCNT=1
Waiting(1)...
JCNT=1
Waiting(0)...
JCNT=1
Y con el comando "trabajos" sin comentar:
[1] Running sleep 1 &
[2]- Running sleep 2 &
[3]+ Running sleep 3 &
Waiting(10)...
JCNT=3
[1] Done sleep 1
[2]- Running sleep 2 &
[3]+ Running sleep 3 &
Waiting(9)...
JCNT=2
[2]- Done sleep 2
[3]+ Running sleep 3 &
Waiting(8)...
JCNT=1
[3]+ Done sleep 3
Waiting(7)...
JCNT=0
AGREGADO
Modifiqué el script de esta manera:
#!/bin/bash
sleep 1 &
sleep 2 &
sleep 3 &
for t in {10..0}
do
KKK=`jobs`
echo $KKK
echo "Waiting("$t")..."
JCNT=`jobs | wc -l`
echo "JCNT="$JCNT
if [ $JCNT -eq 0 ]; then
break
fi
sleep 1
done
exit 0
...y descubrí que hay información sobre el último trabajo realizado:
[3] Done sleep 3
Llamar "trabajos" simplemente elimina la lista de trabajos realizados. Hacer "jobs|wc -l" es la forma incorrecta de contar los trabajos activos. La forma correcta es "jobs -r|wc -l".
Respuesta1
No responde a tu pregunta, solo como pista:
cuando ejecuta el trabajo por separado en el bucle. Funciona/cuenta como se esperaba.
Pero no tengas pegamento, por ahora.