| | |
Counter
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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)
#!/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
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)
•
•
Join Date: Dec 2008
Posts: 63
Reputation:
Solved Threads: 13
0
#2 26 Days Ago
•
•
•
•
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)
timer "00:00:10" &
Please put code inside [code] tags, as I have done above and below, and indent it for legibility.
The standard syntax for defining functions is:
Shell Scripting Syntax (Toggle Plain Text)
timer()
Why use OLD_IFS? Just make IFS local:
Shell Scripting Syntax (Toggle Plain Text)
local IFS=:
•
•
•
•
Shell Scripting Syntax (Toggle Plain Text)
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.
•
•
•
•
Shell Scripting Syntax (Toggle Plain Text)
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:
Shell Scripting Syntax (Toggle Plain Text)
if [ "$code" = "1234567890" ]; then
•
•
•
•
Shell Scripting Syntax (Toggle Plain Text)
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:
Shell Scripting Syntax (Toggle Plain Text)
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)
printf "\e7\e[1:1H%02d:%02d:%02d\e8" \ $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
Chris F.A. Johnson
http://cfajohnson.com
http://cfajohnson.com
![]() |
Similar Threads
- ASP .NET hit counter? (ASP.NET)
- letter and word counter (C)
- I'm looking for a counter.. (HTML and CSS)
- Counter issues (C)
- ASP .NET database hit counter (ASP.NET)
- Counter Strike issue (Windows Software)
- Page counter print accounting (*nix Software)
Other Threads in the Shell Scripting Forum
- Previous Thread: Shell script to read lines in a text file and filter user data
- Next Thread: I need help with the cut command.
| Thread Tools | Search this Thread |





