expect: Simulando comandos interativos em Shell Script

O programa expect pode ser utilizado para simular comandos interativos em um Shell Script.

Por exemplo, ao abrir o SQL*Plus, e “ver” a string SQL, “digitar” a string SELECT FLASHBACK_ON FROM V\$DATABASE;. Para “ver” usa-se o comando expect, e para “digitar” usa-se o comando send.

O expect ainda pode esperar por várias strings, e executar um comando send diferente para cada uma.

No exemplo abaixo, se o Flashback estiver habilitado, deve ser desabilitado, e vice-versa.

[oracle@nerv10 ~]$ cat ./AlteraFlashback.sh
#!/usr/bin/expect

set timeout -1

spawn sqlplus / AS SYSDBA

expect "SQL"
send -- "SELECT FLASHBACK_ON FROM V\$DATABASE;\r";

expect {
"YES" { send "ALTER DATABASE FLASHBACK OFF;\r"}
"NO" { send "ALTER DATABASE FLASHBACK ON;\r"}
}

expect "SQL"
send -- "EXIT;\r"

expect eof

[oracle@nerv10 ~]$
[oracle@nerv10 ~]$ ./AlteraFlashback.sh
spawn sqlplus / AS SYSDBA

SQL*Plus: Release 12.1.0.2.0 Production on Tue Dec 27 13:50:57 2016

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

SELECT FLASHBACK_ON FROM V$DATABASE;

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Advanced Analytics
and Real Application Testing options

SQL>
FLASHBACK_ON
------------------
YES

SQL> ALTER DATABASE FLASHBACK OFF;
EXIT;

Database altered.

SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Advanced Analytics
and Real Application Testing options
[oracle@nerv10 ~]$
[oracle@nerv10 ~]$ ./AlteraFlashback.sh
spawn sqlplus / AS SYSDBA

SQL*Plus: Release 12.1.0.2.0 Production on Tue Dec 27 13:51:16 2016

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

SELECT FLASHBACK_ON FROM V$DATABASE;

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Advanced Analytics
and Real Application Testing options

SQL>
FLASHBACK_ON
------------------
NO

SQL> ALTER DATABASE FLASHBACK ON;
EXIT;

Database altered.

SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Advanced Analytics
and Real Application Testing options
[oracle@nerv10 ~]$

Leave a Reply

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Esse site utiliza o Akismet para reduzir spam. Aprenda como seus dados de comentários são processados.