PDA

View Full Version : countdown



Erik-joix
8th May 2004, 05:41 AM
Hi guys.
I don't consider myself to be a coder/programmer but i know some about it. I passed a C++ class a while back. Anyway, i wrote this little script some time ago to basically make it easyer for myself to look for certain types of files and delete (shred) them. I put in a little countdown but i want to make it look nicer. Here's how it is right now.

echo -e "You have entered\n"
echo -e "Directory: " "\033[0;34m$location"; tput sgr0
echo -e "File type: " "\033[0;34m$type\n"; tput sgr0
echo "Preparing search....."
sleep 5
echo -e "Search starts in ""\033[0;31m3"; tput sgr0 "seconds"
sleep 1
echo -e "Remaining: ""\033[0;33m2"; tput sgr0
sleep 1
echo -e "Remaining: ""\033[0;32m1\n"; tput sgr0
sleep 1

find $location -name "*.$type" >$type-list.txt

This creates a new line after every second but i want it to stay one line and just change the number behind "Remaining". How could i do that?

The output looks like this.

You have entered

Directory: /home
File type: mp3

Preparing search.....
Search starts in 3
Remaining: 2
Remaining: 1

theurge
10th May 2004, 03:49 AM
Ok, I found the solution. I had the idea, just not the right syntax with echo:



#!/bin/bash
#
# Demonstrates using backspace in a shell script
#
# theurge14@yahoo.com Sun May 9 21:50:22 CDT 2004

totalLoops=20
counter=1

echo Counting to $totalLoops!
echo
echo -n "Count = "

while (( counter <= totalLoops ))
do
if (( counter<10 )); then
echo -n "$counter "
else
echo -n "$counter"
fi

(( counter++ ))
sleep 1

# Backspace twice over the number, see the man page on echo for more info
echo -n -e \\b\\b
done

echo
echo "Done!"


The above script will do exactly what you are asking to do. Note this line:



echo -n -e \\b\\b


There are your two backspaces. Apparently the octal code only works for printable characters.

Have fun!

Erik-joix
10th May 2004, 04:00 AM
Thank you very much theurge!
This works like a champ.
Here is the part of the script that i altered.

echo -e "You have entered\n"
echo -e "Directory: " "\033[0;34m$location"; tput sgr0
echo -e "File type: " "\033[0;34m$type\n"; tput sgr0
echo "Preparing search....."
sleep 5
echo -e -n "Search starts in:""\033[0;31m 3"; tput sgr0
echo -e -n " seconds"
sleep 1
echo -e -n "\010""\010""\010""\010""\010""\010""\010""\010""\010""\010" "\033[0;33m2"; tput sgr0
echo -e -n " seconds"
sleep 1
echo -e -n "\010""\010""\010""\010""\010""\010""\010""\010""\010""\010" "\033[0;32m1"; tput sgr0
echo -e -n " seconds"
sleep 1

find $location -name "*.$type" >$type-list.txt

theurge
10th May 2004, 04:02 AM
Aha! Looks like the octal does indeed work.

So either "\010" or \\b or "\b" is a backspace.

Cool to know. Thanks!

Erik-joix
10th May 2004, 04:05 AM
Originally posted by theurge
Ok, I found the solution. I had the idea, just not the right syntax with echo:



#!/bin/bash
#
# Demonstrates using backspace in a shell script
#
# theurge14@yahoo.com Sun May 9 21:50:22 CDT 2004

totalLoops=20
counter=1

echo Counting to $totalLoops!
echo
echo -n "Count = "

while (( counter <= totalLoops ))
do
if (( counter<10 )); then
echo -n "$counter "
else
echo -n "$counter"
fi

(( counter++ ))
sleep 1

# Backspace twice over the number, see the man page on echo for more info
echo -n -e \\b\\b
done

echo
echo "Done!"


The above script will do exactly what you are asking to do. Note this line:



echo -n -e \\b\\b


There are your two backspaces. Apparently the octal code only works for printable characters.

Have fun!

When i posted i didn't see this one.
Anyway, i like this way more.
It looks more professional than what i did.
I'll try and implement your solution into my script by some time tomorrow.

Thanks for the help here!

theurge
10th May 2004, 04:07 AM
Yeah it looks like we're playing post-tag here.

Probably replacing all those octals with "\b" would help shorten the script up.

Good luck.

Erik-joix
11th May 2004, 05:26 AM
I implemented your idea into my script and hope that's alright with you.
I also altered it a little bit to fit my "colored text needs" and you're right. It did significantly shorten the script. Thanks again.

echo -e "You have entered\n"
echo -e "Directory: " "\033[0;34m$location"; tput sgr0
echo -e "File type: " "\033[0;34m$type\n"; tput sgr0
echo -e "Preparing search....."
sleep 3
echo -n "Search starts in: "

while (( counter >= liftoff ))
do
if (( counter>3 )); then
echo -e -n "\033[0;31m$counter"; tput sgr0
else
if (( counter>1 )); then
echo -e -n "\033[0;33m$counter"; tput sgr0
else
echo -e -n "\033[0;32m$counter"; tput sgr0
fi
fi
(( counter-- ))
sleep 1
echo -n -e \\b
done

find $location -name "*.$type" >$type-list.txt

theurge
11th May 2004, 06:10 AM
Of course I don't mind, don't mention it. On the contrary I'm rather flattered you used my code. :D

One comment I might add though on what you have now, on the 2nd nested if structure:



if (( counter>1 )); then
echo -e -n "\033[0;33m$counter"; tput sgr0
else
echo -e -n "\033[0;32m$counter"; tput sgr0
fi


Looks like you have two identical echo statements there.

I wrote this in the original code to make up for the extra digit (when I reached ten) when I used the two backspaces.

I see you took the double backspaces out, but I also see you're counting down instead of up. I guess you're not using numbers greater than 10 so it isn't a problem, but then again I haven't run your code myself so I don't really know.

But anyway, I brought this up as you may decide to remove these lines of code as unnecessary and just stick with:



echo -e -n "\033[0;33m$counter"; tput sgr0




Thanks for posting this, I enjoy shell scripting.

Erik-joix
11th May 2004, 01:12 PM
Originally posted by theurge
[B]


if (( counter>1 )); then
echo -e -n "\033[0;33m$counter"; tput sgr0
else
echo -e -n "\033[0;32m$counter"; tput sgr0
fi


Looks like you have two identical echo statements there.


I'm sure they aren't identical. ;)

theurge
11th May 2004, 10:28 PM
Late night. Sorry. Thanks. :D

Erik-joix
12th May 2004, 03:40 AM
And there i thought it can't be done any better.
Wrong.

#!/bin/bash

clear
printf "%s" "The counter is now: "

for (( counter=1; counter<10; counter++ )) ; do
printf "%d\b" "$counter"
sleep 1
done

printf "\n"

exit 0

Picomp314
12th May 2004, 03:41 AM
counter++
word.

theurge
13th May 2004, 12:44 AM
Yes, a for loop works much better in this case.

This is fun though, change one small thing in your code:



#!/bin/bash

clear
printf "%s" "The counter is now: "

for (( counter=1; counter<13; counter++ )) ; do
printf "%d\b" "$counter"
sleep 1
done

printf "\n"

exit 0


And it produces this:



The counter is now: 1112


:D

Erik-joix
13th May 2004, 01:05 AM
:D
yes, this code is not supposed to count into the 2 digit region. :D