display a - 1 - b - 2 - c - 3…- z - 26 by creating

2019-06-14 17:51发布

问题:

display a1b2c3...z26 by creating 2 process and each process run seperatley with a random sleep of 1 to 5

is there any way to show off the process 1 has to wait for process 2 respectively p1 will generate abcdef... with sleep $[ ( $RANDOM % 5 ) + 1 ]s p2 will generate 12345..with sleep $[ ( $RANDOM % 5 ) + 1 ]s

回答1:

Process 1 ./abc.sh

> cd /home/sanjaisambu/Sanjai/a1b2/ for j in {a..z} do
>         while  [ -f './syncfile' ]
>         do
>         echo 'process 1 waiting'
>         sleep 1
>         done
>         touch ./syncfile
>         echo -n $j  >> a1b2.txt
>         sleep $[ ( $RANDOM % 5 ) + 1 ]s done

Process 2 123.sh

> #!/bin/bash cd /home/sanjaisambu/Sanjai/a1b2/ for j in $(seq 1 26) do
>         while [ ! -f './syncfile' ]
>         do
>         echo 'process 2 is waiting'
>         sleep 1
>         done
>         echo -n - $j -  >> a1b2.txt
>         rm ./syncfile
>         sleep $[ ( $RANDOM % 5 ) + 5 ]s done

When we Execute

./abc.sh & ./123.sh

Output:

a - 1 -b- 2 -c- 3 -d- 4 -e- 5 -f- 6 -g- 7 -h- 8 -i- 9 -j- 10 -k- 11 -l- 12 -m- 13 -n- 14 -o- 15 -p- 16 -q- 17 -r- 18 -s- 19 -t- 20 -u- 21 -v- 22 -w- 23 -x- 24 -y- 25 -z- 26