如何将终端中定义的变量读取到 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43294150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Read a variable in defined in terminal to a bash script
提问by samuel koduri
I am new to shell scripting. I have a very basic question about how we read variables defined in our terminal as input to a shell scirpt.
我是 shell 脚本的新手。我有一个非常基本的问题,关于我们如何读取终端中定义的变量作为 shell 脚本的输入。
let us say i defined this variable in my terminal
a=22
if i do echo $a
it gives 22 as output in my terminal.
假设我在终端
a=22 中定义了这个变量,
如果我echo $a
在终端中给出 22 作为输出。
I wanted to pass this variable as a parameter to a script named input.sh
我想将此变量作为参数传递给名为 input.sh 的脚本
#!/bin/bash
echo "Enter variable name:"
read Input
echo $Input
I ran the scirpt as ./input.sh It popped up the message as
Enter variable name:$a
我将脚本运行为 ./input.sh 它弹出的消息为
Enter variable name:$a
But in the output i have $a as output not 22. I wanted 22 as output. Is there a way to do this?
但是在输出中我有 $a 作为输出而不是 22。我想要 22 作为输出。有没有办法做到这一点?
回答by Fred
You are mixing several things here. Where to start...
你在这里混合了几件事。从哪儿开始...
The usual way to pass values to a script is through positional parameters.
将值传递给脚本的常用方法是通过位置参数。
Suppose you have this script, called s1
:
假设你有这个脚本,叫做s1
:
#!/bin/bash
echo "First two args are: "
If you execute it like this :
如果你像这样执行它:
./s1 Arg1 Arg2
You will see the following output :
您将看到以下输出:
First two args are: Arg1 Arg2
If you want to pass a variable name to the script, and have that script output the value of this variable, then you must do two things. First, initialize the variable and export
it so that it can be seen by the children processes (including the script you will call, which is a separate process unless called with source
or .
).
如果您想将变量名传递给脚本,并让该脚本输出该变量的值,那么您必须做两件事。首先,初始化变量和export
它,以便子进程可以看到它(包括您将调用的脚本,除非使用source
或调用,否则这是一个单独的进程.
)。
VAR="Some value"
export VAR
You can also do both in a single statement:
您也可以在一个语句中同时执行这两项操作:
export VAR="Some value"
Then, adapt the script to perform an indirect access to the variable :
然后,调整脚本以执行对变量的间接访问:
#!/bin/bash
echo "Value of variable named : ${!1}"
Please note that while $1
means "the content of variable 1", ${!1}
means "the content of the variable that is named $1
". This is the indirect part.
请注意,while 的$1
意思是“变量 1 的内容”,${!1}
意思是“被命名的变量的内容$1
”。这是间接部分。
Now, if you want to take it a step further, and allow the script to interactively read user input (not an argument) and use the value read as a variable name to expand, you would do something like this :
现在,如果您想更进一步,并允许脚本以交互方式读取用户输入(不是参数)并使用读取的值作为变量名进行扩展,您可以执行以下操作:
#!/bin/bash
echo "Please enter a variable name"
read VARNAME
echo "Value of variable named $VARNAME : ${!VARNAME}"
Using positional parameters makes the script easier to reuse in non-interactive scenarios, so reading user input should be limited to cases where it is necessary.
使用位置参数使脚本更容易在非交互式场景中重用,因此读取用户输入应仅限于必要的情况。
The above is to help understand the basics. If you move beyond toy scripts, you will need to understand the security implications of indirect access (especially if you allow user input). You will also need to validate positional parameters or user input are valid for your purpose (i.e. contains a valid variable name) so that you may have your script react appropriately. Well, you would probably need to check if positional parameters were even provided to begin with. All of this is doable in shell scripting, but is beyond the scope of a single question. In any case, checking input (and also errors) will be required if you intend to have robust scripts in situations where reliability is expected.
以上是为了帮助理解基础知识。如果您超越了玩具脚本,您将需要了解间接访问的安全含义(尤其是在您允许用户输入的情况下)。您还需要验证位置参数或用户输入是否符合您的目的(即包含有效的变量名称),以便您的脚本做出适当的反应。好吧,您可能需要检查是否甚至提供了位置参数。所有这些在 shell 脚本中都是可行的,但超出了单个问题的范围。在任何情况下,如果您打算在期望可靠性的情况下拥有健壮的脚本,则需要检查输入(以及错误)。
回答by user12340
To use the variables defined in the terminal in your bash script -
要在 bash 脚本中使用终端中定义的变量 -
Change last line of your script so that it looks like -
#!/bin/bash echo "Enter variable name:" read Input echo ${!Input}
Run your script input.sh on the terminal as -
. input.sh
And finally, when you input the variable name, do not use $ sign. For e.g. -
6c4008a16b7c:~ z001lg8$ . input.sh Enter variable name: a 22 6c4008a16b7c:~ z001lg8$
更改脚本的最后一行,使其看起来像 -
#!/bin/bash echo "Enter variable name:" read Input echo ${!Input}
在终端上运行脚本 input.sh 作为 -
. input.sh
最后,当你输入变量名时,不要使用 $ 符号。例如——
6c4008a16b7c:~ z001lg8$ . input.sh Enter variable name: a 22 6c4008a16b7c:~ z001lg8$
Voila, you can now use the variables defined in the terminal in your script.
瞧,您现在可以在脚本中使用终端中定义的变量。
Explanation -
解释 -
In your script, $Input is changed to ${!Input} so that the content of the user input(which is variable name) is echoed and not the variable name itself. As explained by @Fred - $1 means "the content of variable 1", ${!1} means "the content of the variable that is named $1".
在您的脚本中, $Input 更改为 ${!Input} 以便回显用户输入的内容(即变量名称)而不是变量名称本身。正如@Fred 所解释的 - $1 表示“变量 1 的内容”,${!1} 表示“名为 $1 的变量的内容”。
When script is run as . input.sh
, it means that you are sourcing the script contents on the terminal. The .
symbol is used for sourcing a command/script.
当脚本作为 运行时. input.sh
,这意味着您正在终端上获取脚本内容。该.
符号用于获取命令/脚本。
The $
sign is not required when the variable name is entered in terminal because ${!Input}
format already takes into account that the value in Input
variable is a variable name.
在$
当在终端上输入变量名,因为不需要签${!Input}
格式已经考虑到的是,在价值Input
变量是一个变量名。