Reply

Join Date: Oct 2009
Posts: 1
Reputation: danialit is an unknown quantity at this point 
Solved Threads: 0
danialit danialit is offline Offline
Newbie Poster

Counter

 
0
  #1
Oct 24th, 2009
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; 25 Days Ago at 7:08 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 63
Reputation: cfajohnson is an unknown quantity at this point 
Solved Threads: 13
cfajohnson cfajohnson is offline Offline
Junior Poster in Training
 
0
  #2
26 Days Ago
Originally Posted by danialit View 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??

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

The standard syntax for defining functions is:

Shell Scripting Syntax (Toggle Plain Text)
  1. timer()
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=:
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.
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
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))
Chris F.A. Johnson
http://cfajohnson.com
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC