Solved

Export shell variable

Posted on 2015-02-10
7
90 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
ID: 40601136
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
ID: 40601152
"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
ID: 40601167
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40601187
. 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
ID: 40601316
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
ID: 40601420
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
ID: 40614528
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

939 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