Switching user inside a shell script doesn't seem to work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switching user inside a shell script doesn't seem to work
# 1  
Old 03-24-2014
Switching user inside a shell script doesn't seem to work

Linux version : Oracle Linux 6.4
Shell : Bash

The following script will be run as root. During the execution, it should switch to oracle user and execute few commands.
After googling and searching within unix.com , I came up with the following syntax

## Enclosing all commands in double quotes with the following syntax
Code:
su -l anotheruser -c "command1;command2;command3;..."

## Using single quote like below didn't work for me
Code:
su -l anotheruser -c 'command1;command2;command3;...'

So, I created the following simple script called test.sh and executed it.
But it seems that the switch user using su didn't work properly.
/tmp/who.txt file shows root. It should show orace user if su worked properly.
/tmp/MYVAR2.txt is empty . It should show Duffy
/tmp/Instancename.txt is empty. It should show JUMBO1


Code:
# cat test.sh
#!/bin/bash
#Usage     :  /root/SCRIPTS/test.sh <Argument1>
echo -e "\n"

## Currently you are root user

## su to oracle user and run the following commands which are separated by semi colon

su -l oracle -c "export ORACLE_SID=$11;export MYVAR1=Stomper;export MYVAR2=Duffy; echo `whoami` >/tmp/who.txt;echo $1 >/tmp/Arg_name.txt;echo $ORACLE_SID >/tmp/Instancename.txt;echo $MYVAR2 >/tmp/MYVAR2.txt"

whoami ## Now you should be back as root
#

-- Executing the script
Code:
#
# test.sh JUMBO


root
#

--- Verifying the content of the files
Code:
# cat /tmp/who.txt
root
# cat /tmp/Arg_name.txt
JUMBO
#
#
# cat /tmp/MYVAR2.txt

# cat /tmp/Instancename.txt

#

# 2  
Old 03-24-2014
The variable and command substitutions in the double-quoted string are performed by the parent shell, while it reads the string, before it invokes su.

Regards,
Alister
# 3  
Old 03-24-2014
Because of the use of double quotes, echo `whoami` gets executed before the su - l takes place. What did not work for you, when you were using single quotes?
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-24-2014
you can try this

Code:
su -l oracle -c "export ORACLE_SID=$11;export MYVAR1=Stomper;export MYVAR2=Duffy; echo \`whoami\` >/tmp/who.txt;echo $1 >/tmp/Arg_name.txt;echo $ORACLE_SID >/tmp/Instancename.txt;echo $MYVAR2 >/tmp/MYVAR2.txt"

This User Gave Thanks to sumitsks For This Post:
# 5  
Old 03-24-2014
Thanks everyone.

I tried enclosing in single quotes. But it doesn't work properly. Any Idea how I can fix this ?

Code:
# cat test_singleQuote.sh
#!/bin/bash
#Usage     :  /root/SCRIPTS/test_singleQuote.sh <Argument1>
# single quote variant of test.sh

echo -e "\n"

## Currently you are root user

## su to oracle user and run the following commands which are separated by semi colon

su -l oracle -c 'export ORACLE_SID=$11; echo $ORACLE_SID >/tmp/ORACLE_SID_VAR; export MYVAR1=Stomper; \
export MYVAR2=Duffy; echo `whoami` >/tmp/who.txt;echo $1 >/tmp/Arg_name.txt; echo $ORACLE_SID >/tmp/Instancename.txt; echo $MYVAR2 >/tmp/MYVAR2.txt'

whoami ## Now you should be back as root
#
#


--- Executing the script
#
Code:
# test_singleQuote.sh SOFIA


root

-- Verifying the contents of files

# cat /tmp/ORACLE_SID_VAR
1 ---------------------> I am expecting SOFIA1 Smilie
#
# cat /tmp/who.txt
oracle ---------------------> Worked as expected Smilie
#
# cat /tmp/Arg_name.txt
----------------------> null. I was expecting SOFIA Smilie
#
# cat /tmp/MYVAR2.txt
Duffy ---------------------> Worked as expected Smilie
#

