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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-14-2013, 04:05 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 18
Rep:
|
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.
|
|
|
|
02-14-2013, 10:41 PM
|
#2
|
|
Member
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 137
Rep:
|
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?
|
|
|
|
02-15-2013, 12:13 AM
|
#3
|
|
LQ Newbie
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 18
Original Poster
Rep:
|
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?
|
|
|
|
02-15-2013, 10:02 AM
|
#4
|
|
Member
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 137
Rep:
|
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.
|
|
|
|
02-15-2013, 12:47 PM
|
#5
|
|
Member
Registered: Aug 2009
Distribution: CentOS
Posts: 693
|
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'
|
|
|
|
02-21-2013, 12:01 AM
|
#6
|
|
LQ Newbie
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 18
Original Poster
Rep:
|
Quote:
Originally Posted by lykwydchykyn
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.
|
|
|
|
02-21-2013, 12:10 AM
|
#7
|
|
LQ Newbie
Registered: Aug 2009
Location: HYD,India
Distribution: RHEL
Posts: 18
Original Poster
Rep:
|
Quote:
Originally Posted by rknichols
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 
|
|
|
|
02-21-2013, 03:48 PM
|
#8
|
|
Member
Registered: Aug 2009
Distribution: CentOS
Posts: 693
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:55 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|