It is currently Mon, 08 Aug 2022 19:01:24 GMT



 
Author Message
 Using global variables in a sh-script
Greetings,

I'm trying to write a shell script, that sets a variable, but apparently it
does this in a sub=shell, because when I try to echo the value, I get nothing.
How can I "export" the value so it can be used as a "global variable"
throughout the entire script. (same thing applies to functions in a script I
think)

=============== example ==================
#!/bin/sh

for file in /some/dir/*.txt
do ls -l $file
   cat $file | while read regel
   do naam=`echo $regel | cut -d= -f1`
      waarde=`echo $regel | cut -d= -f2`
      case $naam in
        "License ID ")    License=$waarde ;;
      esac
   done
 echo License ID = $License
done

=========================================

Regards,...
Willem Koster

(Remove the NOSPAM. from the E-mail address)
(CC's by mail are appreciated).



 Sun, 03 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script

By default a shellscript runs in its own shell. To run it in the current
shell you put a dot and a space before it like this:

. /directory/shellscript.sh

Remember when you do this that the everything that happens, happens in the
current shell. This is true for ALL variables, cd commands and so on.
Usually you use this for scripts that only sets and exports variables, and
have a separate script that actuallt does something.
(This does not seem to be a problem in your example though.)

Regards Cecilia



 Sun, 03 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script
In article <01be28e7$d438c800$90ba6...@STO9806071456.se.pnu.com>, "Cecilia Burman" <no.commercials.please.cecilia.bur...@eu.pnu.com.remove.beginning.and.end> wrote:

Hmm.. sorry, I don't think I'm getting through.
It's just 1 shellscript. No calls of external shell scripts.
I just want to be able to use a certain variable all through the script, just
like a global variable in Pascal.

That is:
- declare it in the beginning of the script,
- Give it a value somewhere in a function
     assign()
     {
       x=1
     }
(or in a while/for loop)
- and then use that value when I get back in the "main" section.

Now it seems that if a while loop is started, a subshell" is started, which
leaves no result in my variable-placeholder. (x)

Hoping this makes sense,...
Willem Koster



 Sun, 03 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script

If i understand you correctly here, it looks like you've found an old silly
error in the standard bourne shell ;).  It is not possible to retain the
contents from variables from inside a 'while' loop.  It works in any other
construct, i.e. 'for' loops.  It works in ksh and bash though.

Example:
:
x=1
cat /etc/hosts | while read lin
do
  # Stuff
  x=5
done
echo $x

from sh this script produces '1'.  From ksh and bash, it produces '5'.

Hope this helps

Regards



 Sun, 03 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script

|> >Greetings,
|> >
|> >I'm trying to write a shell script, that sets a variable, but apparently it
|> >does this in a sub=shell, because when I try to echo the value, I get
|> nothing.
|> >How can I "export" the value so it can be used as a "global variable"
|> >throughout the entire script. (same thing applies to functions in a script
|> I
|> >think)
|> >
|> >=============== example ==================
|> >#!/bin/sh
|> >
|> >for file in /some/dir/*.txt
|> >do ls -l $file
|> >   cat $file | while read regel
|> >   do naam=`echo $regel | cut -d= -f1`
|> >      waarde=`echo $regel | cut -d= -f2`
|> >      case $naam in
|> >        "License ID ")    License=$waarde ;;
|> >      esac
|> >   done
|> > echo License ID = $License
|> >done
|> >
|> >=========================================
|>
|>
|> If i understand you correctly here, it looks like you've found an old silly
|> error in the standard bourne shell ;).  It is not possible to retain the
|> contents from variables from inside a 'while' loop.

This has nothing to do with while.  It's simply the fact that the
components of a pipeline are executed in subshells.

|> It works in any other
|> construct, i.e. 'for' loops.  It works in ksh and bash though.
|>
|> Example:
|> :
|> x=1
|> cat /etc/hosts | while read lin
|> do
|>   # Stuff
|>   x=5
|> done
|> echo $x
|>
|> from sh this script produces '1'.  From ksh and bash, it produces '5'.

Did you really try it?

--
Andreas Schwab                                      "And now for something
sch...@issan.cs.uni-dortmund.de                      completely different"
sch...@gnu.org



 Mon, 04 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script

1. Yes
Here's my test result:

My script was tested on the following platforms:

                                                                    sh
ksh
Dec OSF1 (Digital Unix)  V3.2 41            1                5
Solaris V5.4                                                1
5
Hp HP0905                                                 1                5
ScoUnix 3.2v4.2                                         1                5
ScoUnix 3.2v5.0.2                                      1                5
Aixuq 4.3.2                                                  1
5

:)

2.
Looks like you are right though.  In std bourne-shell, components are
executed in subshells, but in ksh and bash, they are not.

Thus:

x=1
echo something | x=5
echo $x

gives the result '1' in sh, and '5' in ksh, thus:
echo something | x=5            in sh equals:
echo something | (x=5)          in ksh.

Sorry about my wrong conclusion about the 'while' statement!



 Mon, 04 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script

for file in /some/dir/*.txt
do
    ls -l "$file"
    exec 3<&0 0<"$file"
    while read regel
    do
        naam=`echo "$regel" | cut -d= -f1`
        waarde=`echo "$regel" | cut -d= -f2`
        case "$naam" in
            "License ID ") License=$waarde;
        esac
    done
    exec 0<&3 3<&-
    echo License ID = "$License"
done

Heiner
--
  --------------------------------------------------------------
 / Heiner Steven / hei...@darwin.noris.de                     /
/ Heiner's SHELLdorado: http://www.oase-shareware.org/shell  /
-------------------------------------------------------------
ZZW:q!^X^C^DYES^M^JQXexit^Mquit^M^C^C^Z^?^Qq^[xxxalles kacke



 Mon, 04 Jun 2001 03:00:00 GMT   
 Using global variables in a sh-script
Yes,
use ksh instead of sh.
or store it in a file.
rgds
dave
--
+-----------------------------+----------------+
| ple...@do.not s...@me.spam  |...@mindpsring.com |
| iw...@not.buy.from.spammers |davegrantier    |
+-----------------------------+----------------+

Willem Koster <W.Kos...@NOSPAM.rc.rug.nl> wrote in article
<75816e$6s...@info.service.rug.nl>...



 Fri, 15 Jun 2001 03:00:00 GMT   
 
   [ 8 post ] 

Similar Threads

1. file contents into variable, or global variable from shell script

2. why global variable is lost in sh ?

3. using the global variable "errno"

4. Scope of variables in sh scripts?

5. awk and sh variables in a shell script

6. awk and sh variables in a shell script

7. Expansion of variables in sh scripts

8. Running scripts using variables in a parent script

9. weird piping and variables in sh script

10. sh script & variable quoting/substitution problem


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software