944,175 Members | Top Members by Rank

Ad:
Oct 24th, 2009
0

Counter

Expand Post »
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??

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. function timer
  3. {
  4. local OLD_IFS="${IFS}"
  5. IFS=":"
  6. local ARR=( $1 )
  7. local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
  8. local START=$(date +%s)
  9. local END=$((START + SECONDS))
  10. local CUR=$START
  11.  
  12. while [[ $CUR -lt $END ]]
  13. do
  14. CUR=$(date +%s)
  15. LEFT=$((END-CUR))
  16.  
  17. printf "\r%02d:%02d:%02d" \
  18. $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
  19.  
  20. sleep 1
  21. done
  22. IFS="${OLD_IFS}"
  23. echo " "
  24. }
  25.  
  26. timer "00:00:10"
  27. time=30
  28. tries=3
  29. start=$(date +%s)
  30. left=$time
  31. while true
  32. do
  33. left=$(($time-$(date +%s)+start))
  34. read -t $left -p "Code: " code
  35. ((tries--))
  36. if [[ "$code" == "1234567890" ]]; then
  37. echo "CORRECT!!! Game Deactivated"
  38. break
  39. else
  40. if (( $left<=0 || $tries==0 ));then
  41. echo "Game Activated!!!"
  42. break
  43. fi
  44. echo "WRONG!!! Please Try Again... There are $left seconds and $tries left."
  45. fi
  46. done
  47. exit
Last edited by peter_budo; Nov 2nd, 2009 at 7:08 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
danialit is offline Offline
1 posts
since Oct 2009
Nov 1st, 2009
0
Re: Counter
Click to Expand / Collapse  Quote originally posted by danialit ...
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:

Shell Scripting Syntax (Toggle Plain Text)
  1. timer "00:00:10" &

Please put code inside [code] tags, as I have done above and below, and indent it for legibility.
Quote ...
Shell Scripting Syntax (Toggle Plain Text)
  1. function timer

The standard syntax for defining functions is:

Shell Scripting Syntax (Toggle Plain Text)
  1. timer()
Quote ...
Shell Scripting Syntax (Toggle Plain Text)
  1.  
  2. {
  3. local OLD_IFS="${IFS}"
  4. IFS=":"

Why use OLD_IFS? Just make IFS local:

Shell Scripting Syntax (Toggle Plain Text)
  1. local IFS=:
Quote ...
Shell Scripting Syntax (Toggle Plain Text)
  1. local ARR=( $1 )
  2. 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 ...
Shell Scripting Syntax (Toggle Plain Text)
  1.  
  2. local START=$(date +%s)
  3. local END=$((START + SECONDS))
  4. local CUR=$START
  5.  
  6. while [[ $CUR -lt $END ]]
  7. do
  8. CUR=$(date +%s)
  9. LEFT=$((END-CUR))
  10.  
  11. printf "\r%02d:%02d:%02d" \
  12. $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
  13.  
  14. sleep 1
  15. done
  16. IFS="${OLD_IFS}"
  17. echo " "
  18. }
  19.  
  20. timer "00:00:10"
  21. time=30
  22. tries=3
  23. start=$(date +%s)
  24. left=$time
  25. while true
  26. do
  27. left=$(($time-$(date +%s)+start))
  28. read -t $left -p "Code: " code
  29. ((tries--))
  30. if [[ "$code" == "1234567890" ]]; then

The standard syntax for testing values is:

Shell Scripting Syntax (Toggle Plain Text)
  1. if [ "$code" = "1234567890" ]; then
Quote ...
Shell Scripting Syntax (Toggle Plain Text)
  1.  
  2. echo "CORRECT!!! Game Deactivated"
  3. break
  4. else
  5. if (( $left<=0 || $tries==0 ));then
  6. echo "Game Activated!!!"
  7. break
  8. fi
  9. echo "WRONG!!! Please Try Again... There are $left seconds and $tries left."
  10. fi
  11. done
  12.  
  13. exit

You should kill the background job when you are finished:

Shell Scripting Syntax (Toggle Plain Text)
  1. 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:

Shell Scripting Syntax (Toggle Plain Text)
  1. printf "\e7\e[1:1H%02d:%02d:%02d\e8" \
  2. $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
Reputation Points: 25
Solved Threads: 23
Junior Poster
cfajohnson is offline Offline
193 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Shell script to read lines in a text file and filter user data
Next Thread in Shell Scripting Forum Timeline: I need help with the cut command.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC