【问题标题】:Variable is not printed csh变量不打印 csh
【发布时间】:2016-03-17 21:12:38
【问题描述】:

我的脚本:

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

您能帮我理解这里出了什么问题吗?很抱歉之前没有解释清楚。

【问题讨论】:

  • 我已经格式化了你的代码,请检查我是否做错了什么。请阅读tour。请同时描述预期结果。
  • count=... 行上有一个不匹配的反引号。很难判断它是否存在于原版中,因为反引号用于格式化。
  • 我已经修改了我的问题,请看问题。
  • 我重新编辑了你的问题。您的部分代码(“current=”部分)没有被格式化为代码。请确认我的修改仍然反映了您的意图。

标签: shell csh


【解决方案1】:

您不能将多行内联输入(例如 cat &lt;&lt;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

【讨论】:

  • 在我的代码中,一切正常。变量 g 和 y 未打印。我定义的变量有什么问题吗?
  • 用准确的语法发布你的完整代码。巧合的是,我在 1981 年编写了我的第一个 csh 脚本,所以我可能能够提供帮助 ;-)
  • 克雷格,我已经为你重新发布了我的问题。变量“total”和“y”没有在控制台中打印,出了什么问题?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2018-10-25
    • 2023-03-31
    • 1970-01-01
    • 2020-02-11
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多