script stays in Oracle db - linux

When I use this script :
sqlplus -s "/ as sysdba" << EOF
startup;
EOF
It starts db and returns to host. I want my script doesn't return to host. Where is my mistake?

Does it help if you add exit to your script? In other words, if you try something like:
sqlplus -s "/ as sysdba" << EOF
startup;
exit
EOF
EDIT: if you don't want SQL*Plus to exit after running startup, put the line
startup;
in a file named startup.sql, say. You can then run
sqlplus -s "/ as sysdba" #startup.sql
That should then start the database and leave you in SQL*Plus.

Related

How can I run commands in nested SSH connections in Bash?

I need to write a script which will connect to the server and run some utils there.
So, if I want to connect to the server, I do
ssh $server << EOF
run
some
commands
EOF
And it works properly.
But if I want to do nested ssh connection, I'm doing like this:
ssh $server_1 << EOF
ssh $server_2 << EOF
run some commands
EOF
I guess it works properly, but I'm receiving error messages
Do you know how to use "nested" EOFs properly?
I know that I can run
ssh $server 'run|some|commands'
but there are a lot of commands here and I cant write it into a line
Thank you for answers
Use two different "end of here-document" delimiters:
ssh $server_1 << EOF1
ssh $server_2 << EOF2
run some commands
EOF2
EOF1
Or better yet, use an "SSH jump host" like this:
ssh -J $server_1 $server_2 << EOF
run some commands
EOF

How to enter pass and username inside script

I wrote a shell script and ı will set it into crontab but ı have a problem. When crontab run my script, running scp command inside script and get some informations from different server so ı have to enter password of other server inside my script. How to enter password inside script ?
my script:
scp username#host:/Products/data/bridge/control.txt .
????? ( have to automatically enter password)
b=$(more control.txt | wc -l)
if [$b = 1]; then
echo " OK "
fi
You could use expect, for example:
my_password="123456"
expect -c "spawn scp username#host:/Products/data/bridge/control.txt .; expect \"password\" {send -- \"${my_password}\r\"; expect eof;};"

Passing the name of spool file to sqlplus from a shell script

I am trying to write a unix program, in which I need to connect to the SQL DB and fetch the data and store it into a file.
Currently I am using the following command:
output1=`sqlplus -s username#SID/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
SPOOL EMP_NAMES.txt
select emp_name from employee order by emp_name;
Spool off;
This is working fine. But my requirement was that I want to pass the value of spool file such that everytime a new Spool file would be generated.
I basically want to append the date at the end of the file name like:
date=`date +'%d/%m/%Y_%H:%M:%S:%2N'`
output1=`sqlplus -s username#SID/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
SPOOL EMP_NAMES_$date.txt
Kindly let me know as to how this can be done.
If you call your sqlplus with a heredoc, you can do this easily:
spool_file=: ... your date logic here ...
sql_ouput=: ... path for sqlplus output & errors ...
sqlplus -s username#SID/password << EOF &> "$sql_output"
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING ON ECHO OFF;
spool $spool_file
# SQL statements
spool off
EOF
if [[ $? != 0 ]]; then
: ... error handling ...
fi
It's better to capture stdout/stderr of sqlplus into a file rather than a shell variable.
I think it is possible to hide the password by removing it from the command line and adding it as the first line of heredoc (this will prevent password from showing in ps)
Take a look at this related post: Connect to sqlplus in a shell script and run SQL scripts

Create SSH Connection Independent Of Calling Bash Script

I am trying to create a bash script that serves as a wrapper for numerous ssh connection options. Basically the user will run the script and the appropriate ssh command will then be created and executed based on their selections. I am trying to find a way to have the script exit and the ssh connection created, rather than having the script continue to run while the user is working on the remote server. Does anyone know if this is possible or how I could achieve this? Thanks!
Edit
Sorry, I should have posted my code:
display_main_menu(){
while true
do
clear
echo ""
echo " Select A Server:"
echo " -------------------------------"
echo " 1) Server 1"
echo " 2) Server 2"
echo " 3) Server 3"
echo " 4) Quit"
local selection
read -p " Enter choice [1 - 4] " selection
case $selection in
1)
# Open SSH Connection to Server 1
break
;;
2)
# Open SSH Connection to Server 2
break
;;
3)
# Open SSH Connection to Server 3
break
;;
4)
exit 0
break
;;
;;
*)
echo -e "Invalid Selection..." && sleep 2
;;
esac
done
}
Use:
exec ssh <pararameters>
The exec builtin command:
Synopsis
exec [-a NAME] [-cl] [COMMAND] [ARG...] [REDIRECTION...]
Description
The exec builtin command is used to
replace the shell with a given program (executing it, not as new
process)
set redirections for the program to execute or for the
current shell
If only redirections are given, the redirections affect the current shell without executing any program.

Read command in bash script not waiting for user input when piped to bash?

Here is what I'm entering in Terminal:
curl --silent https://raw.githubusercontent.com/githubUser/repoName/master/installer.sh | bash
The WordPress installing bash script contains a "read password" command that is supposed to wait for users to input their MySQL password. But, for some reason, that doesn't happen when I run it with the "curl githubURL | bash" command. When I download the script via wget and run it via "sh installer.sh", it works fine.
What could be the cause of this? Any help is appreciated!
If you want to run a script on a remote server without saving it locally, you can try this.
#!/bin/bash
RunThis=$(lynx -dump http://127.0.0.1/example.sh)
if [ $? = 0 ] ; then
bash -c "$RunThis"
else
echo "There was a problem downloading the script"
exit 1
fi
In order to test it, I wrote an example.sh:
#!/bin/bash
# File /var/www/example.sh
echo "Example read:"
read line
echo "You typed: $line"
When I run Script.sh, the output looks like this.
$ ./Script.sh
Example read:
Hello World!
You typed: Hello World!
Unless you absolutely trust the remote scripts, I would avoid doing this without examining it before executing.
It wouldn't stop for read:
As when you are piping in a way you are forking a child which has been given input from parent shell.
You cannot give the values back to parent(modify parent's env) from child.
and through out this process you are always in parent process.

Resources