LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-20-2013, 12:45 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Angry 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!
 
Old 02-20-2013, 12:58 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 02-20-2013, 01:00 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by druuna View Post
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.
 
Old 02-20-2013, 01:04 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@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.
 
Old 02-20-2013, 01:09 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
$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.
 
1 members found this post helpful.
Old 02-20-2013, 03:04 PM   #6
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,158
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Quote:
Originally Posted by Drigo View Post
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
 
Old 02-20-2013, 08:00 PM   #7
codergeek
Member
 
Registered: Dec 2012
Posts: 52

Rep: Reputation: 7
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

Last edited by codergeek; 02-21-2013 at 11:45 PM.
 
1 members found this post helpful.
Old 02-21-2013, 06:08 AM   #8
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,052

Rep: Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881Reputation: 881
IIRC (and it was quite a time ago, so maybe I don't):

Quote:
Originally Posted by druuna View Post
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
 
Old 02-21-2013, 07:05 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by salasi View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the options "Nosuid" "mode" "size" in /etc/fstab? tuxfiles.org does not help pstein Linux - Newbie 1 11-16-2012 01:58 AM
A question about "echo "$CALLER" | tr -s '[:upper:]' '[:lower:]'" thomas2004ch Linux - Software 5 03-13-2012 03:57 PM
How to set the font-size for "xterm" terminal-emulator (running "bash" shell) ajaygarg Linux - Desktop 1 03-09-2012 01:38 PM
why `tr "\000" "\125" < /dev/zero | dd bs=1K count=1 of=01data` creates wrong size? albert316 Linux - Newbie 11 11-16-2011 11:08 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 03:14 PM


All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration