export variable not working after su


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting export variable not working after su
# 1  
Old 08-23-2012
export variable not working after su

I have a requirement to change user inside a shell script and execute group of commands. I have done it many times earlier but never came across the issue with exporting variables. Strangely if a var is exported afetr su, it is not working where as if it is does outside su, it works. Another issue I noticed was with variable in for loop. Also I didnt have these problems with many other commands inside su .

Code:
#!/bin/sh
#ident  "@(#)Test script"
#set -x
id
var0=123
su - user1<<EOF
id
var1='123'
export var2=345
echo "var0 is $var0"
echo "var1 is $var1"
echo "var2 is $var2"
EOF

Here is the output am getting where var1 and var2 is blank.

HTML Code:
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
uid=501(user1) gid=30001(user1) groups=30001(user1)
var0 is 123
var1 is
var2 is
Would really appreciate your help as am clueless with what's happening and my options are exhausted

Last edited by methyl; 08-23-2012 at 06:33 PM.. Reason: spellcheck for readablility
# 2  
Old 08-23-2012
The contents (including those variables) of the heredoc are expanded by the parent shell.

Regards,
Alister
# 3  
Old 08-23-2012
sorry for my ignorance. What does that mean?

is it not possible to do it this way?
# 4  
Old 08-23-2012
Please post what Operating System and version you are running so we can work out what Shell /bin/sh is (unless you can tell us).

What @alister is saying is that the environment variables in the "Here Document" (the bit between <<EOF and EOF) are evaluated by the calling Shell BEFORE the su - runs.

I can't think of an Operating System where the script would work at all because su - should prompt for a password. ... Unless you are logged in as root ?

There are techniques to do what I think you want to do, but let's find out what you actually want the script to do? Are you running as root ?
# 5  
Old 08-23-2012
I am using Redhat Linux 2.6.32-220.4.1.el6.x86_64 and is logged in as root


The final idea is to login as root and su to some other id and run installation scripts. Those installation script need to access some variables and hence trying to set those.

As I mentioned, its not only failing in setting variables but also thing like below are also not working

Code:
for filestring in `cat $SRCDIR/install.control | grep -v "^#"`
do
echo "FILESTRING=${filestring}"
done

Can you please advise any other technique to accomplish this?

Last edited by methyl; 08-23-2012 at 06:34 PM.. Reason: correct spelling and typos for readability ... and code tags
# 6  
Old 08-23-2012
When you run that script, it promptly exports var2=345, guaranteeing that any child processes it creates will have this variable in their environments.

Then, it finishes and ceases to exist without creating any child processes. Meaning, the variable was never set anywhere except inside that script.

Children inherit exported values from their parents. Running a script doesn't set values in your own shell. Try sourcing the script instead, which runs it without creating a new shell. source ./scriptname
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-23-2012
As i mentioned in the original post, i understand that children inherit exported values from parents, because if i move that export outside su, am still able to access those values

However am looking for ways to make this work inside heredoc. Or is there any alternate way of accomplishing this?

Last edited by methyl; 08-23-2012 at 06:26 PM.. Reason: another spellcheck
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help export variable

Hi, Please find my code below. ps -xef | grep 14766 | awk 'BEGIN{X="java_home=";X="weblogic_home="} {for(i=1;i<=NF;i++){if($i ~ /-Dplatform\.home|java$/){split($i,P,"=");s=P?P:$i;print X""s}}}' echo "java_home="$java_home echo "weblogic_home="$weblogic_home Output: Why does... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Can't export variable

I am relatively new to exporting variables, and I just can't seem to make this count work. What I have is the following: TOTAL=$($IMAGELIST -backupid $IM -U |gawk '{print $5}' |tail -1)|gawk '{print $6}' RESTORED=$($BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d,) |gawk '{print $6}' export... (7 Replies)
Discussion started by: newbie2010
7 Replies

3. Shell Programming and Scripting

export of a variable isn't working

Hi I want export input data ... echo "month: " read m export m=$m also export m is not working ? the month-variable should be exportet for the use in other scripts, but it is not working like this. What i'm doing wrong? Thanks in advance! IMPe (10 Replies)
Discussion started by: IMPe
10 Replies

4. Shell Programming and Scripting

where does variable export to?

Hi, Unix Gurus, I have a problem need help. I have a script to generate environment variable code same as following: oracle_sid=abcd export oracle_sid when I execute this code with command ./script_nane it succeeded. when I try to find it with env command or echo $oracle_sid, it does not show... (5 Replies)
Discussion started by: ken002
5 Replies

5. Shell Programming and Scripting

Export variable

Hi I have a pass a variable from one script to another. Here are my scripts Script #1 ./profile #!/bin/sh export NAME="Hello" Script #2 ./test #!/bin/sh ./profile echo $NAME when I run ./test .. i am not getting anything .. why is that? (5 Replies)
Discussion started by: arex876
5 Replies

6. Solaris

nfs export not working at boot

we have a Solaris 8 nfs server that exported two shares. entries are in /etc/dfs/dfstab. clients have been accessing these shares for several years. we just rebooted this nfs server and noticed that no share gets exported. I don't see relevant messages from dmsg nor messages file, is there... (6 Replies)
Discussion started by: jalite19
6 Replies

7. Shell Programming and Scripting

export not working in Bash shell

Hi Friends, I am presently migrating shell scripts writter in KSH to SH.I am stuck at this place and i am not able to find a work around:- Let the script name is x.sh Below are some of the codes in it... export abc=hello export abc=hi export abc=how When i am trying to compile the script ... (6 Replies)
Discussion started by: amit.behera
6 Replies

8. AIX

export command is not working in AIX5.3

I am trying to export command at shell in AIX5.3. example: environment variable SAMPLE=test is set. after logged in to the shell, i tried to export this variavle to $ export SAMPLE=test1. If i do $ echo $SAMPLE, it is still giving test instead of test1. what shall i do?? (1 Reply)
Discussion started by: balareddy
1 Replies

9. Shell Programming and Scripting

Export Query not Working in Shell

I am exporting data using a shell program. I am getting all of the records instead of getting a subset based on my query. The export_problem.par lists all of my tables. I tried the query with the ' and without it. How do I get the export to use the query? Thanks! ... (1 Reply)
Discussion started by: GEBRAUN
1 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question