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. This script basically reads line by line from a file and executes a stored proc (in 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? I am not able to debug on this machine since it's a customer environment (which I don't have access to).

Answers


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.

You can either replace your shebang to call the Solaris POSIX compliant 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). Try changing the shebang (#!/bin/bash) to (#!/bin/ksh)


Looping over text files with for is a bad idea, anyway. See http://mywiki.wooledge.org/BashFAQ/001- the recommended syntax is more portable, too:

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.

However, very often, a shell loop is altogether the wrong approach. A single SQL invocation is a lot better and more robust:

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

Need Your Help

How to change the header background color of a QTableView

c++ qt qtableview qabstracttablemodel

The following is what I've currently tried. The header text changes color correctly but the background will not change from the default.

Iphone Deployment Target with ASIHTTPRequest

iphone objective-c asihttprequest

I'm having an issue trying to use ASIHTTPRequest libraries in my iPhone application.