Solved

Export shell variable

Posted on 2015-02-10
7
88 Views
Last Modified: 2015-02-17
Hi ,

In my script i use export variable . So i can use the variable in another script which was sourced from main script.

But in command line, if i use $variable i see an empty value returned.

Please help on this. My aim is if some problem happened the script will be started in middle so if they start script 2 it should aware of the variable which was set in main script.

Thanks
0
Comment
Question by:magento
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
Comment Utility
You would need to source the script that defined $variable from the command line in order to use $variable in the command line.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
Comment Utility
"export" makes a variable available to subshells of the shell where it was issued, but never to the parent of that shell.

That's a security measure to avoid that a script could modify the environment of your login shell.
As ozo said, you must source the script which exports the variable to make it available to the current shell (= the parent of the script containing "export"):

. scriptname # note it's  "dot space" on the left!)

or (only in bash)

source scriptname
0
 
LVL 5

Author Comment

by:magento
Comment Utility
say script1.sh has a variable named code_version . This is the input to script2.sh

Sometimes the system will fail for unknown causes ..at that time, script 1.sh completed so we will start script2.sh . At that time the variable input will not be available.

Can you advice me how to solve this issue ?

Script1.sh
export $(code_version=1.7)

script2.sh
if [ $code_version == 1.7 ] then ...

Thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
. Script1.sh

Please note that the script must not contain "exit"! This would terminate the calling shell (probably your login shell)!

By the way, what's this strange syntax "$(code_version=1.7)" ??
This will definitely not work (at least not in the shells I use to use).

Correct syntax is simply:

export code_version="1.7"
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
If you want variables to be seen in other scripts, your best bet is to write the valeus to a file, as:

script1.sh
  code_version=1.7
  echo $code_version > /tmp/code_ver.txt

script2.sh
   code_ver=$(cat /tmp/code_ver.txt)
   echo Code version is $code_ver

Note that the variable name does not have to be the same in both scripts.


An alternative is to write the definition of the variable to the file, as

script1.sh
  echo export code_version=$code_version > /tmp/code_ver.txt

then source that file in the second script to define the value

script2.sh
  . /tmp/code_ver.txt

In this case, the variable name in script2.sh will be whatever you wrote to /tmp/code_ver.txt.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
If you want the input to script2.sh to come from script1.sh,
you can pipe the input;
script1.sh | script2.sh
with
cat > script1.sh <<< 'echo 1.7'
cat > script2.sh <<< 'read code_ver; echo $code_ver'
or pass an arg:
script2.sh `script1.sh`
with
cat > script2.sh <<< 'echo $1'
or call script2 from script1
with
cat > script1.sh <<< 'script2.sh 1.7'
or
cat > script1.sh <<< 'echo 1.7 | script2.sh
or
cat > script1.sh <<< 'export code_ver=1.7; script2.sh'
or call script1 from script2
with
cat > script2.sh <<< 'code_ver=`script1.sh`; echo $code_ver'
or with
cat > script1.sh <<< 'code_ver=1.7'
cat > script2.sh <<< '. script1.sh; echo $code_ver'
or with
cat > script1.sh <<< 'exit 17'
cat > script2.sh <<< 'script1.sh; echo $?'
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Just remember that sourcing a shell script updates your current shell environment, possibly affecting anything you do afterwards (e.g. adding entries to the PATH, changing current directory, creating function names that silently overlay commands and so on).

It will probbaly be fine for you, but is something to bear in mind.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to start Neon 20 49
Linux "time" command output redirection 16 71
Sendmail STARTTLS error 37 38
linux ssh 4 26
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now