Issue with running multiple commands withing su command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with running multiple commands withing su command
# 1  
Old 03-24-2014
Issue with running multiple commands withing su command

RHEL 6.2/Bash shell

root user will be executing the below script. It switches to oracle user and expect to do the following things

A. Source the environment variables for BATGPRD Database (the file used for sourcing is shown below after the script)
B. Shutdown the DB from sqlplus -- The section between EOF takes care of this
C. execute ps command and grep for the string pmon
D. Execute srvctl start command to start the database

Commands for A,B,C and D and enclosed in a single quote delimited by colons.

Issue description:
DB is shutdown succesfully from sqlplus (A & B ). But ps command and srvctl commands shown in red are not executed.
The exit command shown below (in green below) is actually the exit from sqlplus. I don't if this affecting the execution of remaining
sections of the script.


Code:
# cat test1.sh
su - oracle -c 'source /home/oracle/BATGPRD1_oracle_env;sqlplus -s / as sysdba <<EOF
spool /home/oracle/SCRIPTS/Clone/mytestSQL.log
set echo off
set heading off

select sysdate from dual;
shutdown immediate;
select name, open_mode from v$database;
exit
EOF; ps -ef | grep pmon ; srvctl start database -d BATGPRD'
#
#

#
The file which I am sourcing above
Code:
# cat /home/oracle/BATGPRD1_oracle_env
export ORACLE_SID=BATGPRD1
export ORACLE_HOME=/db/product/oracle/11.2
export PATH=$PATH:$ORACLE_HOME/bin
#

#


### Script execution
Code:
# ./test1.sh
-bash: line 9: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')

24/03/2014

Database closed.
Database dismounted.
ORACLE instance shut down.
select name, open_mode from v
*
ERROR at line 1:
ORA-01034: ORACLE not available
Process ID: 71641
Session ID: 2243 Serial number: 5

# 2  
Old 03-24-2014
EOF needs to be on it's own, try this:
Code:
EOF
ps -ef | grep pmon ; srvctl start database -d BATGPRD'

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 03-24-2014
You can do it like this:

Code:
su - oracle -c "source /home/oracle/BATGPRD1_oracle_env;sqlplus -s / as sysdba <<EOF ; ps -ef | grep pmon ; srvctl start database -d BATGPRD
spool /home/oracle/SCRIPTS/Clone/mytestSQL.log
set echo off
set heading off

select sysdate from dual;
shutdown immediate;
select name, open_mode from v$database;
exit
EOF
"

Can I suggest putting the code in another script (like /usr/local/bin/o_info) and pass $database in as a parameter you can the call with su like this:

Code:
su - oracle -c "/usr/local/bin/o_info $database"

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 03-24-2014
Thank you Subbeh. Your fix worked.

Hi Chubler,
Your idea of passing DB name as a parameter is great. Hope it won't treat $database as a literal because of the double quotes. Let me try to modify the script as per suggestion. If it works, it will make my script much more flexible. Thank you ChublerXL
# 5  
Old 03-24-2014
Regarding Chubler's example

Code:
su - oracle -c "/usr/local/bin/o_info $database"

Is double quotes used when you need to pass a parameter ?
# 6  
Old 03-24-2014
-c is for command.. that means a string, so to pass more than 1 string you will use double quotes making what is within considered as a single...
This User Gave Thanks to vbe For This Post:
# 7  
Old 03-24-2014
Hi vbe,
As you can see above, OP used just single quotes to enclose multiple commands. So double quotes are not actually needed when you have multiple commands. Right ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check status of long running multiple curl commands in shell script

I am working on script. it reads a file which contains multiple lines Ex; curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=1 curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=2 curl --write-out %{http_code} --silent ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies

2. Shell Programming and Scripting

Perl script running multiple commands at once

Hi All, I have put a perl script together to go and collect some information from multiple nodes/endpoints. The script works absolutly fine however I want to make it quicker. You will see in the below that my script calls an expect script called ssh_run_cmd2.exp followed by the IP of... (7 Replies)
Discussion started by: mutley2202
7 Replies

3. Shell Programming and Scripting

Issue with quotes when running SQL command from within su -c

RHEL 6.2/Bash shell root user will be executing the below script. It switches to oracle user logs in using sqlplus and tries to run the below UPDATE statement. All the commands after su -c are enclosed in a single quote delimited by semicolon. The execution has failed because the quotes... (3 Replies)
Discussion started by: omega3
3 Replies

4. Shell Programming and Scripting

Issue with running commands from shell script

I'm trying to copy files from a remote windows server to Unix server. I was successfully able to copy files from windows server using command prompt but when I run these commands from a script it's not working as expected. commands used: sftp user@remoteserver.com lcd local_dir cd... (3 Replies)
Discussion started by: naresh7590
3 Replies

5. Shell Programming and Scripting

Running multiple commands stored as a semi-colon separated string

Hi, Is there a way in Korn Shell that I can run multiple commands stored as a semi-colon separated string, e.g., # vs="echo a; echo b;" # $vs a; echo b; I want to be able to store commands in a variable, then run all of it once and pipe the whole output to another program without using... (2 Replies)
Discussion started by: svhyd
2 Replies

6. Programming

Running Multiple Unix commands in qx

Hi All, Is there anything wrong with below syntax? qx {perldoc -v ModuleName.pm | grep -i Description } BTW, this question is related to Perl. Thanks. (3 Replies)
Discussion started by: jal_capri
3 Replies

7. UNIX and Linux Applications

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... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. Shell Programming and Scripting

awk splits file in to multiple - how to get filenames in variables withing ksh?

Hi all, I'm using awk in a .ksh script to split one file by line prefix into different files (up to 4). The files are named after the prefix in the line, and a sequence no. Is there any way to get the filenames in to variables too? I need _ftpfile1, _ftpfile2, _ftpfile3 and _ftpfile4 which are... (2 Replies)
Discussion started by: spidermike
2 Replies

9. Shell Programming and Scripting

Running multiple unix commands in a single script

Hi, I would like to write a script with include more than 6 unix commands. my script like below: echo " script started" ls -ld bdf | grep "rama" tail -10 log.txt ... .. ... now, i want to run above unix commands one by one. example: first the ls -ld command will be... (3 Replies)
Discussion started by: koti_rama
3 Replies

10. UNIX for Dummies Questions & Answers

running a simple script file with multiple commands

I'm trying to run a script file with multiple commands that I would normally type into the command line. The commands are: #!/bin/bash diff Test1.o0 /usr3/ronelso4/Desktop/verificationKPC/Test1.o0 > differences2 diff Test1a.o0 /usr3/ronelso4/Desktop/verificationKPC/Test1a.o0 >> differences2... (1 Reply)
Discussion started by: knelson
1 Replies
Login or Register to Ask a Question