title: Connecting to Oracle DB from shell script with 'sysdba'
body: <p>I need to connect my Oracle 11g DB from shell script with 'sysdba' permissions. To do this I have to switch user from 'root' to 'oracle'.
  I've tried the following with no success.</p>
  
  <pre><code>su - oracle -c "&lt;&lt; EOF1
      sqlplus -s "/ as sysdba" &lt;&lt; EOF2
      whenever sqlerror exit sql.sqlcode;
      set echo off 
      set heading off
  
      shutdown immediate
      startup mount
      alter database archivelog;
      alter database open;
  
      exit;
      EOF2
  EOF1"
  </code></pre>
  
  <p>I'm getting the following error. </p>
  
  <blockquote>
    <p>[root@vEMS-23 devel]# ./enableArchiveLogs.sh en ERROR: ORA-01031:
    insufficient privileges</p>
    
    <p>SP2-0306: Invalid option. Usage: CONN[ECT] [logon] [AS
    {SYSDBA|SYSOPER|SYSASM}] where   ::=
    [/][@] [edition=value] | /
    SP2-0306: Invalid option. Usage: CONN[ECT] [logon] [AS
    {SYSDBA|SYSOPER|SYSASM}] where   ::=
    [/][@] [edition=value] | /
    SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting
    SQL*Plus [root@vEMS-23 devel]#</p>
  </blockquote>
  
  <p>As far as I may understand it fails to authenticate as 'sysdba'. It doesn't occurs if I eliminate 'su - oracle -c' and run script as an oracle user initially.</p>
  
  <p>Any clues?</p>
  
  <hr>
  
  <p><strong>I've got an answer:</strong></p>
  
  <p>The answer is pretty simple. I just a bit messed with syntax. The following is the working piece of code:</p>
  
  <pre><code>su - oracle -c 'sqlplus -s / as sysdba &lt;&lt;EOF
     whenever sqlerror exit sql.sqlcode;
     set echo off 
     set heading off
  
     shutdown immediate
     startup mount
     alter database noarchivelog;
     alter database open;
  
     exit;
  EOF
  '
  </code></pre>