You are not logged in.
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
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
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
Thanks, that fixed the problem.
Offline