Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-13-2003, 06:01 AM
|
#1
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Rep:
|
Scripting question: the width of the terminal window
Hello,
I'm using a lot of scripts to automatize stuff, and when I echo something I usually pipe it through fold to get a nice length on the lines - for instance
echo "blah blah" | fold -s -w 80
Now, I'd like to adapt the line length to the actual width of the terminal window - does anyone know how to do this? I mean, so that I could do something like
echo "blah blah" | fold -s -w `command to get the terminal width`
It would look much nicer
Any help is much appreciated!
Stefan
|
|
|
08-13-2003, 06:45 AM
|
#2
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
<edit> ignore what i posted before, bash sets the terminal size in two variables, $LINES and $COLUMNS
Last edited by kev82; 08-13-2003 at 06:55 AM.
|
|
|
08-14-2003, 05:36 AM
|
#3
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Original Poster
Rep:
|
Thanks! But this is odd... I can echo the variables from the command line and get the proper results, but in my script(s) the $LINES and $COLUMNS are empty... This quick and silly test gives nothing for me:
Code:
#!/bin/bash
echo $COLUMNS
echo $LINES
Any ideas?
|
|
|
08-14-2003, 10:23 AM
|
#4
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
im afraid that bash isnt really my area of expertise, however, the following badly written c program will get the terminal width and height, feel free to tidy it up.
Code:
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
int main()
{
struct winsize ws;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
printf("%d\n%d\n", ws.ws_col, ws.ws_row);
return 0;
}
changes that should be made - it could use error checking, some options for output, and replace stdin_fileno with a terminal device from command line.
Last edited by kev82; 08-14-2003 at 10:24 AM.
|
|
|
08-14-2003, 11:14 AM
|
#5
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Original Poster
Rep:
|
Heh, my area of expertise certainly isn't C  But thanks a bunch for your help, I'll see if I can get something out of it.
|
|
|
08-14-2003, 04:04 PM
|
#6
|
Member
Registered: Aug 2003
Distribution: many
Posts: 37
Rep:
|
Check out the 'stty' function...
Code:
$ stty -a
speed 38400 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff -ixany -imaxbel -ignbrk
-brkint -inpck -ignpar -parmrk
oflags: opost onlcr -ocrnl -oxtabs -onocr -onlret
cflags: cread cs8 parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^H; erase2 = ^@; intr = ^C; kill = ^U;
lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q;
status = <undef>; stop = ^S; susp = ^Z; time = 0; werase = ^W;
|
|
|
08-15-2003, 07:25 AM
|
#7
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Original Poster
Rep:
|
Aha! This seems to be something I can use directly in a script - thanks! 
|
|
|
11-06-2003, 12:21 PM
|
#8
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Original Poster
Rep:
|
Hello again!
Well, well, I haven't done very much with this, but since yesterday I have been looking into kev82's " badly written c program"  I have now two very short programs that give me the terminal width and height, respectively. It would be nice if someone could have a look and comment. I had some big problems with finding out how to treat STDIN_FILENO, but it seems to be enough just to put a 0 there. Is this the way to do it? It works, but that doesn't necessarily mean its right, right?
OK, here is the one that gives me the terminal width, i.e. the number of columns:
Code:
#include <sys/ioctl.h>
int main()
{
struct winsize ws;
ioctl(0, TIOCGWINSZ, &ws);
printf("%d\n", ws.ws_col);
return 0;
}
Please be kind - this is my first C program ever  ...or rather it was kev82's program...but I compiled it...hrm...
Cheers
Last edited by Bebo; 11-06-2003 at 12:54 PM.
|
|
|
11-06-2003, 01:20 PM
|
#9
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
I had some big problems with finding out how to treat STDIN_FILENO, but it seems to be enough just to put a 0 there. Is this the way to do it?
|
It actually is (a little) better to use STDIN_FILENO instead of 0. Suppuse you compile it on a system where stdin is not on filedescriptor 0, but say 5, then the system will #define STDIN_FILENO as being 5. Well, in this case of stdin, this is very unlikely to happen, so it's a non-issue. But making a habit of using symbols #defined by the system header files is a good thing in general as there are many examples where it would cause a problem when compiled on another system, or when some library/kernel-headers are update (with possibly changed #defined symbols).
STDIN_FILENO is #defined in unistd.h, so you need to include that header. Also, it's better to #include stdio.h when you use printf() in the .c file. When you compile your program with all warnings turned on you get a warning about this:
gcc -Wall -pedantic -o winsize winsize.c
winsize.c: In function `main':
winsize.c:9: warning: implicit declaration of function `printf'
So, actually there's nothing really wrong with your program, but as I understand you are learning C, and are asking for comment, I figured you might wanted to know.
I would write your program like:
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main()
{
struct winsize ws;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
printf("%d\n", ws.ws_col);
return 0;
}
|
|
|
11-06-2003, 01:22 PM
|
#10
|
Member
Registered: May 2002
Posts: 964
Rep:
|
Try something like this:
Code:
cols=`stty -a | tr -s ';' '\n' | grep "column" | sed 's/columns =//' | sed 's/ //g'`
echo "blah blah" | fold -s -w $cols
|
|
|
11-06-2003, 01:51 PM
|
#11
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Original Poster
Rep:
|
Thanks guys, for your comments!
Aha, so I forgot to include unistd.h - that's why it didn't work! And the stdio.h: I removed it since it seemed to work anyway... Well, I'm learning, I'm learning
My plan was to use the stty solution, but I thought it would be nicer - and a bit more general - if I did something in c instead.
Cheers!
|
|
|
11-06-2003, 02:00 PM
|
#12
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
Originally posted by Bebo
My plan was to use the stty solution, but I thought it would be nicer - and a bit more general - if I did something in c instead.
|
If you're making a general solution, and/or you are scripting anyways, I think it's much better to stick with the stty solution, and use the C-thing for learning C only.
|
|
|
11-06-2003, 05:35 PM
|
#13
|
Member
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553
Original Poster
Rep:
|
Yeah, maybe that would get the scripts more transparent... Well, jim mcnamara's example is nice, but it didn't really work; here's my solution:
Code:
stty -a | tr -s ';' '\n' | grep "column" | sed s/'[^[:digit:]]'//g
|
|
|
11-06-2003, 05:41 PM
|
#14
|
Member
Registered: May 2002
Posts: 964
Rep:
|
The exact output format of stty -a varies - mine was on HPUX. The RH linux box has a memory issue. GLad you got what you needed.
|
|
|
All times are GMT -5. The time now is 11:27 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|