You are not logged in.

#1 2010-09-29 04:49:58

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Trying to update clock in Bash script menu

I'm writing a quick launch style Bash program and I would like it to display the date at the top, but I can't seem to figure out how to keep it updated.  The bash 'read' command seems to cause the program to pause and wait, so the date doesn't get updated.  If you can help me figure this out it would be very much appreciated, thanks!

#!/bin/bash
#####
# Xplorer - A launcher to various programs, similar to favorites
# Author - Mega-G33k
#####
RUNNING=1
while [ $RUNNING -eq 1 ]; do
   clear
   echo "Date: $(date)"
   echo "Welcome to \"Xplorer\""
   echo ""
   echo "1 - Midnight Commander"
   echo "2 - Links"
   echo "3 - Mutt"
   echo "4 - Newsbeuter"
   echo "5 - Podbeuter"
   echo "6 - NCMPC"
   echo "7 - Irssi"
   echo "8 - CenterIM"
   echo "9 - Start X"
   echo "10 - Mount first DVD drive (sr0)"
   echo "11 - Unmount first DVD drive (sr0)"
   echo "0 - Exit"
   echo ""
   echo -n "Please enter an option[0-11] "
   read boole
   if [ $boole -eq 1 ]; then
      mc
   elif [ $boole -eq 2 ]; then
      links
   elif [ $boole -eq 3 ]; then
      mutt
   elif [ $boole -eq 4 ]; then
      newsbeuter
   elif [ $boole -eq 5 ]; then
      podbeuter
   elif [ $boole -eq 6 ]; then
      ncmpc
   elif [ $boole -eq 7 ]; then
      irssi
   elif [ $boole -eq 8 ]; then
      centerim
   elif [ $boole -eq 9 ]; then
      if ps -A | grep X &> /dev/null; then
         echo "X has already been started"
         read -n 1 -s
      else
         startx
      fi
   elif [ $boole -eq 10 ]; then
      if [ "$UID" != "0" ]; then
         gksudo mount /dev/sr0 ~/drives/sr0
      fi
   elif [ $boole -eq 11 ]; then
      if [ "$UID" != "0" ]; then
         gksudo umount ~/drives/sr0
      fi

   elif [ $boole -eq 0 ]; then
      exit 0
   else
      echo "Invalid option entered"
      read -n 1 -s
   fi
done

Offline

#2 2010-09-29 08:45:11

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Trying to update clock in Bash script menu

See the last few posts here: https://bbs.archlinux.org/viewtopic.php?id=56646&p=54
If you want it updating separately to the menu, you'll need it in its own loop, backgrounded.

eg

(while true; do
printf "\r$(date "+%T")"
  sleep 1
done) &

May need tweaking to fit in with the rest of the script - see man bash for an explanation.

Also, another tip - check out the case statement as a possible alternative to if...elseif...fi


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#3 2010-09-29 12:25:26

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Trying to update clock in Bash script menu

I think the correct link is https://bbs.archlinux.org/viewtopic.php … 77#p832477 ;P
Wrt to case / select, you really should have a look at them, why do you want to make your scripts half-baked?
https://bbs.archlinux.org/viewtopic.php … 37#p831737

Offline

#4 2010-09-29 13:48:55

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

Here's what I got so far:

