get env variable from last script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get env variable from last script
# 1  
Old 02-03-2009
get env variable from last script

I have 2 scripts t2.sh calls t1.sh. I need to get the vaule of a env variable from t1.sh

Code:
/tmp/test$ cat t1.sh 
#!/bin/sh
INSTANCE="font/fc-cache"
export INSTANCE
svcadm disable  ${INSTANCE}

Code:
 
/tmp/test$ cat t2.sh 
#!/bin/sh
. /tmp/test/t1.sh
echo ${INSTANCE}

The above works fine on the condition that i know the env variable name
But if the env name is unknown, but can be got by grep "svcadm"

Code:
 
/tmp/test$ cat t2.sh 
#!/bin/sh
. /tmp/test/t1.sh
SVCNAME=`grep svcadm t1.sh| awk '{ print $3}'`
echo $SVCNAME

the above script doens't work, it ouputs ${INSTANCE} instead of the real value

So how to get the real value?
# 2  
Old 02-03-2009
Code:
temp=$( grep svcadm t1.sh )
var=${temp##* \${}
eval "$( grep "${var%\}}" t1.sh )"

# 3  
Old 02-03-2009
Smilie
Quote:
Originally Posted by cfajohnson
Code:
temp=$( grep svcadm t1.sh )
var=${temp##* \${}
eval "$( grep "${var%\}}" t1.sh )"

Thanks alot, that almost worked, it ran the svcadm enable command instead of print the value ${INSTANCE} and it seems work for bash only, Can you write sh version to get value of ${INSTANCE} only, I need the value to check it's status with another cmd e.g 'svcs ${INSTANCE}?

The last 2 expressions are hard to underdstand, Can you explain in details?
# 4  
Old 02-04-2009
Quote:
Originally Posted by honglus
Smilie

Thanks alot, that almost worked, it ran the svcadm enable command instead of print the value ${INSTANCE} and it seems work for bash only, Can you write sh version to get value of ${INSTANCE} only, I need the value to check it's status with another cmd e.g 'svcs ${INSTANCE}?

It will work in any standard UNIX shell.

On some systems, /bin/sh is an old Bourne shell. Such systems should also have a POSIX shell, e.g. ksh.
Quote:
The last 2 expressions are hard to underdstand, Can you explain in details?

See Parameter Expansion in your shell man page.
# 5  
Old 02-04-2009
Quote:
Originally Posted by cfajohnson
See Parameter Expansion in your shell man page.

Ok, I will check Parameter Expansion later, Can you give a quick solution to get the value of ${INSTANCE} only, because your script seems ran the cmd "svcadm disable ${INSTANCE}"
# 6  
Old 02-04-2009
Quote:
Originally Posted by honglus
Can you give a quick solution to get the value of ${INSTANCE} only,

Code:
eval "$( grep INSTANCE= t1.sh )"

Quote:
because your script seems ran the cmd "svcadm disable ${INSTANCE}"

No, it doesn't.
# 7  
Old 02-04-2009
Quote:
Originally Posted by cfajohnson
it didn't work, let me put in the other way

Code:
 
/tmp/test$ cat t3.sh 
#!/bin/bash
INSTANCE="font/fc-cache"
var=\$\{INSTANCE\}
echo $var
 
/tmp/test$ ./t3.sh 
${INSTANCE}

it outputs ${INSTANCE}, but i need outputs "font/fc-cache".

don't change this:"var=\$\{INSTANCE\} and i don't know the name INSTANCE.


that is how i made it work by creating new file, but it is stupid, any decent solution?


Code:
 
:/tmp/test$ cat t3.sh 
#!/bin/bash
INSTANCE="font/fc-cache"
var=\$\{INSTANCE\}
echo "echo $var" >t4.sh
. ./t4.sh

Code:
 
$ ./t3.sh 
font/fc-cache


Last edited by honglus; 02-04-2009 at 01:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Treat value of variable as env variable

Hi All, I have a requirement where I have a config file, which contains 2 coulmn.values of first column are environmnet variable, whose value is defined in an environment file. In my script I need to read the config file, and get the value of the config file variable from env file. I... (2 Replies)
Discussion started by: alok2082
2 Replies

2. Shell Programming and Scripting

Bash script - ENV Variable context problem using su

Hello I have found some piece of code to verify and then run shell script with root permission from normal user. see : http://blog.mecworks.com/articles/2006/02/23/bash-scripting-tip-running-a-script-as-root I have wrote two scripts using this tips. - one to copy file from server to local... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Web Development

Deny from env=env-variable Does not work

(Above from Apache docs). On my system, using: SetEnvIf User-Agent Mozilla IsBad=1 Order allow,deny Allow from all Deny from env=IsBad ...I see that environment variable is set (using phpinfo()) but the page is still served. No errors in the Apache logs. (1 Reply)
Discussion started by: gnurob
1 Replies

4. Shell Programming and Scripting

how to set/get shell env variable in python script

greetings, i have a sh script that calls a python script. the sh script sets an env variable BIN: export BIN=bin64i need to get that BIN variable's value and use it within this python script. anyone know how to do this? thanx in advance. (5 Replies)
Discussion started by: crimso
5 Replies

5. Solaris

Env variable in solaris

Hi, Am installing SAP on Solaris 10. How to set env variables permanently? Reg (0 Replies)
Discussion started by: daggupati453
0 Replies

6. Shell Programming and Scripting

Env variable

Hello, I want to cange env variable on SunOS. I tried: export GONGA=$GONGA:/users/BANK1/basic/queues/SARON_SPACE1 it changed it only localy for my session. when i opened a new session (telnet etc') the old value exist. How can I change it to effact all sessions. Thanks. (2 Replies)
Discussion started by: LiorAmitai
2 Replies

7. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

8. UNIX for Dummies Questions & Answers

Env Variable

Hi, I have a doubt on Environment variable. I want to know where and when the envirnment variables are defined? Thanks & Regards, Siba (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

9. UNIX for Dummies Questions & Answers

PWD env variable

Could you please tell me, which process / file is responsible for the setting of PWD env variable in Solaris Thanks (1 Reply)
Discussion started by: chaandana
1 Replies

10. Shell Programming and Scripting

bash env variable containing @

I want to set a bash env variable which has @ in its name, for example, @YOGESH@ may i know how do i do this? (4 Replies)
Discussion started by: Yogesh Sawant
4 Replies
Login or Register to Ask a Question