1

There is a tool in Shell Script which Displays the menu in center and do some task.I get this error while running the script :

\007
In center function
COLUMNS : columns 142
2 : 13
/ShellsAndSQLs/task_menu: line 80:  (columns 142 - 13) / 2 : arithmetic syntax error

This code was copied from solaris env where echo $COLUMNS(why??) show blank value.This when copied to Redhat Env shows echo $COLUMNS as 142(as it should in full screen).Here is the relevent section of the code :

#!/bin/ksh 

trap "tput sgr0; tput clear;" 1 2 3 4 5 6 7 8 10 11 12 13 14 15

if [ $# -ne 2 ]
then
    echo "Wrong parameters specified" >&2
    exit 1
fi

if [ "$COLUMNS" = "" ]
then
    echo "column in if"
    stty -a |
    awk -F \; '/columns/ { for (i = 1; i <= NF; i++) {
                               if ($i ~ "columns") {
                                   sub(" *columns *= *", "", $i);
                                   print $i;
                                   exit
                               }
                         }
             }' |
    read COLUMNS
fi

center()
{

 echo "In center function"
 echo "COLUMNS : $COLUMNS"
 echo "2 : ${#2}"
    n=$(( ($COLUMNS - ${#2}) / 2 ))
   #n=`expr $COLUMNS - ${#2} / 2`;
    echo "n is $n";
   echo "COLUMNS in center fun : $COLUMNS" ;
    tput cup $1 $n
    echo $_N $2 $_C
}

I know that it is not going in the if condition as the COLUMNS is not blank.Also COLUMNS may vary depending on the size of the window.How do i make the

n=$(( ($COLUMNS - ${#2}) / 2 ))

Work?

1
  • Use COLUMNS=$( tput cols ) instead of the awk construct. Commented Jul 14, 2012 at 19:38

1 Answer 1

1

Your echo and your error message seem to hold the clue:

COLUMNS : columns 142

and

/ShellsAndSQLs/task_menu: line 80:  (columns 142 - 13) / 2 : arithmetic syntax error

Where does "columns" (all lower-case) come from? Somehow your variable is getting some text added to it.

That doesn't happen for me in ksh and I don't see where it's happening in your script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.