Scripting question: the width of the terminal window
ProgrammingThis 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.
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.
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`
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:
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.
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:
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.