You are not logged in.

#1 2012-01-13 07:51:52

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

[SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

I've googled for a way to check the status of my vpny-connection. I found this one-liner here http://www.rajatswarup.com/blog/2011/05 … on-status/:

 while [ `ps aux |grep vpnc|grep -v grep|awk '{print $2}'` ] ; do printf "Connected\r"; done 

It shows "connected" as long as the vpnc connection is established and ends as soon as the connection's over. Just what I was looking for.

The problem is that it is quite long, so I tried making an alias for it in .bashrc or a short bash script. The problem in case of the .bashrc is that it starts a new line every time it outputs "connected", i.e.

$ alias vpnc-status="while [ `ps aux |grep vpnc|grep -v grep|awk '{print $2}'` ] ; do printf "Connected\r"; done"
$ vpnc-status
connected
connected
connected
connected
connected
connected
connected

When I put the line in a short shell script:

#!/bin/bash
# -> I've also tried #!/bin/sh (just a shot in the dar), but that didn't help either
while [ `ps aux |grep vpnc|grep -v grep|awk '{print $2}'` ] ; do printf "Connected\r"; done

I get this error when I execute it:

/home/<user>/Programming/scripts/vpn-check: line 3: [: 10504: unary operator expected 

(the directory is where I put my scripts)

How can I turn this one-liner into a easy-to-write command (script or alias)?

Last edited by cryptkeeper (2012-01-13 12:00:17)

Offline

#2 2012-01-13 07:57:35

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

Re: [SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

'pgrep vpnc' can save you some typing.

Offline

#3 2012-01-13 08:21:10

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

karol wrote:

'pgrep vpnc' can save you some typing.

Thanks, that's easier to type. Well, this returns a number when I have a vpnc connection and nothing when there's no connection. I tried to put this into a short shell script, but something strange happens. I don't have a vpnc connection established right now, and when I write `pgrep vpnc` directly on command line, I get no output. But when I put the same command (and nothing else!) into a shell script:

#!/bin/bash
pgrep vpnc

and execute it I get a number as output!

Now why could THAT be? I'm quite confused...

Offline

#4 2012-01-13 08:24:17

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

Re: [SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

How about reading 'man pgrep'?

DESCRIPTION
       pgrep looks through the currently  running  processes  and  lists  the
       process  IDs  which matches the selection criteria to stdout.


I suggest using a function instead of an alias

# Pick some name for the function
foobarbaz () {
# hides the cursor
  tput civis
# I was testing it with htop, check if it works with your app
  while [ "$(pgrep htop)" ]; do
    printf "\rConnected"
# I set it to check every second
    sleep 1
# Comment out the next two lines if you don't like the blinking
    printf "\r         "
    sleep 1
  done
  printf "\rDiconnected\n"
# shows the cursor
  tput cnorm
} 

One problem is that if you exit this function via ^C, it won't show your cursor, you have to run 'tput cnorm' by hand.



Edit: It seems if you put this function in your .bashrc you can't exit it with ^C ;P you have to kill the app you're monitoring (e.g. htop).

Last edited by karol (2012-01-13 08:33:02)

Offline

#5 2012-01-13 08:39:27

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

karol wrote:

How about reading 'man pgrep'?

DESCRIPTION
       pgrep looks through the currently  running  processes  and  lists  the
       process  IDs  which matches the selection criteria to stdout.

Yeah I should have done that... Wouldn't have anything though, I still don't see why it doesn't work as it should if put into a shell script.

I suggest using a function instead of an alias

# Pick some name for the function
foobarbaz () {
# hides the cursor
  tput civis
# I was testing it with htop, check if it works with your app
  while [ "$(pgrep htop)" ]; do
    printf "\rConnected"
# I set it to check every second
    sleep 1
# Comment out the next two lines if you don't like the blinking
    printf "\r         "
    sleep 1
  done
  printf "\rDiconnected\n"
# shows the cursor
  tput cnorm
} 

One problem is that if you exit this function via ^C, it won't show your cursor, you have to run 'tput cnorm' by hand.

Edit: It seems if you put this function in your .bashrc you can't exit it with ^C ;P you have to kill the app you're monitoring (e.g. htop).

Thanks a lot! I simplified it a bit and put this in my .bashrc:

vpnc-check () {
  while [ "$(pgrep vpnc)" ]; do
    printf "\rConnected "
    sleep 1
  done
  printf "\rDiconnected\n"
} 

It works like a charm, and I can also abort it with ^C.

But I first tried to put the code in the function body (from while to printf) into a shell script. I have no problems with printf, that works, but again, pgrep vpnc returns a process id even if there's no vpnc connection and a direct pgrep vpnc returns nothing (as it should). Why doesn't this work in a shell script?

Offline

#6 2012-01-13 08:42:43

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

Re: [SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

Try running 'prep -x vpnc' - maybe there's some other process with vpnc in the name.

Offline

#7 2012-01-13 11:59:50

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] echo -e "...\r" and printf "...\r" don't work in shell script

Strange, I've tried it again within a shell script, both with and without the -x flag, but now both works...

Whatever. The problem is solved anyway with the function in the .bashrc.

Thanks a lot for your help!

Offline

Board footer

Powered by FluxBB