#!/bin/bash
#####
# Xplorer - A launcher to various programs, similar to favorites
# Author - Mega-G33k
#####
RUNNING=1
while [ $RUNNING -eq 1 ]; do
   clear
   (while true; do printf "\r$(date)"
      sleep 1
    done) &
   echo "Welcome to \"Xplorer\""
   echo ""
   echo "1 - Midnight Commander"
   echo "2 - Links"
   echo "3 - Mutt"
   echo "4 - Newsbeuter"
   echo "5 - Podbeuter"
   echo "6 - NCMPC"
   echo "7 - Irssi"
   echo "8 - CenterIM"
   echo "9 - Start X"
   echo "10 - Mount first DVD drive (sr0)"
   echo "11 - Unmount first DVD drive (sr0)"
   echo "0 - Exit"
   echo ""
   echo -n "Please enter an option[0-11] "
   read boole
   if [ $boole -eq 1 ]; then
      mc
   elif [ $boole -eq 2 ]; then
      links
   elif [ $boole -eq 3 ]; then
      mutt
   elif [ $boole -eq 4 ]; then
      newsbeuter
   elif [ $boole -eq 5 ]; then
      podbeuter
   elif [ $boole -eq 6 ]; then
      ncmpc
   elif [ $boole -eq 7 ]; then
      irssi
   elif [ $boole -eq 8 ]; then
      centerim
   elif [ $boole -eq 9 ]; then
      if ps -A | grep X &> /dev/null; then
         echo "X has already been started"
         read -n 1 -s
      else
         startx
      fi
   elif [ $boole -eq 10 ]; then
      if [ "$UID" != "0" ]; then
         gksudo mount /dev/sr0 ~/drives/sr0
      fi
   elif [ $boole -eq 11 ]; then
      if [ "$UID" != "0" ]; then
         gksudo umount ~/drives/sr0
      fi

   elif [ $boole -eq 0 ]; then
      exit 0
   else
      echo "Invalid option entered"
      read -n 1 -s
   fi
done

I can't seem to figure out what exactly the carriage return is doing, when I get rid of it the date gets printed over and over in a line.  Also I'm not sure why it appears at the bottom.  And as to my half-baked code, I do appreciate the suggestions and I am planning on switching it over, I just haven't done it yet.

Last edited by Mega-G33k (2010-09-29 13:49:39)

Offline

#5 2010-09-29 13:55:39

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Trying to update clock in Bash script menu

Run

while true; do printf "\r$(date)" && sleep 1; done

with the '\r' and

while true; do printf "$(date)" && sleep 1; done

without.

Offline

#6 2010-09-29 14:01:51

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

So does it over write the prompt at the bottom because the 'read' command pauses the program there?  Also, I always thought carriage return was like pressing the enter key?

Offline

#7 2010-09-29 14:06:19

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Trying to update clock in Bash script menu

Mega-G33k wrote:

So does it over write the prompt at the bottom because the 'read' command pauses the program there?  Also, I always thought carriage return was like pressing the enter key?

Things are more complicated. I think you mean '\n' - a newline or '\n\r'. '\r' alone just moves the cursor to the beginning of the line :-)

Edit:

while true; do printf "\r$(date "+%T")" && sleep 1 && printf "\rfoobarba" && sleep 1; done

:-)

Last edited by karol (2010-09-29 14:14:25)

Offline

#8 2010-09-29 14:22:02

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

I've almost got the clock, It starts at the top where it is supposed to be but then jumps down to the prompt:

#!/bin/bash
#####
# Xplorer - A launcher to various programs, similar to favorites
# Author - Mega-G33k
#####
RUNNING=1
(while true; do
printf "\r$(date "+%T")"
  sleep 1
done) &
echo ""
while [ $RUNNING -eq 1 ]; do
   clear
   echo ""
   echo "Welcome to \"Xplorer\""
   echo ""
   echo "1 - Midnight Commander"
   echo "2 - Links"
   echo "3 - Mutt"
   echo "4 - Newsbeuter"
   echo "5 - Podbeuter"
   echo "6 - NCMPC"
   echo "7 - Irssi"
   echo "8 - CenterIM"
   echo "9 - Start X"
   echo "10 - Mount first DVD drive (sr0)"
   echo "11 - Unmount first DVD drive (sr0)"
   echo "0 - Exit"
   echo ""
   echo -n "Please enter an option[0-11] "
   read boole
   if [ $boole -eq 1 ]; then
      mc
   elif [ $boole -eq 2 ]; then
      links
   elif [ $boole -eq 3 ]; then
      mutt
   elif [ $boole -eq 4 ]; then
      newsbeuter
   elif [ $boole -eq 5 ]; then
      podbeuter
   elif [ $boole -eq 6 ]; then
      ncmpc
   elif [ $boole -eq 7 ]; then
      irssi
   elif [ $boole -eq 8 ]; then
      centerim
   elif [ $boole -eq 9 ]; then
      if ps -A | grep X &> /dev/null; then
         echo "X has already been started"
         read -n 1 -s
      else
         startx
      fi
   elif [ $boole -eq 10 ]; then
      if [ "$UID" != "0" ]; then
         gksudo mount /dev/sr0 ~/drives/sr0
      fi
   elif [ $boole -eq 11 ]; then
      if [ "$UID" != "0" ]; then
         gksudo umount ~/drives/sr0
      fi

   elif [ $boole -eq 0 ]; then
      exit 0
   else
      echo "Invalid option entered"
      read -n 1 -s
   fi
