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
# 8  
Old 03-24-2014
Passing DB name as a parameter was not very succesfull.
Just to let you know that ORACLE_SID should ${DB_NAME}1 since this is RAC. Hence I am using $11 .eg BATGPRD1

As shown below, the execution didn't go well as the PATH variable didn't get set correctly and the script cannot find sqlplus and srvctl binaries.

Code:
# cat test3.sh
su - oracle -c "export ORACLE_SID=$11;export ORACLE_HOME=/oracle/product/oracle/11.2.0;export PATH=$PATH:$ORACLE_HOME/bin
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|grep -v source ; srvctl start database -d $1"
#

### Executing . Pass BATGPRD as Parameter1
Code:
# ./test3.sh BATGPRD
-bash: line 1: sqlplus: command not found
oracle   18475     1  0 23:00 ?        00:00:00 ora_pmon_BATGPRD1
grid     31520     1  0 Feb16 ?        00:15:33 asm_pmon_+ASM1
-bash: line 11: srvctl: command not found
#

# 9  
Old 03-24-2014
Uh, no. The shell parser on the receiving end of su -c has to know this is one command.

Code:
su - someuser  -c 'ls; grep foo bar'
su - someuser  -c "ls; grep foo bar"

Either of these will work.

A probably ridiculous example:

Code:
su - someuser  -c 'su - anotheruser  -c "ls; grep foo bar" '

You might do this because of how /etc/sudoers is set up.
This User Gave Thanks to jim mcnamara For This Post:
# 10  
Old 03-24-2014
Thanks Jim. Thanks everyone.

Any idea why passing the DB name as the parameter doesn't work ? Code and its execution shown in my last post. Just reminding that I execute the script as root.
# 11  
Old 03-24-2014
Yes.
You could try this:
Code:
su -c "echo $UID;whoami;echo $USER"

EDIT -- happens when one answers at the end of page one... --

Does the o_info script (as in Chubler's example) check for $1 ?

If you SOURCE the file, it will use the same variable name as the caller script: $database
If you EXECUTE the file, it will as well parse its arguments, starting with $1 which would be the value of $database.

Hope this helps

Last edited by sea; 03-24-2014 at 09:34 PM..
This User Gave Thanks to sea For This Post:
# 12  
Old 03-24-2014
Thank you Sea. So, how can I pass the database name into the su command from the caller script ?
# 13  
Old 03-25-2014
The problem I think is that some of your $ variables are being expanded before the su command is run, try escaping them like this, I don't really like $11 either, can I suggest ${1}1 for more readability:

Code:
# cat test3.sh
su - oracle -c "export ORACLE_SID=${1}1;export ORACLE_HOME=/oracle/product/oracle/11.2.0;export PATH=\$PATH:\$ORACLE_HOME/bin
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|grep -v source ; srvctl start database -d $1"
#

This User Gave Thanks to Chubler_XL For This Post:
# 14  
Old 03-25-2014
sample_cmd:
Code:
printf "My location is: $0
My arguments are: $*
Database: $database"

Your script:
Code:
database=myDBname
echo EXEC:
sh /path/to/../sample_cmd $database
echo SOURCE
. /path/to/../sample_cmd someWHAT

Saying:
Inside the script, you can just pass an argument like you would do beeing in the shell.
Whethere there is full path required or not, depens on the scripts location.

$database is only availabe within the caller script, however, calling another script passing $database inside the caller script, calls the 2nd script with 'expanded' $database, so the 2nd script gets "myDBname" as argument.

In your script call it like:
Code:
su -c "/path/to/o_info $database"

hope this helps
This User Gave Thanks to sea For This Post:
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