Mailinglist Archive: opensuse (1696 mails)

< Previous Next >
Re: [opensuse] BASH: has $COLUMNS gone nuts?
  • From: David Haller <opensuse@xxxxxxxxxx>
  • Date: Tue, 28 Jul 2009 05:44:04 +0200
  • Message-id: <20090728034404.GA12850@xxxxxxxxxxxxxxxxxx>
Hello,

On Thu, 23 Jul 2009, Randall R Schulz wrote:
[snippetry]

A somewhat inelegant approach, for my taste, and if I may say so ;)

==== (Note: my sed has no '-r') ====
#!/bin/bash --norc
ttyinfo="$(stty -a)"
for arg; do
case "$arg" in
-r|--r*)
echo "$ttyinfo" | sed -n '/rows/ s/.*rows \+\([^;]\+\);.*/\1/p';
continue
;;
-c|--c*)
echo "$ttyinfo" | \
sed -n '/columns/ s/.*columns \+\([^;]\+\);.*/\1/p';
continue
;;
esac
done
if test "$#" -eq 0; then
echo "$ttyinfo" | \
sed -n '/rows.*columns/s/.*\(rows[^;]\+;[^;]\+\);.*/\1/p'
fi
====

That way, you can even use '-r' and '-c' together without getting
logically inconsistent, if you reinterpret -r / -c as 'give me rows'
and 'give me columns' and still call stty only once ;)

Of course, you could also use awk similar to below instead of sed.

With a different output-format, done as a function for easy copy and
paste:

====
#!/bin/bash --norc
termsize() {
stty -a | awk '
BEGIN {
RS=";";
FS=" ";
}
$1 ~ /rows|columns/ {
### or: $1 == "rows" || $1 == "columns" # [*]
sub("rows", "LINES", $1);
printf("%s=%s\n", toupper($1), $2);
}
'
}
termsize
# eval "$(termsize)"

### [*] no idea what is faster in awk, the regex-match or the
### two string comparisons.
====

Its output can be eval'd (as commented), and an 'export ' could be
added to the printf. Otherwise you could just comment the sub() line.

One could also make the exporting into an option to the function.

BTW: LINES and COLUMNS are not exported here on an ex-SUSE 6.2.

(0)$ grep COLUMN /proc/$$/environ
(1)$ env | grep COLUMN
(1)$ set | grep COLUMN
COLUMNS=81
(0)$

HTH,
-dnh

--
If the bit is set to 1, the packet has evil intent. Secure systems
SHOULD try to defend themselves against such packets. Insecure systems
MAY chose to crash, be penetrated, etc. -- RfC 3514
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >