You are not logged in.

#1 2012-04-23 22:27:38

unilx
Member
From: Fredericton,Canada
Registered: 2009-10-01
Posts: 183

(SOLVED) bash script header problem

When i run this piece of code from the command line it works properly, but when i try and run it from a script it doesn't.
It's supposed to be the beginning of a header

printf "%*s\n" "$((COLUMNS))" " " | tr " " "="  ; printf "%*s\n" $((COLUMNS/2)) " Database "  ; printf "%*s\n" $((COLUMNS)) " " | tr " " "="

Any help would be appreciated, thanks

Last edited by unilx (2012-04-23 23:58:53)

Offline

#2 2012-04-23 22:51:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: (SOLVED) bash script header problem

COLUMNS is not defined in a normal script.

Now to find a solution ...

Edit: does "sourcing" the script work from an interactive session?
e.g.

~$ ./test.sh
./test.sh: line 2: 0: command not found
$ . test.sh
bash: 89: command not found

where test.sh has only

#!/bin/bash
$((COLUMNS))

Edit2:
try

WIDTH=`stty size | cut -d" " -f2`

then use $WIDTH instead of $((COLUMNS))

Last edited by Trilby (2012-04-23 23:16:22)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-04-23 23:21:40

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: (SOLVED) bash script header problem

stty will fail when stdin is not a terminal. For 99% of cases, 'tput cols' is a better idea. The 1% is when the terminfo doesn't exist for your terminal.

$ stty size < /dev/null
stty: standard input: Inappropriate ioctl for device
$ tput cols
199

Related: $COLUMNS is the proper way to expand a variable. $((COLUMNS)) will always results in a 0 or 1, as it's evaluating the variable to be true or false.

Offline

#4 2012-04-23 23:49:34

unilx
Member
From: Fredericton,Canada
Registered: 2009-10-01
Posts: 183

Re: (SOLVED) bash script header problem

Thanks, that fixed the problem.

Offline

Board footer

Powered by FluxBB