done

Oh, and thanks for that little example, that helped with understanding the carriage return.
EDIT: I'm guessing that it jumps down to the prompt because the cursor is there waiting for a response?

Last edited by Mega-G33k (2010-09-29 14:26:10)

Offline

#9 2010-09-29 14:29:03

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Trying to update clock in Bash script menu

How do you kill this thing?? (except killing the terminal it's running on).

Offline

#10 2010-09-29 14:31:10

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

'0' kills the script, but then you have to type in "killall xplorer" to kill the clock.

Offline

#11 2010-09-29 14:33:17

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Trying to update clock in Bash script menu

You need cursor save, jump and restore: echo -ne "\e7\e[0;0H$(date)\e8"

Offline

#12 2010-09-29 14:35:31

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Trying to update clock in Bash script menu

Procyon wrote:

You need cursor save, jump and restore: echo -ne "\e7\e[0;0H$(date)\e8"

So there are no "shortcuts"? I was trying to avoid the terminal escapes hieroglyphs but I can't seem to find another way.

Offline

#13 2010-09-29 14:37:03

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

Procyon wrote:

You need cursor save, jump and restore: echo -ne "\e7\e[0;0H$(date)\e8"

That was amazing! You just jumped in and saved the day, the clock works.  Now I just have to figure out how to kill it when I start a program and bring it back when they exit.

EDIT: Here's the code so far:

#!/bin/bash
#####
# Xplorer - A launcher to various programs, similar to favorites
# Author - Mega-G33k
#####
RUNNING=1
while [ $RUNNING -eq 1 ]; do
   clear
   (while true; do
   printf "\e7\e[0;0H$(date "+%T")\e8"
      sleep 1
   done) &
   echo ""
   echo "Welcome to \"Xplorer\""
   echo ""
   echo "1 - Midnight Commander"
   echo "2 - Links"
   echo "3 - Mutt"
   echo "4 - Newsbeuter"
   echo "5 - Podbeuter"
   echo "6 - NCMPC"
   echo "7 - Irssi"
   echo "8 - CenterIM"
   echo "9 - Start X"
   echo "10 - Mount first DVD drive (sr0)"
   echo "11 - Unmount first DVD drive (sr0)"
   echo "0 - Exit"
   echo ""
   echo -n "Please enter an option[0-11] "
   read boole
   if [ $boole -eq 1 ]; then
      mc
   elif [ $boole -eq 2 ]; then
      links
   elif [ $boole -eq 3 ]; then
      mutt
   elif [ $boole -eq 4 ]; then
      newsbeuter
   elif [ $boole -eq 5 ]; then
      podbeuter
   elif [ $boole -eq 6 ]; then
      ncmpc
   elif [ $boole -eq 7 ]; then
      irssi
   elif [ $boole -eq 8 ]; then
      centerim
   elif [ $boole -eq 9 ]; then
      if ps -A | grep X &> /dev/null; then
         echo "X has already been started"
         read -n 1 -s
      else
         startx
      fi
   elif [ $boole -eq 10 ]; then
      if [ "$UID" != "0" ]; then
         gksudo mount /dev/sr0 ~/drives/sr0
      fi
   elif [ $boole -eq 11 ]; then
      if [ "$UID" != "0" ]; then
         gksudo umount ~/drives/sr0
      fi

   elif [ $boole -eq 0 ]; then
      exit 0
   else
      echo "Invalid option entered"
      read -n 1 -s
   fi
done

Last edited by Mega-G33k (2010-09-29 14:40:03)

Offline

#14 2010-09-29 14:49:23

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

So, apparently when I use the while loop in my code it starts another "xplorer" process, for example:

28479 pts/0    00:00:00 xplorer
28481 pts/0    00:00:00 xplorer

Would that be how I would kill the clock, because it should start again since it's in the program loop? I would guess that it is the one with the bigger PID #, but how in my script would I be able to compare them "pidof" maybe?

Offline

#15 2010-09-29 14:53:24

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Trying to update clock in Bash script menu

You need to put a

kill %%

Before the exit command. This kills the last job to be run in the background. Obviously it will break if your menu starts anything in the background - in that case use %- instead.
You may need to add one at the end of the script for the when you start an app and don't exit (this done from reading it and I may miss something).


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#16 2010-09-29 14:53:39

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Trying to update clock in Bash script menu

karol wrote:
Procyon wrote:

You need cursor save, jump and restore: echo -ne "\e7\e[0;0H$(date)\e8"

So there are no "shortcuts"? I was trying to avoid the terminal escapes hieroglyphs but I can't seem to find another way.

man terminfo

tput sc
tput rc

tput is slower though.

@mega-g33k: After backgrounding the PID is accessible with $!.
function clock() {
[ $1 = on ] && { echo/tput sc...... & clockpid=$!; }
[ $2 = off ] && { kill $clockpid; }
}

clock on
...
clock off
mc
clock on

Offline

#17 2010-09-29 14:54:01

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Trying to update clock in Bash script menu

I don't know if you already know this site http://mywiki.wooledge.org/BashGuide but reading some guides will help you understand what pipes and loops do and what's not possible i.e. can a child process influence the parent one etc.

Offline

#18 2010-09-29 14:54:11

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Trying to update clock in Bash script menu

Procyon wrote:

You need cursor save, jump and restore: echo -ne "\e7\e[0;0H$(date)\e8"

Ah thanks! I was about to say that it had to be at the prompt, but I've learned something (so the day's not been completely wasted). wink


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#19 2010-09-29 14:55:05

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

Here's what I did:

   if [ $boole -eq 1 ]; then
      kill $(pidof -xs xplorer)
      mc

And I plan on downloading the Bash scripting guides on TLDP, because this simple little script showed me how much I really need to learn.

Last edited by Mega-G33k (2010-09-29 14:55:49)

Offline

#20 2010-09-29 15:00:30

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

Procyon wrote:
karol wrote:
Procyon wrote:

You need cursor save, jump and restore: echo -ne "\e7\e[0;0H$(date)\e8"

So there are no "shortcuts"? I was trying to avoid the terminal escapes hieroglyphs but I can't seem to find another way.

man terminfo

tput sc
tput rc

tput is slower though.

@mega-g33k: After backgrounding the PID is accessible with $!.
function clock() {
[ $1 = on ] && { echo/tput sc...... & clockpid=$!; }
[ $2 = off ] && { kill $clockpid; }
}

clock on
...
clock off
mc
clock on

I really like that idea for a clock function, I think I will use that. Thanks!

Offline

#21 2010-09-29 15:05:41

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Trying to update clock in Bash script menu

karol wrote:

I don't know if you already know this site http://mywiki.wooledge.org/BashGuide but reading some guides will help you understand what pipes and loops do and what's not possible i.e. can a child process influence the parent one etc.

I will be reading more guides now, because as you can see my understanding is very limited.  I think as I read the guides I'll use what I learn in to make this script more full-fledged.  Thanks for all the help!

Last edited by Mega-G33k (2010-09-29 15:11:05)

Offline

Board footer

Powered by FluxBB