export variable not working after su


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

Try cat instead of su and you'll see what's happening.

Code:
$ cat <<EOF
export var2=345
echo "var0 is $var0"
echo "var1 is $var1"
echo "var2 is $var2"
EOF

export var2=345
echo "var0 is "
echo "Var1 is "
echo "var2 is "

$

Your variables are getting substituted too soon.

Try <<"EOF" The quotes make it take the contents inside the here-doc entirely literally. No substitution.
# 9  
Old 08-23-2012
The script in post #10 would be much better as:
Code:
cat "${SRCDIR}/install.control" | grep -v "^#" | while read filestring
do
        echo "FILESTRING=${filestring}"
done

Never use a for loop for open-ended lists.
Third time this week I have seen the "cat and for" combination but I have still never seen this construct in a manual or course notes.


Still thinking about your main question because it is virtually impossible to work out what is intended from faulty code. So, general answer only:

What I usually do is the following construct:
1) Create the script "scriptname" which you want to run and make it accept parameters ($1 $2 $3 ...) and convert those parameters to the Environment Variable names required by the installation script. Ensure that the Environment Variables are exported.

2) Run the script from the su command line, complete with the parameters.
Code:
su username -c "scriptname param1 param2 param3"

I'd only execute su - if I really wanted to execute the other user's profile. I'd rather set all the Environment Variables I need in the script itself.

Hope this helps.

Addendum: If you need to pass parameters back to the calling script, echo the parameters from the sub script and read them:
Code:
su username -c "scriptname param1 param2 param3" |  read subscript_param1 subscript_param2 subscript_param3

This will only work if you deal with absolutely all other output from the called script by redirection to logfiles or whatever.

Last edited by methyl; 08-23-2012 at 06:36 PM..
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