Bash script exporting variables with if function - linux

I got some not working script about checking if the variable is empty, and based on this create the next variable and export it.
export VARIABLE=macmac123
if [ -z "$VARIBLE" ]
then
unset $VAR2
else
export VAR2="test"
fi
Next I run this script ./script.sh and then running echo $VAR2 doesn't show anything. Any ideas? I can't change the whole script, because I need this, but it doesn't export anything into environmental variables.

Shell scripts are executed in a subshell. If you want the variables in the current shell you have to source the script like: source script.sh or . script.sh (dot is an alias for source).

Related

Exporting shell variables output of this code

so basically i have a shell script below and i wanted to know what the value of x is after the code has been executed. What does the export does in shell scripting?
export x=4
bash
echo $x
x=100
echo $x
exit
echo $x
In theory echo will say [in order]:
4
100
then the script exits, but from the terminal you could enter the last echo manually and it would say
4
"export", from what i'm gathering is like the windows environmental variables...
user#machine~$export
declare -x .....
So everything that's listed from the base command in the terminal are things like $USER $LANG $HOME $SHELL, which can all be used to insert global variables into your script. For example,
#!/bin/bash
mkdir /$HOME/newdir
touch /$HOME/newdir/newfile
The commands you entered would have different outputs reliant on whether or not you typed them into your terminal or ran from script. If you ran from script x would equal the last thing you assigned it to via "export" or locally assigning a value to it via "x=3".
The script will hold the last assigned value until exit. "Export" will save the variable forever.

Exporting a global variable from child to parent in shell scripts

Is it possible to export a variable from a parent shell script to its children ?
Trying the execute the two following scripts it always returns to me 0 but I want it to return 3. I`ve also tried to export, set and add the variable error to the .bash_profile without success...
test.sh
$ cat test.sh
#!/bin/bash
error=0
./envtest.sh
echo $error
envtest.sh
$ cat envtest.sh
#!/bin/bash
source ./test.sh
test=3
error=$test
echo $error
Like #chepner commented, no.
When you invoke ./envtest.sh from within test.sh, a new process is created to run /bin/bash ./envtest.sh, and that process's environment is initialized with a copy of every environment variable from the parent process. No matter what you do inside envtest.sh, it can only impact the variables within its own environment; it cannot touch the variables in the parent's environment.

Two shell scripts using same variable

I have two shell scripts test1.sh and test2.sh.
In test1.sh I have the below statements :
In, test1.sh,I have a variable I whose value will be used by test2.sh
#!/bin/sh
I="10"
echo $I
In, test2.sh, the same value of the variable will be copied and printed
#!/bin/sh
J=$I
echo $J
I need to run both the scripts in crontab, I tried export command, but nothing worked.
Add this to you crontab :
. ./test1.sh && ./test2.sh;
And modify you test1.sh like that :
#!/bin/sh
export I="10"
echo $I
With . the first will be executed as source and will hold variables.
Both scripts are running in their own shell, and only share their environment with their parent process. If you want two separate shell scripts to share environment variables, the variables have to be set (and exported) in the parent process before calling the scripts.
You could also create a third script that only sets the variables, and source that script from the two main scripts.
If you want to use the output of test1.sh in script test2.sh, you have two options
store the output of test1.sh in a file and read that file back in test2.sh
call test1.sh from test2.sh
test2.sh:
#!/bin/sh
J=$(test1.sh)
echo $J
As #Joachim Pileborg already suggested, you can set (but not echo) variables in one script and source it in the other one
test1.sh
I="10"
J=20
K=30
test2.sh
source test1.sh
# do something with I, J, K

Why doesn't a shell get variables exported by a script run in a subshell?

I have two scripts 1.sh and 2.sh.
1.sh is as follows:
#!/bin/sh
variable="thisisit"
export variable
2.sh is as follows:
#!/bin/sh
echo $variable
According to what I read, doing like this (export) can access the variables in one shell script from another. But this is not working in my scripts.
If you are executing your files like sh 1.sh or ./1.sh Then you are executing it in a sub-shell.
If you want the changes to be made in your current shell, you could do:
. 1.sh
# OR
source 1.sh
Please consider going through the reference-documentation.
"When a script is run using source [or .] it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script."
export puts a variable in the executing shell's environment so it is passed to processes executed by the script, but not to the process calling the script or any other processes. Try executing
#!/bin/sh
FOO=bar
env | grep '^FOO='
and
#!/bin/sh
FOO=bar
export FOO
env | grep '^FOO='
to see the effect of export.
To get the variable from 1.sh to 2.sh, either call 2.sh from 1.sh, or import 1.sh in 2.sh:
#!/bin/sh
. ./1.sh
echo $variable

exported variables are not reflected in "env" output

I ran the below script to set environment variables for oracle(oracle_env.sh which comes with oracle package itself).
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
ORACLE_SID=XE
export ORACLE_SID
NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
export NLS_LANG
PATH=$ORACLE_HOME/bin:$PATH
export PATH
if [ $?LD_LIBRARY_PATH ]
then
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$ORACLE_HOME/lib
fi
export LD_LIBRARY_PATH
After that when I ran env to ensure that the variables are exported properly, I found no properties are exported(below is the output).
invincible:/home/invincible# /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
invincible:/home/invincible# env | grep ORACLE_HOME
invincible:/home/invincible#
Now I am not sure whether variables are exported properly.If not what I have done wrong? Please help me out.
And one more thing, I am running as root.
The scripts only sets the environment inside the subshell it runs in. You should source it:
# POSIX
. /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
or
# bash/ksh
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
I believe that when you run a script, bash forks and execs the script in a new shell instance, any exports done in the script doesn't propagate back to your parent shell.
However it seems that you can simply execute your script with:
prompt$ . /path/to/script.sh # note the period!
Example:
prompt$ echo "export FOO=foobar" > /tmp/tst
prompt$ sh /tmp/tst
prompt$ echo $FOO
prompt$ . /tmp/tst
prompt$ echo $FOO
foobar
I believe you should use source to load that script.
source /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
From man source:
source filename [arguments]
Read and execute commands from filename in the current shell environment and
return the exit
status of the last command executed from filename.
Exporting variables only makes them available to children of the shell you export them from. There is no way of changing the environment variables in the parent shell, as you seem to be trying to do. You can change the variables in the same shell by sourcing the script using the "dot" command:
. myscript

Resources