Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

my first post. Just created a teeny little script to count the seconds until I get auto-logged off. I already solved my issue with my ssh client settings, but I'd still like any help making the bash script nicer to read, or just tips in general. Thanks in advance.

#!/bin/bash

count=0
while ( [ : ] )
do
  count=$(($count+1))
  n=${n:-0}
  for i in `seq 0 $n`; do echo -en '\b'; done
  n="${#count}"
  echo -n $count
  sleep 1
done
share|improve this question

2 Answers 2

up vote 4 down vote accepted
count=0
while true; do
  printf "\r%d" $((++count))
  sleep 1
done

Your while condition is doing a lot of work just to return true: you launch a subshell, and evaluate a test giving a 1-character string (which will always return true). Make it simpler and more readable by just executing the true program.

Note that variable names inside an arithmetic expression do not require the leading dollar sign in most cases. That's quietly documented here:

"Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax."

printf is more portable than echo, if that's a concern for you. I also find it makes your intentions more obvious.

Instead of backing up the right number of characters, just carriage return to the first column and overwrite the previous contents.

share|improve this answer
    
No freaking way. I did previously know about /bin/true, I just didn't think of it. However, I never knew bash had a ++ syntax, nor did I realize I can use variable names without the dollar sign. THANKS! –  Bryan Harris Jan 11 '13 at 18:12

If the answer is what you care for,

in your .bash_profile :

touch "~/.loggued_in"

in your .bash_logout:

date -r ~/.loggued_in ; date

and check if there is a timeout setup:

{ set ; env ; } | egrep 'TM|TIME|OUT' | sort | uniq

and lastly, if connecting via something like Putty (or other), they can be setup to have a "keepalive" sent regularly (usually a "nop"). In Putty, "Settings - Connection - seconds between keepalives" (0 by default : set it to, say, 50 if you get auto loggued off after 1mn)

ok, about the code itself : very nice for displaying the time in an interresting manner (sort of like a progress bar). If you prefer it in one place (and no need for an extra $n) :

while ( [ : ] ); do count=$(($count+1)); echo -en '\b\b\b\b'; printf "%4s" $count; sleep 1; done
share|improve this answer
    
sorry for the 3 first parts: i got used to questions on so ... I believe my answer is not at all what you need here, ie code review - wise... But your script is quite fine already. Let's see what others propose as interresting modifications –  Olivier Dulac Jan 11 '13 at 14:25
    
Thanks guys for your comments. I tried to give you guys a point but it's saying I have to login (which I don't want to do right now). It let me do the check box though. Oh and yes I had already determined how to prevent ssh disconnects. It did take me a long time, I should have asked on here. :-) –  Bryan Harris Jan 11 '13 at 14:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.