Any Idea how I can fix this ?

Last edited by vbe; 03-24-2014 at 06:10 AM..
# 6  
Old 03-24-2014
Try this sample code:
Code:
#!/bin/bash
su -l guest -c "export a=$1;export b=$2;echo \`whoami\`>/tmp/me;echo \$a >/tmp/a;echo \$b>/tmp/b"
echo Now I am
whoami
echo -----------
echo Contentes of file me
cat me
echo -----------
echo Contentes of file a
cat a
echo -----------
echo Contents of file b
cat b

And this is the output I am getting:
Code:
./test.sh 1 2
Now I am
root
-----------
Contentes of file me
guest
-----------
Contentes of file a
1
-----------
Contents of file b
2

This User Gave Thanks to chacko193 For This Post:
# 7  
Old 03-24-2014
Thanks Chacko, everyone
Your script works.
How does this work, when all the echo commands put together after the export commands ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Su to another user from root doesn't work within shell script

Hello I have a shell script that is run as root. Script rins ok until the point where it have to switch to user "mqm" to run other commands. It just hangs at the point of this line in the script su - mqm -c "dspmq" I ran the same commands at the terminal and they run fine. Any thoughts. (6 Replies)
Discussion started by: mo12
6 Replies

2. Shell Programming and Scripting

How to login as a different user inside a shell script?

hi, i want to login as a different user inside a shell script and then call another shell script from that script. how to do that? original script : script_A.sh so when the script_A.sh is called , i want to login as a different user and then call another shell script(script_B.sh) from... (3 Replies)
Discussion started by: Little
3 Replies

3. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

4. Shell Programming and Scripting

Perl variables inside Net::Telnet::Cisco Module doesn't work

I am writing perl script to configure Cisco device but Variables inside Net::Telnet::Cisco Module doesn't work and passed to device without resolving. Please advise. here is a sample of script: use Net::Telnet::Cisco; $device = "10.14.199.1"; ($o1, $o2, $o3, $o4) = split(/\./,$device);... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

5. Shell Programming and Scripting

$RANDOM does not work inside a shell script

Hi folks I'm coding on Ubuntu 9.04 standard shell. I'm writing a script that needs to generate a random number at some point of its execution. When I do echo $RANDOMas a command inside shell, I clearly get some randomly generated number However when I do i=`$RANDOM` echo $ior even... (14 Replies)
Discussion started by: ksk
14 Replies

6. UNIX for Dummies Questions & Answers

Switching user inside a shell script

Dear All, I want to switch a user inside a shell script.My current user say x and user y to whom i want to switch both have login on the same server,the one on which i want to execute my script on. "I want to do something like this su - y Password should be provided in the script itself.And... (6 Replies)
Discussion started by: navjotmannan
6 Replies

7. Shell Programming and Scripting

shell script, echo doesn't work

#!/bin/sh something(){ echo "Inside something" echo $1 $2 } val=$(something "Hello " "world") Output expected: Inside somethingHello world But it's not echoing. (4 Replies)
Discussion started by: cola
4 Replies

8. Shell Programming and Scripting

switching to another user in shell script...

Hi, I have a shell script in which I need to switch to another user and execute some commands and then come back to the original user. To make it more clear - I have to log in as user root then 'su' to jag - execute a script called backup.sh and then logout and come back to root again.. ... (1 Reply)
Discussion started by: bjagadeesh
1 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. Shell Programming and Scripting

grep doesn't work within shell script?

I am trying to run the following code from a script file but it complains that syntax of (both instances of) grep is wrong. When I copy and paste it to the terminal, it is OK. Any idea what the problem might be? set i = `grep -c #define flags.h` while ($i>20) @ i-- my func (`cat... (4 Replies)
Discussion started by: barisgultekin
4 Replies
Login or Register to Ask a Question