Page 1 of 1

Shell variable values are transient when used from script! [solved!]

Posted: Mon Aug 31, 2020 9:22 am
by cobaka
Hello Puppians! Woof!:

I asked this Q before, got an answer on the murga forum but can't find it.

I want to learn how to use variables in a script.
The problem: I assign a value to a variable and when I use the variable inside the script: bingo! The variable works. (example shown below)
When I use the same variable from the keyboard the variable is 'gone'.

cobaka.

Examples below:
First - the script. I call it vdf.sh (variable definition file - shell script)
#!/bin/sh

# file name: variable definition file, vdf.sh
# contains variable definitions used when grepping and awking text transcript.
# file location /root/my-applications/bin.

echo 'this script defines string variables: '
sdpa=/root/my-applications/bin # script directory path
echo 'printing script directory path:'
echo $sdpa
echo
echo 'and now: I define the path to transcript text folder'
ttPa=/mnt/sda1/Law/bNgo/transcript/text
ls $ttPa
cd $ttPa; echo
pwd
echo '(end of script file).'
I run the script. You can see it work:
# vdf.sh
defining variables from a script file:
printing script directory path:
/root/my-applications/bin

and now: I define the path to transcript text folder
1t-text a_ht_grep.txt int-afm.txt tmp_al3t.txt wit-1t.txt
2t-text cm-text nq-text wd.txt wit_list_cm.txt
3t-text --color pt-text wit-1tB.txt wtbc-1tB.txt

/mnt/sda1/Law/bNgo/transcript/text
(end of script file).
When I list the folder (or directory) from the keyboard I get a completely different result. I see the content of the root directory.
# ls $ttPa
95-99 Downloads my-applications scr V_16-26.mp4 wla.txt
95-99-tmp exhi-a my-documents spot V_16-29+.mp4 wl.txt
bin ex-tmp network Startup V_16-47.mp4
Choices File-Sharing one_GiB Templates V_16.mp4
d35+99 ftpd ot-fil tmpp V_IG.mp4
Desktop grefil puppy-reference V_15-54.mp4 Web-Server
# pwd
/root
#
OMG! The value of the string has changed! Life isn't worth living!
Clearly, the defined variable has a transient life. It exists only until the end-of-shell file marker.
PLEASE HELP ME UNDERSTAND WHY THIS IS SO. I know there reason for this and a solution.

cobaka

Re: Shell variable values are transient when used from script! [solved]

Posted: Mon Aug 31, 2020 10:19 am
by cobaka
Hello all:

I think the answer is found here:
https://www.howtogeek.com/442332/how-to ... s-in-bash/
How to Export Variables

When a script runs, it’s in its own process, and the variables it uses cannot be seen outside of that process. If you want to share a variable with another script that your script launches, you have to export that variable.
I haven't confirmed this to be the solution to my problem, but I have confidence that it is.
Thanks to all
cobaka.

Re: Shell variable values are transient when used from script! Pls help

Posted: Mon Aug 31, 2020 11:04 am
by cobaka
Hello Script writers:
Contrary to my previous posting, the problem remains unsolved.
I refer to the following reference: https://tldp.org/LDP/Bash-Beginners-Gui ... 03_02.html

This deals with the export command and variable definition:
I am trying to export Var1 so it can be used in Bash, outside the script.
The script file, t.sh:
#!/bin/sh

# file_name: t.sh - a temporary file for testing shell scripts
# file_location xx/my-applications/bin, (where xx means 'a longer path ...')

export Var1=1023;
echo 'value of Var1: ' $Var1
printf 'end of script';
exit
I run the script. The script prints the value of Var1
# t.sh
value of Var1: 1023
end of script#
At the terminal (command line) the value of Var1 is a null string:
# echo $Var1

#
Var1 is NOT exported!
Observations about what I don't understand are welcome!
cobaka.

Re: Shell variable values are transient when used from script! [solved!]

Posted: Mon Aug 31, 2020 11:38 am
by cobaka
The solution to the problem depends on identifying whether a script is a child or a parent process.
That is discussed here: https://askubuntu.com/questions/53177/b ... ot-working.

The answer given is:
The 'export' command will send the variable assignment to child processes of the shell in which the export command was ran. Your command-line environment is the parent of the script's shell, so it does not see the variable assignment.

You can use the . (or source) bash command to execute the script commands in the current shell environment and achieve what you want.
Now my script file is t.sh. When I type #. t.sh (or #source t.sh) at the command-line prompt the value is exported from the script to the keyboard shell.

Tnx to all
cobaka

Re: Shell variable values are transient when used from script! [solved!]

Posted: Sun Sep 06, 2020 10:12 pm
by mikewalsh
@cobaka :-

Les:-

I'm no expert on this stuff, and I'll be the first to admit it, an' all! However; I take it you are aware that processes/scripts, etc, that are run within a given shell are only 'active' while that shell is open, yes? I.e., you close the shell, and then re-open a new instance, everything you were doing in that previous shell has now vanished into cyberspace.....

Thus, every time you want to run a specific script, you have to re-load it all over again. With me? So if you want to re-load summat you were doing in a previous shell, you first of all have to save the process into a script/text file - whichever is appropriate - created while that shell is still open.....

At least, that's my (admittedly tenuous!) understanding of the process.


Mike. ;)

Re: Shell variable values are transient when used from script! [solved!]

Posted: Mon Sep 07, 2020 11:29 am
by user1111
Run the script with a dot (followed by a space) prefix i.e.

. /home/user/myscript.sh

If myscript.sh contains just

export v=hello

then running it without the dot will have echo $v returning a empty value.
Running it with the dot and echo $v will return 'hello'

Basically the dot prefix runs the script in the current shell rather than spawning a child shell.

EDIT: Whoops! Completely missed that you'd already discovered this and had marked the thread solved. Going crazier with age :)