O script não está pausando até eu digitar soff

0

O script chama automaticamente o script de sinalização em vez de pausar até eu digitar "soff"?

Além disso, um usuário não deve conseguir iniciar o script "signon" novamente enquanto estiver em execução. Abaixo está o que eu tenho até agora, por favor, ajude e obrigado!

#!/bin/bash
#getname file used as my base for signon
#gets a few words of wisdom from user and prints to log.dat file

echo -n "What are your words of wisdom Grasshopper?: "
read name

    echo >> log.dat
    echo "
          -----------------------------------------------
          Signon:"     $(date) >> log.dat
    echo "          Now hear this: $name" >> log.dat

touch .running

cat <<EOF > soff
rm .running
rm soff
EOF
chmod +x soff

echo "You must type 'soff' to end this script."
while [ -f .running ]
do
   echo "I'm still running, type 'soff' to stop me :)..."
   sleep 10

done &

signoff
    
por HankG 25.03.2015 / 20:12

1 resposta

1

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 
    
por glenn jackman 25.03.2015 / 21:06