LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-14-2013, 04:05 AM   #1
visu_kvg
LQ Newbie
 
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 19

Rep: Reputation: 0
How command line options work after switching the user in script?


Hello All,

I am writing a script where I need to switch to another user in the middle of the script. It goes as below

Code:
echo "$HOME"
echo $1
id
sudo su - oradev << \EOF
echo "$HOME"
echo $1
id
When I run the above script as "sh script_name hello" out put is as below
Code:
/home/nagios
hello
uid=5003(nagios) gid=5003(nagios) groups=500(dba),5003(nagios)
/home/oradev
                           <####### I want my command line option,hello, to be echoed here as well 
uid=544(oradev) gid=500(dba) groups=500(dba)
our command line option, $1=hello, is working fine before switching the user, but for some reason it is not showing up after switching user.
I would be really happy if any one has any ideas on how to get this worked.
 
Old 02-14-2013, 10:41 PM   #2
lykwydchykyn
Member
 
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 135

Rep: Reputation: 36
I tried your script on my Ubuntu system, and it worked just as you expected it to. What are you running this on? Is it using bash, sh, or some other shell?
 
Old 02-15-2013, 12:13 AM   #3
visu_kvg
LQ Newbie
 
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks for the reply lykwydchykyn.

It tried with bash and sh and the result was same in both the cases.

Can you paste your script and output?
 
Old 02-15-2013, 10:02 AM   #4
lykwydchykyn
Member
 
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 135

Rep: Reputation: 36
My script looked like this:
Code:
echo $HOME
echo $1
sudo su -  xtest <<EOF
echo $HOME
echo $1
EOF
Output (not counting the sudo prompt, since I'd previously authenticated sudo) was this:
Code:
$ bash test.sh hello
/home/alanm
hello
/home/alanm
hello
Interesting that "$HOME" did not change. Hrm.
 
Old 02-15-2013, 12:47 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,421

Rep: Reputation: 597Reputation: 597Reputation: 597Reputation: 597Reputation: 597Reputation: 597
Variables in a HERE document are expanded by the parent shell before the text is passed to the child process. To prevent that, you can either:
A. Use a backslash escape on each of the "$" characters in the HERE document,
or
B. Quote any of the characters in the "EOF" marker string, e.g.,
Code:
sudo su -  xtest <<'EOF'
 
Old 02-21-2013, 12:01 AM   #6
visu_kvg
LQ Newbie
 
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lykwydchykyn View Post
My script looked like this:
Code:
echo $HOME
echo $1
sudo su -  xtest <<EOF
echo $HOME
echo $1
EOF
Output (not counting the sudo prompt, since I'd previously authenticated sudo) was this:
Code:
$ bash test.sh hello
/home/alanm
hello
/home/alanm
hello
Interesting that "$HOME" did not change. Hrm.

Thanks for the reply lykwydchykyn.
From your output I can say that switch user to xtest is not happening in your script.
 
Old 02-21-2013, 12:10 AM   #7
visu_kvg
LQ Newbie
 
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rknichols View Post
Variables in a HERE document are expanded by the parent shell before the text is passed to the child process. To prevent that, you can either:
A. Use a backslash escape on each of the "$" characters in the HERE document,
or
B. Quote any of the characters in the "EOF" marker string, e.g.,
Code:
sudo su -  xtest <<'EOF'
Thanks for your reply rknichols.

I have run edited script as per your suggestion as below
Code:
echo "$HOME"
echo $1
id
sudo su - oradev <<'EOF'
echo "$HOME"
echo $1
id
'EOF'
I executed the script as 'sh script_name hello' to get the following output.

Code:
/home/nagios
hello
uid=5003(nagios) gid=5003(nagios) groups=500(dba),5003(nagios)
/home/oradev

uid=544(oradev) gid=500(dba) groups=500(dba)
-bash: line 4: EOF: command not found
I am not able to understand what happened
 
Old 02-21-2013, 03:48 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,421

Rep: Reputation: 597Reputation: 597Reputation: 597Reputation: 597Reputation: 597Reputation: 597
Quote that string (or any part of it) only where you are specifying the delimiter, not where the string occurs to mark the end of the document.
Code:
echo "$HOME"
echo $1
id
sudo su - oradev <<'EOF'
echo "$HOME"
echo $1
id
EOF
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Command line works, won't work from inside bash script w6lsn Programming 4 02-17-2011 05:45 AM
mkdir won't work in a script but works on command line ifeatu Linux - Newbie 3 02-04-2010 02:26 PM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 02:00 AM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 04:14 PM
switching from command line to gui on 9.2 mattfritze Mandriva 2 12-06-2003 07:43 PM


All times are GMT -5. The time now is 04:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration