(SOLVED) I need a countdown and the ability to exit it

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
ericramos1990

(SOLVED) I need a countdown and the ability to exit it

Post by ericramos1990 »

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!
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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Please help, I need a countdown and the ability to exit

Post by xenopeek »

A challenge! :D 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"
Image
ericramos1990

Re: Please help, I need a countdown and the ability to exit

Post by ericramos1990 »

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
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: (SOLVED) I need a countdown and the ability to exit it

Post by xenopeek »

Well, let's break it down line by line:

Code: Select all

for SECONDS in {10..1} ; do
Loops 10 times, counting down from 10 to 1 in the $SECONDS variable.

Code: Select all

   printf "\rContinuing in $SECONDS seconds. Press Enter to continue now. "
Returns the cursor to the beginning of the line (the '\r') and prints the message with the number of $SECONDS remaining.

Code: Select all

   read -s -t 1
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).

Code: Select all

   [[ $? -eq 0 ]] && break
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 :wink:). If that was the case, breaks the loop and continues immediately to the statement after the `done` line.

Code: Select all

done
printf "\n"
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 :wink:
Image
ericramos1990

Re: (SOLVED) I need a countdown and the ability to exit it

Post by ericramos1990 »

Things you taught me today:

Too many things!

Code: Select all

[[ $? -eq 0 ]] && break
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?
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: (SOLVED) I need a countdown and the ability to exit it

Post by xenopeek »

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.
Image
ericramos1990

Re: (SOLVED) I need a countdown and the ability to exit it

Post by ericramos1990 »

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
Locked

Return to “Scripts & Bash”