LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   echo "#################################" to window size (https://www.linuxquestions.org/questions/linux-newbie-8/echo-to-window-size-4175450982/)

Drigo 02-20-2013 11:45 AM

echo "#################################" to window size
 
Hello,
So I am creating a instructions script but to delineate each part I would like to echo "###########" these characters.

However, since the terminal window size will change, I dont know how to pad it with the exact value.
Is there any way I could do it, according to the window terminal size?
Thanks in advance!

druuna 02-20-2013 11:58 AM

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.

shivaa 02-20-2013 12:00 PM

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.

Are you talking about this?:
Code:

~$ stty columns 130
It will change terminal column size to 130 or whatever value is set.

druuna 02-20-2013 12:04 PM

@shivaa: No, I'm talking about this:
Code:

$ echo $COLUMNS
80

# and after resizing that window:
$ echo $COLUMNS
65

That variable holds the window width, which you could use to set the amount of chars needed.

millgates 02-20-2013 12:09 PM

$COLUMNS may not be available in a script. You can use

Code:

tput cols
instead, though. So, something like

Code:

printf -v foo "%$(tput cols)s" '' && echo ${foo//?/#}
or

Code:

perl -e "print '#'x$(tput cols)"
could work.

Habitual 02-20-2013 02:04 PM

Quote:

Originally Posted by Drigo (Post 4896100)
since the terminal window size will change, I dont know how to pad it with the exact value.

Some terminal emulators will accept a "--geometry" c-li argument, so size isn't really an issue, if you're clever. :)
but it seems that
Code:

perl -e "print '#'x$(tput cols)"
is far less coding.

+1 for that snippet

codergeek 02-20-2013 07:00 PM

Try this code Drigo

Code:

printf "%*s" $(tput cols) | tr ' ' '#'
This will print the # symbol across the terminal without guessing how much padding you need.


Optional: IF you plan to draw many horizontal lines from your script you can create a function called drawline. Whenever you need to draw a line witthin the terminal just call the drawline function name anytime.
Code:

drawline() {
printf "%*s" $(tput cols) | tr ' ' '#'
}

drawline


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


druuna 02-21-2013 06:05 AM

Quote:

Originally Posted by salasi (Post 4896574)
IIRC (and it was quite a time ago, so maybe I don't):
Quote:

Originally Posted by druuna
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...

See post #5 :)


All times are GMT -5. The time now is 12:35 PM.