将文本文件中的变量加载到 bash 脚本中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2652753/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:11:42  来源:igfitidea点击:

Loading variables from a text file into bash script

bash

提问by S1syphus

Is it possible to load new lines from a text file to variables in bash?

是否可以将文本文件中的新行加载到 bash 中的变量?

Text file looks like?

文本文件长什么样?

EXAMPLEfoo 
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4

Variables become

变量变成

 = EXAMPLEfoo 
 = EXAMPLEbar 

ans so on?

等等?

回答by ghostdog74

$ s=$(<file)
$ set -- $s
$ echo 
EXAMPLEfoo
$ echo 
EXAMPLEbar
$ echo $@
EXAMPLEfoo EXAMPLEbar EXAMPLE1 EXAMPLE2 EXAMPLE3 EXAMPLE4

I would improve the above by getting rid of temporary variable s:

我将通过摆脱临时变量 s 来改进上述内容:

$ set -- $(<file)

And if you have as input a file like this

如果你有这样的输入文件

variable1 = value
variable2 = value

You can use following construct to get named variables.

您可以使用以下构造来获取命名变量。

input=`cat filename|grep -v "^#"|grep "\c"`
set -- $input

while [  ]
 do
  eval =
  shift 3
 done

回答by Андрей Костенко

cat somefile.txt| xargs bash_command.sh

bash_command.sh will receive these lines as arguments

bash_command.sh 将接收这些行作为参数

回答by Paused until further notice.

saveIFS="$IFS"
IFS=$'\n'
array=($(<file))
IFS="$saveIFS"
echo ${array[0]}    # output: EXAMPLEfoo 
echo ${array[1]}    # output: EXAMPLEbar
for i in "${array[@]}"; do echo "$i"; done    # iterate over the array

Edit:

编辑:

The loop in your pastebin has a few problems. Here it is as you've posted it:

您的 pastebin 中的循环有一些问题。这是您发布的内容:

for i in "${array[@]}"; do echo "  "AD"$count = "$i""; $((count=count+1)); done

Here it is as it should be:

这是它应该的样子:

for i in "${array[@]}"; do declare AD$count="$i"; ((count=count+1)); done

or

或者

for i in "${array[@]}"; do declare AD$count="$i"; ((count++)); done

But why not use the array directly? You could call it AD instead of array and instead of accessing a variable called "AD4" you'd access an array element "${AD[4]}".

但是为什么不直接使用数组呢?您可以将其称为 AD 而不是数组,而不是访问名为“AD4”的变量,而是访问数组元素“${AD[4]}”。

echo "${AD[4]}"
if [[ ${AD[9]} == "EXAMPLE value" ]]; then do_something; fi

回答by AnrDaemon

None of the above will work, if your values are quoted with spaces.

如果您的值用空格引用,则上述方法均无效。

However, not everythinf is lost.

然而,并不是所有的东西都丢失了。

Try this:

尝试这个:

eval "$(VBoxManage showvminfo "$VMname" --details --machinereadable | egrep "^(name|UUID|CfgFile|VMState)")"
echo "$name {$UUID} $VMState ($VMStateChangeTime) CfgFile=$CfgFile"

P.S. Nothing will ever work, if your names are quoted or contain dashes. If you have something like that, as is the case with VBoxManage output ("IDE-1-0"="emptydrive" and so on), either egreponly specific values, as shown in my example, or silence the errors.

PS 如果您的名字被引用或包含破折号,则什么都不会起作用。如果你有类似的东西,就像 VBoxManage 输出的情况(“IDE-1-0”=“emptydrive”等等),要么egrep只有特定的值,如我的例子所示,要么消除错误。

However, silencing erors is always dangerous. You never know, when the next value will have unquoted "*" in it, thus you must treat values loaded this way very careful, with all due precaution.

然而,沉默错误总是危险的。你永远不知道,下一个值什么时候会有不带引号的“*”,因此你必须非常小心地对待以这种方式加载的值,并采取一切应有的预防措施。

回答by Andy

This can be done be with an array if you don't require these variables as inputs to a script. push()function lifted from the Advanced Scripting Guide

如果您不需要这些变量作为脚本的输入,则可以使用数组来完成此操作。push()高级脚本指南中提取的功能

push()            # Push item on stack.
{
if [ -z "" ]    # Nothing to push?
then
  return
fi

let "SP += 1"     # Bump stack pointer.
stack[$SP]=

return
}

The contents of /tmp/test

的内容 /tmp/test

[root@x~]# cat /tmp/test
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4

SP=0; for  i in `cat /tmp/test`; do push $i ; done

Then

然后

[root@x~]# echo ${stack[3]}
EXAMPLE1