A
A
Alexey_Shalin2014-04-08 07:04:46
bash
Alexey_Shalin, 2014-04-08 07:04:46

How to pass a variable from one script to another in bash?

Good afternoon
Using pdmenu, I built a menu with the following items
Search User by ID:: sudo /scripts/firewall search_id
View User::sudo /scripts/firewall view_user The
menu is launched using the pdmenu command.
So, the crux of the problem is this.
When a certain user enters the menu - he calls Search User by ID finds a specific user. Accordingly, the user has an ID -> let it be 5000. The user selected this value and it was written to the client_id variable
. was taken from the first step
The option to write to a file is not suitable.
The export option fails because each time the script is run, the export environment is new.
How can you pass a variable?
FreeBSD OS

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
comAT0Zz, 2014-04-08
@comAT0Zz

Follow the thought..
1.sh source:

#!/bin/bash
MY_USER_ID=5000
./2.sh $MY_USER_ID

2.shsource:
#!/bin/bash
echo $1

A
Alexey_Shalin, 2014-04-08
@Alexey_Shalin

A little different from what I write above
There is another option
. ./1.sh
. ./2.sh
then yes, the export environment will be the same .. but I can't call the script this way from pdmenu
this is how the menu is formed

#!/usr/local/bin/pdmenu
#
# Note that the above bang-path isn't required, but it lets you run this
# file directly as a sort of pdmenu script.

# Sample menus for Pdmenu.

# Define the main menu.
menu:main:Main Menu
        show:_Search Client..::findclient
        show:_Clients...::clients
        show:_Tools...::tools
        nop
        exit:_Exit

menu:findclient:Find Client:Find Client
        exec:_Find by ID:: sudo /root/scripts_new/firewall select_id
        exec:_Find by Name::sudo /root/scripts_new/firewall select_name
        exec:_Find by IP::sudo /root/scripts_new/firewall select_ip
        exec:_Find by Pipe::sudo /root/scripts_new/firewall select_pipe
        nop
        exit:_Main menu..
menu:clients:Clients:Clients
        exec:_Add new Client:: sudo /root/scripts_new/firewall new_client
        exec:_View Client:: sudo  /root/scripts_new/firewall view_client
        exec:_Edit Client::sudo /root/scripts_new/firewall edit_client
        exec:_Delete Client::sudo /root/scripts_new/firewall delete_client
        nop
        exit:_Main menu..
menu:tools:Tools:Tools
exec:_Trafshow on em0::sudo /usr/local/bin/trafshow -i em0
exec:_Trafshow on em1::sudo /usr/local/bin/trafshow -i em1
nop
exit:_Main menu..

call from
shell pdmenu pdmenurc

J
jcmvbkbc, 2014-04-08
@jcmvbkbc

Changing the environment of the parent process will not work.
But you can do it this way: create a temporary file with settings, made in the form of a shell script, change it from called scripts and load it into running scripts at startup. For example,
the main script (starts pdmenu, which then starts 1.sh and 2.sh):

CONF=`mktemp`
export CONF
...
pdmenu pdmenurc
...
rm -f "${CONF}"

1.sh (changes var):
. "${CONF}"
...
sed -i "${CONF}" -e "/^VAR=/d"
echo "VAR=\"${new_var_value}\"" >> "${CONF}"
...

2.sh (outputs VAR modified in 1.sh):
. "${CONF}"
...
echo "${VAR}"
...

A
Alexey_Shalin, 2014-04-08
@Alexey_Shalin

did so

cat start.sh
#!/usr/local/bin/bash
export CONF=`mktemp`
pdmenu /root/scripts_new/pdmenurc
rm -f "${CONF}"

in pdmenurc
exec:_test::sudo /root/scripts_new/1.sh
        exec:_test2::sudo /root/scripts_new/2.sh

cat  /root/scripts_new/1.sh
#!/usr/local/bin/bash
. "${CONF}"
export K=1
export

/root/scripts_new/1.sh
: line 2: : No such file or directory is per line. "${CONF}"
and there is no CONF variable in 1.sh when doing export

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question