I want to make a simple countdown that when it ends, it continues the remainding of the script afterwards.
However, I also want to have the option to exit the countdown by pressing "Enter" and go directly into the script if I decide to skip the countdown portion.
The only thing I need help with is exiting the countdown with "Enter" I do not want to use "Ctrl-c" since that will exit the entire process, when I just want to skip the countdown.
I tried several variations of using the "read" command, and "sleep," and I am using a "while" loop for the countdown, but no luck.
I hope you bash geniuses can help, what I want to do seems pretty simple (I hope)
Thanks in advance guys!
(SOLVED) I need a countdown and the ability to exit it
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
(SOLVED) I need a countdown and the ability to exit it
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Please help, I need a countdown and the ability to exit
A challenge! How about this one, it counts down from 10 to 1 and then continues or continues as you press Enter.
Code: Select all
#!/bin/bash
for SECONDS in {10..1} ; do
printf "\rContinuing in $SECONDS seconds. Press Enter to continue now. "
read -s -t 1
[[ $? -eq 0 ]] && break
done
printf "\n"
Re: Please help, I need a countdown and the ability to exit
xenopeek that is PERFECT!
I'm going to stare at the code for a really long time until I comprehend it lol!
I never knew of the read -t option, I'm guessing that was essential to the script?
Thanks a lot dude, it was really helpful.
P.S. Sorry about the 24 hr forum, I guess I did make it personal
I'm going to stare at the code for a really long time until I comprehend it lol!
I never knew of the read -t option, I'm guessing that was essential to the script?
Thanks a lot dude, it was really helpful.
P.S. Sorry about the 24 hr forum, I guess I did make it personal
Re: (SOLVED) I need a countdown and the ability to exit it
Well, let's break it down line by line:
Loops 10 times, counting down from 10 to 1 in the $SECONDS variable.
Returns the cursor to the beginning of the line (the '\r') and prints the message with the number of $SECONDS remaining.
Reads input from the terminal, '-s' making it silent so no key presses are echoed to the screen, '-t 1' setting a timeout of 1 second before the read command will abort waiting for input of a single line (which is finished only by pressing the Enter key).
Checks the return value of the read command. As per the documentation, a return value of zero means input of a single line was received (so Enter key was pressed, and we don't care about any other keys pressed ). If that was the case, breaks the loop and continues immediately to the statement after the `done` line.
Closes the loop and moves the cursor to the next line.
The bash manpage has excellent documentation on the built-in commands, like the read command. `man bash` to read the manpage. Search for ^SHELL BUILTIN to quickly jump to the section documenting all the builtin commands. (To do that, type the following sequence in man and press Enter: /^SHELL BUILTIN). It's worth your time to one day sit down and read the enter manpage
Code: Select all
for SECONDS in {10..1} ; do
Code: Select all
printf "\rContinuing in $SECONDS seconds. Press Enter to continue now. "
Code: Select all
read -s -t 1
Code: Select all
[[ $? -eq 0 ]] && break
Code: Select all
done
printf "\n"
The bash manpage has excellent documentation on the built-in commands, like the read command. `man bash` to read the manpage. Search for ^SHELL BUILTIN to quickly jump to the section documenting all the builtin commands. (To do that, type the following sequence in man and press Enter: /^SHELL BUILTIN). It's worth your time to one day sit down and read the enter manpage
Re: (SOLVED) I need a countdown and the ability to exit it
Things you taught me today:
Too many things!
I remember trying to do something similar, but I failed. So the ? means the most recent input? I would have never thought of that in a million years lol.
I knew about man pages, but I didn't know about man bash. That's going to really, REALLY help me out.
This is honestly making me regret my major in college, programming/coding is so interesting, even though I know I would have been forced to use Microsoft eventually...
Thanks so much dude!! According to your sig, are you really using Nadia still? Is there a reason for that? Or it's just outdated lol and you are at Petra RC?
Too many things!
Code: Select all
[[ $? -eq 0 ]] && break
I knew about man pages, but I didn't know about man bash. That's going to really, REALLY help me out.
This is honestly making me regret my major in college, programming/coding is so interesting, even though I know I would have been forced to use Microsoft eventually...
Thanks so much dude!! According to your sig, are you really using Nadia still? Is there a reason for that? Or it's just outdated lol and you are at Petra RC?
Re: (SOLVED) I need a countdown and the ability to exit it
All clues are in the bash manpage But, $? is a builtin variable and contains the exit status of the last command run.
And yes, I'm still using Nadia. There's a nasty bug on my motherboard which at the time prevented me from running Olivia. I'll try again with Petra and hope there's a workaround that works like it did on Nadia. I've been running Olivia in VirtualBox though, which works fine.
And yes, I'm still using Nadia. There's a nasty bug on my motherboard which at the time prevented me from running Olivia. I'll try again with Petra and hope there's a workaround that works like it did on Nadia. I've been running Olivia in VirtualBox though, which works fine.
Re: (SOLVED) I need a countdown and the ability to exit it
I hope that works out for you. And once again thank you for the valuable information, I hope you have a great week!
Cheers
-Eric Ramos
Cheers
-Eric Ramos