Running oracle commands Directly from Command Line

In mysql, I can get what I want from a database without having to log into mysql. works great for me and it is perfect.

now, i want to be able to do the same in oracle but I dont know how to.

For example, if i want to retrieve information from the mysql database from the command line, i issue a command similar to the below:

mysql --pager=/usr/bin/less -u cacti_user -p -e 'select hostname,description,availability,notes,availability_method from host' cacti

Oracle has to have similar thing to this. What is it? How can I retrieve this same information from a database using the command line?

Something like this:

sqlplus -S user/password@sid << EOF
select sysdate from dual;
EOF

What is the "sid" part? and is there a way to make a one liner out of that code?

The SID is the system identifier for a database, describing the listener to use to connect. And I wouldn't know of a way to do this in a one-liner.

$ printf 'select * from dual;' | sqlplus -S '/ as sysdba'

D
-
X

For Oracle >= 10g you don't need to quote the / as sysdba part (or you can use a regular username/password[@service] pair).

Some shells support here-strings via the <<< operator:

$ sqlplus -S '/ as sysdba'<<<'select * from dual;'

D
-
X