Problems setting or exporting variable when using multiple commands from same line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems setting or exporting variable when using multiple commands from same line.
# 1  
Old 10-25-2012
Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it.

I cannot get one of the variables to display back in the main calling script when I use a command form that sources the scripts from the same line:

Code:
. $MYROOT/junk && $MYROOT/morejunk

However, it works like this:

Code:
. $MYROOT/junk && . $MYROOT/morejunk

It also works like this:

Code:
. $MYROOT/junk && \
       . $MYROOT/morejunk

Maybe you must source each individual script. But, I used as a base for these experiments a shell that used this form, which does not source the individual commands:

Code:
   . $MYROOT/scripts/script1 && \
        $MYROOT/scripts/script2 && \
        [ -n "$MYDIR" ] && cd $MYDIR

and it works fine.

My version does execute but there are issues with the second variable I try to display. It seems to not be getting exported so that it can display properly.

Here are the three simple scripts followed by the output. Any insight would be appreciated. (BTW - I am not a programmer by trade but am rather trying to learn some stuff here.)

mytest script:

Code:
#!/bin/sh

if [ -z "$ZSH_NAME" ] && [ "x$0" = "x./my-test" ]; then
   echo "Error: This script needs to be sourced. Please run as '. ./my-test'"
else
   if [ -n "$BASH_SOURCE" ]; then
      MYROOT="`dirname $BASH_SOURCE`"
   else
      MYROOT="`pwd`"
   fi
   echo MYROOT = $MYROOT
   MYROOT=`readlink -f "$MYROOT"`
   echo MYROOT = $MYROOT
   export MYROOT

   . $MYROOT/junk && \
          $MYROOT/morejunk

   echo JUNK_VAR = $JUNK_VAR
   echo MOREJUNK_VAR = $MOREJUNK_VAR
   unset MYROOT
   unset JUNK_VAR
   unset MOREJUNK_VAR
fi

junk script

Code:
#!/bin/sh

echo "********************"
echo "* Executing junk   *"
echo "********************"

JUNK_VAR="junk_var"

export JUNK_VAR

morejunk script:

Code:
#!/bin/sh

echo "************************"
echo "* Executing morejunk   *"
echo "************************"

MOREJUNK_VAR="morejunk_var"

export MOREJUNK_VAR

Here is the output:

Code:
$ source my-test
MYROOT = .
MYROOT = /home/scottrif/test
********************
* Executing junk   *
********************
************************
* Executing morejunk   *
************************
JUNK_VAR = junk_var
MOREJUNK_VAR =
$

# 2  
Old 10-25-2012
Quote:
Originally Posted by scottrif
I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it.
You must source them. Nothing else will work.

When you source it, it runs inside your current shell, exporting variables inside it for you to use.

When you run it directly, it executes in a new shell. Variables are set and exported in the new shell, which dies uselessly immediately thereafter, taking all its variables with it, because variables don't travel between parent and child. Children get copies of anything you've exported, but are independent thereafter.
# 3  
Old 10-25-2012
Corona688,

Thanks very much for the explanation. This is subtle behaviour I of which I was not aware. I looked further into the scripts from which I based my test on that "seemed" to work and found that the second script (which was not sourced) does not do anything with variables that need to exist outside of the second temporary shell. That explains that!

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Csh - how to combine multiple commands in one line

Hey everyone, I am working in an environment where the different users can use ksh or csh. My situation is that I need the same result with one single command line. I am searching for the real path the file is in. My ksh input and output ts2:ts2adm> cd $(dirname $(which sapcontrol)); pwd -P... (2 Replies)
Discussion started by: h1kelds
2 Replies

2. Shell Programming and Scripting

Setting a variable in a stream of commands

can someone help me with this? AgentDX=/tmp/tesfinal && cat final.enc | openssl aes-128-cbc -a -d -salt -k hello | sh the content of final.enc is a code. and in that code, the value of AgentDX is needed. however, i cant seem to find a way to allow the code to recognize that the variable... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

4. UNIX for Dummies Questions & Answers

Multiple Commands on a Single Line

Hi There, I have a cronjob that executes a small script (few lines) that I am certain can be achieved in a single line. The functional objective is actually really simple; cmd var1 The '1' in 'var1' is actually derived from date (day of month) but the snag is when working with 1-9 I... (3 Replies)
Discussion started by: Random79
3 Replies

5. Shell Programming and Scripting

Export command variable exporting problem

I have a txt file from which i am assiging a value to a variable using the code in script1 script1.sh export f=$(sed -n "/Freq *=/ s/.*= *//p" ${R_path}/output.txt) echo "$f" --------> this works in script2 ( which executes the script1) eval ./script1.sh if && ; then echo... (1 Reply)
Discussion started by: shashi792
1 Replies

6. Shell Programming and Scripting

exporting variable

Hi All; I m working on a script and came across an issue ,to explain it briefly here is the sample code echo $HOSTNAME|while read IN; do var=`echo $IN|awk -F "-" '{print $2}'`; export var; echo $var; done now I get the value of $var but when it is out of the while loop it does not return... (3 Replies)
Discussion started by: maverick_here
3 Replies

7. Shell Programming and Scripting

Exporting my dynamical variable won't work?

Even though the idea "might" not be great I still wrote this piece of code to get practice.. Which means that it is the CODE that matters here. Anyways; The intension is to create a program(or do we call it script?) that searches recursively through a folder to find a file - stored in a... (4 Replies)
Discussion started by: Pesk
4 Replies

8. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies

9. Solaris

variable exporting

Hi, can anyone tell me the difference between the below two examples: Eg-1: # name=bravo # echo $bravo what would be the o/p Eg-2: # name1=jhonny # export name1 # echo $name1 what would be the o/p If the o/p's of both examples are the same then what is the use of the cmd export... (3 Replies)
Discussion started by: rahul_11d
3 Replies

10. Shell Programming and Scripting

Exporting variable from config file

Hi, I am exporting the environment variable from config file, but when I echo the variable it does not display any value. Here is the snippet of the code #!/bin/sh export ENVIRONMENT_ROOT_DIRECTORY="/cb/$ENVIRONMENT" echo $ENVIRONMENT_ROOT_DIRECTORY ${JAVA_HOME}/bin/java... (2 Replies)
Discussion started by: bhavnabakshi
2 Replies
Login or Register to Ask a Question