Script giving error-Unable to identfiy

#!/bin/sh
#!/bin/prel
#set -x
while getopts ":n:t:" opt; do
    case "$opt" in
        n)
            host=$OPTARG
        ;;
        t)
            targ=$OPTARG
        ;;
        *)
            break
        ;;
    esac
done
if ping -c 2 $host >/dev/null 2>&1
then
    echo Host OK
else
    echo Bad Host $host
fi
case "$targ" in
    pref|avail) echo targ OK;;
    *)    echo Bad argument to -t;;
esac
echo "host = $host targ = $targ"
calc_pref_avail(y to check the dba_jobs job last execution time till completion (time taken by job in last run))
{
declare -a lines
  declare -a pref
  declare -a avail

  IFS=$'\n';
  lines=`srvctl config service -d ${1} -s ${2}|egrep '^(Preferred|Available) instances:'`;
  for (i=0;i<${#lines[@]};i++)
  do
    line=${lines[$i]};
    IFS=$'\n';
    if [ "${line:0:21}" = 'Preferred instances: ' ]; then

Below is the error

Host OK
targ OK
host = dev-oragrid-ux01 targ = pref
test4.sh[28]: syntax error at line 36 : `(' unexpected

Please assist

Best regards,
Vishal

Try changing your for loop to this:

for ((i=0;i<${#lines[@]};i++))

A numeric for loop uses double brackets

for ((i=0;i<${#lines[@]};i++))

It's still giving the same error:

#!/bin/sh
#!/bin/prel
#set -x
while getopts ":n:t:" opt; do
    case "$opt" in
        n)
            host=$OPTARG
        ;;
        t)
            targ=$OPTARG
        ;;
        *)
            break
        ;;
    esac
done
if ping -c 2 $host >/dev/null 2>&1
then
    echo Host OK
else
    echo Bad Host $host
fi
case "$targ" in
    pref|avail) echo targ OK;;
    *)    echo Bad argument to -t;;
esac
echo "host = $host targ = $targ"
calc_pref_avail()
{
declare -a lines
  declare -a pref
  declare -a avail

  IFS=$'\n';
  lines=`srvctl config service -d ${1} -s ${2}|egrep '^(Preferred|Available) instances:'`;
  for ((i=0;i<${#lines[@]};i++))
  do
    line=${lines[$i]};
    IFS=$'\n';

Below is the error:

Host OK
targ OK
host = dev-oragrid-ux01 targ = pref
test4.sh[28]: syntax error at line 36 : `(' unexpected

Best regards,
Vishal

Hello Vishal,

Not sure if you have posted the complete script, buyt seems the for loop should be closed by done .

try the same and let us know.

Thanks,
R. Singh

Below is the complete script

#!/bin/sh
#!/bin/prel
#set -x
while getopts ":n:t:" opt; do
    case "$opt" in
        n)
            host=$OPTARG
        ;;
        t)
            targ=$OPTARG
        ;;
        *)
            break
        ;;
    esac
done
if ping -c 2 $host >/dev/null 2>&1
then
    echo Host OK
else
    echo Bad Host $host
fi
case "$targ" in
    pref|avail) echo targ OK;;
    *)    echo Bad argument to -t;;
