2012-07-14 51 views
1

有一个在Shell脚本一个工具,它显示的菜单中心,并做运行脚本,而一些task.I得到这个错误:算术语法错误

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

此代码是从Solaris ENV复制其中回声$柱(为什么?)是空白的价值。当复制到红帽信封节目回声$的列142(因为它应该在全屏幕)。这里是代码的培训相关部分:

#!/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 
} 

由于COLUMNS不为空,我知道它不会进入if条件。同样,COLUMNS可能会因s在window.How的IZE做我做

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

工作?

+0

使用COLUMNS = $(tput cols)而不是awk构造。 – 2012-07-14 19:38:24

回答

1

echo和您的错误信息似乎拥有线索:

COLUMNS : columns 142 

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

哪里 “列”(全部小写)从何而来?不知何故,你的变量正在添加一些文本。

这不会发生在ksh中,我看不到它在脚本中发生了什么。

+0

非常感谢!应该看到... – subodh1989 2012-07-14 07:14:38