Two shell scripts using same variable - linux

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

Related

Pass all environment variables to sudo'ed script

I have one shell script that calls another using sudo, e.g:
test1.sh:
#! /bin/sh
echo "In test1"
UVAR=bar
echo "UVAR=${UVAR}"
sudo ./test2.sh
echo "In test1 - after call to test2"
echo "UVAR=${UVAR}"
and test2.sh:
#! /bin/sh
echo "In test2"
whoami
echo "UVAR=${UVAR}"
UVAR=baz
echo "UVAR=${UVAR}"
echo "leaving test2"
I can make single variables from test1.sh (e.g. UVAR) available in test2.sh by putting UVAR=$UVAR in front of the call to sudo, but what if I want it to be for all variables? Explicitly naming all variables in the call to test2.sh becomes unpractical (especially as I don't have direct control over which variables might be present in practice). If I didn't call test2.sh with sudo (it's needed in the real problem) I could just source test2.sh.
An extra bonus is a way that allows me to protect test1.sh against changes to the variables made in test2.sh, i.e. in the example the second line outputting UVAR in test1.sh should give the same result as the first.
(I'm tagging this with bash, as the scripts use bash at the moment, but if it can be solved with some other shell, I'll be interested to know, and if only minor changes to the scripts are required could consider replacing bash with something else)

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.

Why isn't export working from bash script

Why isn't exporting a variable working in the following case:
In the following example I export the PARAM variable and set the sleep to 1000 second in order to run the script as process on the background.
#!/bin/bash
export PARAM="I AM A REAL VALUE"
sleep 1000
so I execute the script as process as the following:
/tmp/example.bash &
Now script runs as a process (I checked it with ps -ef) and from the Linux console I want to print the $PARAM as the following
echo $PARAM
but no value from PARAM variable.
Why? The export from the script isn’t exporting the value when the script process is running.
When you run /tmp/example.bash &, you set the environment in the sub-shell, but that does not affect the parent shell that ran it.
You need to (a) remove the sleep 1000 and (b) use the . command or (in Bash or a C shell) the source command to read the file as part of the current process:
sed -i.bak '/sleep/d' /tmp/example.bash # GNU or BSD sed
. /tmp/example.bash
echo $PARAM

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

Resources