变量未打印csh

时间:2016-03-17 21:12:39

标签: shell csh

我的剧本:

current = `sqlplus -s $Schemaname/$password@$SID <<END
       set pagesize 0 feedback off verify off heading off echo off;
       select Count (*) from usr where activeuser='+';
       exit;
       END`

     set total = 1981

      echo $total
      echo $current

      set y = [ `expr 1981 - $current`] 

      echo $y

控制台输出:

1935

预期产出:

1981
1935
46

你能帮我理解这里出了什么问题吗?对于之前没有解释清楚的道歉。

2 个答案:

答案 0 :(得分:0)

您无法将多行内联输入(例如cat <<EOF)与反引号语法相结合。反引号必须在同一行。

你能做的最好的事情是[原创]:

#!/bin/tcsh -f

cat > /tmp/data <<END
set pagesize 0 feedback off verify off heading off echo off;
select Count (*) from user where activeuser='+';
exit;
END

set count="`sqlplus -s $schemaid$pwd@SID < /tmp/data`"

rm -f /tmp/data

set g = 44

echo $g
echo $count

set y = `expr $g - $count`

echo $y

<强>更新

这在csh

中无效
#!/bin/tcsh -f
# badsyn

set val=`head -1 << EOF
abc
def
EOF`

echo "val is '$val'"

它产生语法错误:

Unmatched `.

这是您的最新代码,其中包含一些注释:

#!/bin/tcsh -f

# dummy variables
set Schemaname=foo
set password=bar
set SID=10

# BUG #1 -- csh needs a set command (i.e. "set current=" is correct but
# "current=" is a syntax error

# BUG #2 -- you want to capture the sqlplus output so you need to use
# backticks/virgules ("`") but they must be on the same line
current= sqlplus -s $Schemaname/$password@$SID <<END
           set pagesize 0 feedback off verify off heading off echo off;
           select Count (*) from usr where activeuser='+';
           exit;
           END

     set total = 1981

      echo $total
      echo $current

# BUG #3 -- the brackets aren't valid in csh here
      set y = [ `expr 1981 - $current`]

      echo $y

使用两个脚本可以在不使用临时文件的情况下完成此操作。这是第一个:

#!/bin/tcsh -f
# fix2a

###set echo verbose

# dummy variables
set Schemaname=foo
set password=bar
set SID=10

set current="`./fix2b -s $Schemaname/$password@$SID`"

     set total = 1981

      echo $total
      echo $current

# BUG #3 -- the brackets aren't valid in csh here
      set y = `expr 1981 - $current`

      echo $y

这是第二个:

#!/bin/tcsh -f
# fix2b

# dummy variables since I don't have sqlplus _or_ the database
alias sqlplus 'echo 1935 ; true'

sqlplus $argv <<END
           set pagesize 0 feedback off verify off heading off echo off;
           select Count (*) from usr where activeuser='+';
           exit;
        END

这是我得到的输出:

1981
1935
46

答案 1 :(得分:0)

在命令行上传递密码存在安全风险。最好用/ nolog启动SQL * Plus,然后使用CONNECT。