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

Forum rules
Before you post please read this

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

Postby ericramos1990 on Mon Nov 18, 2013 12:03 am

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 ericramos1990 on Mon Nov 18, 2013 12:12 pm, edited 1 time in total.
Keep on tuxin' =)
User avatar
ericramos1990
Level 3
Level 3
 
Posts: 137
Joined: Sun Jul 21, 2013 11:54 am

Linux Mint is funded by ads and donations.
 

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

Postby xenopeek on Mon Nov 18, 2013 10:50 am

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"
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14722
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby ericramos1990 on Mon Nov 18, 2013 12:11 pm

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
Keep on tuxin' =)
User avatar
ericramos1990
Level 3
Level 3
 
Posts: 137
Joined: Sun Jul 21, 2013 11:54 am

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

Postby xenopeek on Mon Nov 18, 2013 12:39 pm

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:
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14722
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby ericramos1990 on Mon Nov 18, 2013 1:04 pm

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?
Keep on tuxin' =)
User avatar
ericramos1990
Level 3
Level 3
 
Posts: 137
Joined: Sun Jul 21, 2013 11:54 am

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

Postby xenopeek on Mon Nov 18, 2013 1:24 pm

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14722
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby ericramos1990 on Mon Nov 18, 2013 2:03 pm

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
Keep on tuxin' =)
User avatar
ericramos1990
Level 3
Level 3
 
Posts: 137
Joined: Sun Jul 21, 2013 11:54 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest