salasi |
02-21-2013 05:08 AM |
IIRC (and it was quite a time ago, so maybe I don't):
Quote:
Originally Posted by druuna
(Post 4896112)
If bash (and maybe other shell as well) is used then you could use the COLUMNS variable, which holds the window width and changes as the window is changed.
|
I don't think that this is correct: it is what I tried when I had a similar problem, and after a while of failing with it, I had to ask for advice. The advice given was that, from a script, COLUMNS probably runs in a sub-shell and reports the width of the subshell, which is zero. At least, this put an end to my messing with quoting everything in every different way that I could think of, because that's what I usually get wrong...
Anyway, here is a code fragment that does something similar: - for me, this is a script called 'sep' because it acts as a separator, either because I like running it manually at the top of, eg, 'locate' results because it enables me to see where the top of the current run is, if you have 'located' multiple times, or between different scripts, to again improve the readabilty of results
- my preferred string to print across the screen ( <=====> )is slightly different from yours, but that doesn't affect the principle; at one time I did think I was going to want to alter those characters for different purposes, but that has never happened
- there is still evidence of some debugging stuff built in, as in it prints the number of columns at the top, because I didn't get comfortable that it would always work (but it does); the intention was to make that an option, so that if invoked with, eg, the '-d' switch the debugging output would be printed, otherwise not.
- equally the intent was to introduce, eg, a '-t' switch, in which case the separator would include the time (in the middle, and only if the window was big enough...you'd probably want to set a 'failed' exit status for the case that it wasn't big enough); the idea was that if you wanted to roughly time the running of some script, or succession of scripts (and it obviously wouldn't be to the nanosecond) you could run this before and after. I see, slightly embarrassingly, some years later, I still haven't done anything about that...
- ...and note also that there are remnants of earlier experiments which haven't been cleaned up, so it is a bit messy
Code:
#!/bin/bash
#
#
#TIMESTAMP=`date +%H:%M:%S`
#echo $TIMESTAMP
#TERMWIDTH=0
#
# just to be sure that we know where we are starting from..
# initialise variables, and set start char, end char and
# seperating char
#
count=0
swidth=0
sc='<'
ec='>'
sepchar='='
#
swidth=$(stty size|cut -f2 -d ' ')
#
# get the second parameter (width), throwing away the first (height)
# with a space as the separator between
#
part=`echo "($swidth / 2) - 4" | bc`
echo "$part"
#TERMWIDTH=echo $COLUMNS
#`${COLUMNS}`=$TERMWIDTH
#
#
#echo $swidth
#
# print a single start character, no carriage return
#
echo -n "$sc"
while [ $swidth -gt 2 ]
do
echo -n "$sepchar"
swidth=$[$swidth-1]
done
#
# and print a single 'terminator' character, with a carriage return
#
echo "$ec"
# if [ $swidth ge 2 ]; then
# echo -n "-"
# swidth=($swidth-1)
# else
# echo "*"
# fi
#
#
#
exit 0
|