Question : How to communicate variables back to calling script not using a file

Hello,
I'm trying to do something in a bash script that I've done in windows where a called script sets some variables which are then used by the caller. An example of such a script would be the case where a called script prompts for user name and password and the entered values are then used by the caller.

So script1.sh would call script2.sh and use some variables that script2.sh sets.
I don't want to use a file for this because there are passwords involved.
I'd prefer to use locally scoped environment variables (I'm not sure of Unix terminology here)
I've included an example of how I'd do this in Windows.

Thank you.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Windows example:
 
Contents of script1.bat
 
@SETLOCAL
@SET _name=script1.bat
@CALL script2.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
@ECHO The user is %_user% and the password is %_password%
 
Contents of script2.bat
 
@ECHO script2.bat was called from %_name%
@SET /P _user=User name?
@SET /P _password=Password?
 
Example of running script1
 
>script1
script2.bat was called from script1.bat
User name?TheUser
Password?""
The user is TheUser and the password is ""
 
(Intentionally entered two quotes for password)
Open in New Window Select All

Answer : How to communicate variables back to calling script not using a file

--> script1.bat  <==
#!/bin/bash
source script2.bat $0 $*
echo The user is $_user and the password is $_password

==> script2.bat
echo script2.bat was called from $1
read -p "User name?" _user
read -p "User name?" _password



 


Random Solutions  
 
programming4us programming4us