Você não está pausando para ler a entrada do usuário em qualquer lugar.
echo "You must type 'soff' to end this script."
while [ -f .running ]
do
if read -t 10 -p "I'm still running, type 'soff' to stop me :)... " ans
then
if [[ $ans == "soff" ]]; then
break
fi
fi
done
signoff # shouldn't this be "soff" ?
read -t
retorna uma condição de falha se o valor do tempo limite expirar, portanto, o bloco if
não entrará e você chegará à próxima iteração do ciclo while
.
Além disso, você não precisa criar dinamicamente um arquivo de script: use uma função de shell
signoff () {
rm .running
# do other stuff
}
echo "You must type 'soff' to end this script."
while ...
done
signoff # this calls the function
Para bloquear várias invocações, verifique a existência do arquivo .running como a primeira coisa que seu script faz.
#!/bin/bash
if [[ -f .running ]]; then
echo "I'm already running (on pid $(<.running))"
exit
fi
# put the current pid in the running file
echo $$ > .running
Note que há uma condição de corrida lá. Eu acho que a criação de diretórios é atômica, então você pode fazer isso em vez disso:
#!/bin/bash
if mkdir .running
then
# nobody else is running this script
# store the pid
echo $$ > .running/pid
else
echo "I'm already running (on pid $(<.running/pid))
exit
fi
signoff() {
rm .running/pid
rmdir .running
}
# ...
Colocando tudo junto:
#!/bin/bash
if mkdir .running
then
# nobody else is running this script
# store the pid
echo $$ > .running/pid
else
echo "I'm already running (on pid $(<.running/pid))
exit
fi
signoff() {
rm .running/pid
rmdir .running
}
read -p "What are your words of wisdom Grasshopper?: " words
cat <<WISDOM >>log.dat
-----------------------------------------------
Signon: $(date)
Now hear this: $words
WISDOM
while [[ -d .running ]]
do
if read -t 10 -p "I'm still running, type 'soff' to stop me :)... " ans
then
[[ $ans == "soff" ]] && break
fi
done
signoff