简体   繁体   English

Shell脚本在ksh中给出错误

[英]Shell script giving errors in ksh

I have a script which runs fine when executed in Bash shell (with Red Hat Linux), however this same script which fails on a Solaris 10 (DB) server where ksh is being used to execute this script. 我有一个脚本,该脚本在Bash shell(与Red Hat Linux)中执行时运行良好,但是,该脚本在使用ksh执行该脚本的Solaris 10(DB)服务器上失败。 This script basically reads line by line from a file and executes a stored proc (in Oracle). 该脚本基本上从文件中逐行读取并执行存储的proc(在Oracle中)。 Below is my script : 下面是我的脚本:

#/bin/sh

for i in $(cat subscriber.txt); do

        SUBSCRIBER_ID="'$i'"
        sqlplus -s myuser/myuser  <<EOF
        execute delete_learnings($SUBSCRIBER_ID);
        commit;
        EXIT    
EOF
done

The error I get is : 我得到的错误是:

./removeLearnings.sh: syntax error at line 3: `$' unexpected

Any idea what might be going wrong? 任何想法可能出了什么问题吗? Should I change the script to have the ksh ? 我应该将脚本更改为具有ksh吗? I am not able to debug on this machine since it's a customer environment (which I don't have access to). 由于这是客户环境(我无权访问),因此我无法在这台机器上进行调试。

The issue is the $(...) construction which is POSIX compliant but unsupported by the legacy Bourne shell which /bin/sh is on Solaris 10 and older. 问题是$(...)构造与POSIX兼容,但旧的Bourne shell不支持它, /bin/sh在Solaris 10和更早版本上。

You can either replace your shebang to call the Solaris POSIX compliant shell: 您可以替换shebang来调用与Solaris POSIX兼容的shell:

#!/usr/xpg4/bin/sh

or use this legacy syntax (less recommended): 或使用以下旧式语法(不建议使用):

for i in `cat subscriber.txt`; do

you are trying to execute a sh( bourne shell) script on ksh (Korn shell). 您正在尝试在ksh(Korn Shell)上执行sh(bourne shell)脚本。 Try changing the shebang (#!/bin/bash) to (#!/bin/ksh) 尝试将shebang(#!/ bin / bash)更改为(#!/ bin / ksh)

Looping over text files with for is a bad idea, anyway. 无论如何,使用for循环文本文件是一个坏主意。 See http://mywiki.wooledge.org/BashFAQ/001- the recommended syntax is more portable, too: 请参阅http://mywiki.wooledge.org/BashFAQ/001-推荐的语法也更便于移植:

while read stuff; do
    : things with "$stuff"
done <subscriber.txt

You would normally use read -r but I don't know if that's available on Solaris. 通常,您将使用read -r但是我不知道Solaris上是否可用。

However, very often, a shell loop is altogether the wrong approach. 但是,shell循环通常是完全错误的方法。 A single SQL invocation is a lot better and more robust: 单个SQL调用会更好,更强大:

( sed 's/.*/execute delete_learnings(&);/'
  printf "commit;\nEXIT\n" ) |
sqlplus -s myuser/myuser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM