LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How command line options work after switching the user in script? (https://www.linuxquestions.org/questions/linux-general-1/how-command-line-options-work-after-switching-the-user-in-script-4175450104/)

visu_kvg 02-14-2013 04:05 AM

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.

lykwydchykyn 02-14-2013 10:41 PM

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?

visu_kvg 02-15-2013 12:13 AM

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?

lykwydchykyn 02-15-2013 10:02 AM

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.

rknichols 02-15-2013 12:47 PM

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'

visu_kvg 02-21-2013 12:01 AM

Quote:

Originally Posted by lykwydchykyn (Post 4892334)
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.

visu_kvg 02-21-2013 12:10 AM

Quote:

Originally Posted by rknichols (Post 4892450)
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:confused:

rknichols 02-21-2013 03:48 PM

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



All times are GMT -5. The time now is 08:28 PM.