esac
echo "host = $host targ = $targ"
calc_pref_avail()
{
declare -a lines
  declare -a pref
  declare -a avail

  IFS=$'\n';
  lines=`srvctl config service -d ${1} -s ${2}|egrep '^(Preferred|Available) instances:'`;
  for ((i=0;i<${#lines[@]};i++))
  do
    line=${lines[$i]};
    IFS=$'\n';
    if [ "${line:0:21}" = 'Preferred instances: ' ]; then
      pref=($(echo "${line:21}"|sed 's/,/\n/g'));
    else
      avail=($(echo "${line:21}"|sed 's/,/\n/g'));
    fi
  done
  # array with preferred instances
  for ((m=0;m<${#pref[@]};m++))
  do
    echo " ${pref[$m]} "
  done
  # array with available instances
  for ((m=0;m<${#avail[@]};m++))
  do
     echo " ${avail[$m]} "
  done
}
export CRS_HOME=/devoragridcn_01/Grid_11203
for database in `$CRS_HOME/bin/crsctl status res -n $host| grep -E "ora.*\.db" | awk -F"." '{print $(NF-1)}'`
  do
export ORACLE_HOME=`$CRS_HOME/bin/srvctl config database -v|grep -i $database | awk '{print $2}'`
for service in `$ORACLE_HOME/bin/srvctl status service -d $database | awk '{print $2}'`
do
c="/product/10"
case $ORACLE_HOME in
  */product/10*)
Instance_pref=`$ORACLE_HOME/bin/srvctl config service -s $service -d $database | sed 's/\(.*: \)\(.*\)\( AVAIL:.*\)/\2/1;1q'`
Instance_avail=`$ORACLE_HOME/bin/srvctl config service -s $service -d $database | sed 's/\(.*AVAIL: \)\(.*\)/\2/g;1q'`;;
*)
calc_pref_avail $database $service| { read Pref Avail;};;
echo $Pref "For 11g"
echo $Instance_pref "For 10g";
echo $Instance_avail "For 10g"
echo $Avail "For 10g"
$pref_node=`$ORACLE_HOME/bin/srvctl status database -d $database | sed 's/\(.*Instance\)\(.*\)\( is running on node $node.*\)/\2/1;1q'`
#if $pref_node == $Instance_pref then
#$ORACLE_HOME/bin/srvctl relocate service -s $service -d $database -i #$Instance_pref -t $Instance_avail
 done
done

Best regards,
Vishal

BTW: the array lines will always hold one element, you have to use the array initializer instead:

$ declare -a lines
$ lines=`cat /etc/profile`
$ echo ${#lines[@]}
1
$ lines=( `cat /etc/profile` )
$ echo ${#lines[@]}
266

This will also not solve the syntax error at line 36 : `(' unexpected error. Please post the entire script.

I have posted the entire script before your post.Please review.

Thanks!

Best regards,
Vishal

I cannot reproduce the (-error in your script, but you have several other problems:

Line 69 should not end with ;;

Line 79 should not begin with a $

After line 79, esac is required to end the case statement.

---------- Post updated at 15:12 ---------- Previous update was at 15:11 ----------

Or line 69 is ok, then esac is needed after line 69.

Hello Vishal,

Not sure but seems you may need to close the case with
esac . Please try to do the same.

Thanks,
R. Singh

I have done the corrections -it's still giving error .Please see the update code

#!/bin/sh
#!/bin/prel
#set -x
while getopts ":n:t:" opt; do
    case "$opt" in
        n)
            host=$OPTARG
        ;;
        t)
            targ=$OPTARG
        ;;
        *)
            break
        ;;
    esac
done
if ping -c 2 $host >/dev/null 2>&1
then
    echo Host OK
else
    echo Bad Host $host
fi
case "$targ" in
    pref|avail) echo targ OK;;
    *)    echo Bad argument to -t;;
esac
echo "host = $host targ = $targ"
calc_pref_avail()
{
declare -a lines
  declare -a pref
  declare -a avail

  IFS=$'\n';
  lines=`srvctl config service -d ${1} -s ${2}|egrep '^(Preferred|Available) instances:'`;
  for ((i=0;i<${#lines[@]};i++))
  do
    line=${lines[$i]};
    IFS=$'\n';
    if [ "${line:0:21}" = 'Preferred instances: ' ]; then
      pref=($(echo "${line:21}"|sed 's/,/\n/g'));
    else
      avail=($(echo "${line:21}"|sed 's/,/\n/g'));
    fi
  done
  # array with preferred instances
  for ((m=0;m<${#pref[@]};m++))
  do
    echo " ${pref[$m]} "
  done
  # array with available instances
  for ((m=0;m<${#avail[@]};m++))
  do
     echo " ${avail[$m]} "
  done
}
export CRS_HOME=/devoragridcn_01/Grid_11203
for database in `$CRS_HOME/bin/crsctl status res -n $host| grep -E "ora.*\.db" | awk -F"." '{print $(NF-1)}'`
  do
export ORACLE_HOME=`$CRS_HOME/bin/srvctl config database -v|grep -i $database | awk '{print $2}'`
for service in `$ORACLE_HOME/bin/srvctl status service -d $database | awk '{print $2}'`
do
c="/product/10"
case $ORACLE_HOME in
  */product/10*)
Instance_pref=`$ORACLE_HOME/bin/srvctl config service -s $service -d $database | sed 's/\(.*: \)\(.*\)\( AVAIL:.*\)/\2/1;1q'`
Instance_avail=`$ORACLE_HOME/bin/srvctl config service -s $service -d $database | sed 's/\(.*AVAIL: \)\(.*\)/\2/g;1q'`;;
*)
calc_pref_avail $database $service| { read Pref Avail;};;
esac
echo $Pref "For 11g"
echo $Instance_pref "For 10g";
echo $Instance_avail "For 10g"
echo $Avail "For 10g"
pref_node=`$ORACLE_HOME/bin/srvctl status database -d $database | sed 's/\(.*Instance\)\(.*\)\( is running on node $node.*\)/\2/1;1q'`
#if $pref_node == $Instance_pref then
#$ORACLE_HOME/bin/srvctl relocate service -s $service -d $database -i $Instance_pref -t $Instance_avail
 done
done
 sh test4.sh -n dev-oragrid-ux01 -t pref
Host OK
targ OK
host = dev-oragrid-ux01 targ = pref
test4.sh[28]: syntax error at line 36 : `(' unexpected

Best regards,
Vishal

Hmm, which shell is /bin/sh ??

Yes I used below code at the top

#!/bin/sh 

Should I try with bash?

 #!/bin/bash

Best regards,
Vishal

That could help. It seems, /bin/sh does not understand (( .

It's still not working with bash

Best regards,
Vishal

Do you use sh script.sh ... or just ./script.sh to run the script? If the first, change it to bash script.sh ...

1 Like

Excellent catch!

I was using sh test4.sh

When I used ./test4.sh it ran fine

Could you please help me understand the logic?

Thank you!

Best regards,
Vishal

When you use ./test4.sh , then the script is "executed" by the kernel. Now the script is just text and not a executable binary. So the kernel needs to execute an "interpreter" instead. If the file starts with #! , then the kernel recognizes this and executes the program defined between #! and the first newline character. The name of the script is passed as commandline argument to the interpreter.

When you type sh test4.sh , then you directly call the program sh and pass the scriptname on the commandline. The first line of the script is just a comment in this case and has no special meaning.

You can find more information here: Shebang (Unix) - Wikipedia, the free encyclopedia

1 Like