Hi there, I have a unsolved problem here...my scripting do not go as I want, actually I want it to be solving the password in 10 seconds, but my scripting is run the timer and then only run the password solving.... anyone got any idea how to combine it??

#!/bin/bash
function timer
{
local OLD_IFS="${IFS}"
IFS=":"
local ARR=( $1 )
local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START

while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))

printf "\r%02d:%02d:%02d" \
$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

sleep 1
done
IFS="${OLD_IFS}"
echo " "
}

timer "00:00:10"
time=30
tries=3
start=$(date +%s)
left=$time
while true
do
left=$(($time-$(date +%s)+start))
read -t $left -p "Code: " code
((tries--))
if [[ "$code" == "1234567890" ]]; then 
echo "CORRECT!!! Game Deactivated"
break
else
if (( $left<=0 || $tries==0 ));then
echo "Game Activated!!!"
break
fi
echo "WRONG!!! Please Try Again... There are $left seconds and $tries left."
fi
done
exit

Hi there, I have a unsolved problem here...my scripting do not go as I want, actually I want it to be solving the password in 10 seconds, but my scripting is run the timer and then only run the password solving.... anyone got any idea how to combine it??

Put the function call into the background:

timer "00:00:10" &

Please put code inside code-tags, as I have done above and below, and indent it for legibility.

function timer

The standard syntax for defining functions is:

timer()

quote:

{
  local OLD_IFS="${IFS}"
  IFS=":"

Why use OLD_IFS? Just make IFS local:

local IFS=:

quote:

  local ARR=( $1 )
  local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))

Bash has a shell variable named SECONDS. It is bad practice to use pre-existing variables.

Using all-uppercase variable names increases your chances of a conflict.

quote:

  local START=$(date +%s)
  local END=$((START + SECONDS))
  local CUR=$START

  while [[ $CUR -lt $END ]]
  do
    CUR=$(date +%s)
    LEFT=$((END-CUR))

    printf "\r%02d:%02d:%02d" \
      $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

    sleep 1
  done
  IFS="${OLD_IFS}"
  echo " "
}

timer "00:00:10"
time=30
tries=3
start=$(date +%s)
left=$time
while true
do
  left=$(($time-$(date +%s)+start))
  read -t $left -p "Code: " code
  ((tries--))
  if [[ "$code" == "1234567890" ]]; then

The standard syntax for testing values is:

if [ "$code" = "1234567890" ]; then

quote:

    echo "CORRECT!!! Game Deactivated"
    break
  else
    if (( $left<=0 || $tries==0 ));then
      echo "Game Activated!!!"
      break
    fi
    echo "WRONG!!! Please Try Again... There are $left seconds and $tries left."
  fi
done

exit

You should kill the background job when you are finished:

kill $!

You will have problems trying to display the timer independently of the prompt and user input.

Try printing it at a specific location on the screen:

 printf "\e7\e[1:1H%02d:%02d:%02d\e8" \
      $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.