bash Bash从文件中读取并存储到MATLAB中的变量

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

Bash read from file and store to variables in MATLAB

bashmatlab

提问by user3654549

I'm trying to read 2 floating numbers from a very simple 1-line file in Bash. I want to store these two numbers into variables. All the examples I see from Googling look like:

我正在尝试从 Bash 中的一个非常简单的 1 行文件中读取 2 个浮点数。我想将这两个数字存储到变量中。我从谷歌上看到的所有例子看起来都像:

while read VAR1 VAR2
do
   <command>
done < file.txt

But this keeps VAR1and VAR2inside the whileloop only. How can I store the two variables so that I can use them anywhere in my script? Thanks so much!

但是,这可防止VAR1VAR2while唯一循环。如何存储这两个变量以便我可以在脚本中的任何位置使用它们?非常感谢!

回答by dvhh

The whileloop is superfluous when reading a file with a single line (as described in your question ).

while使用单行读取文件时,循环是多余的(如您的问题所述)。

why not simply do :

为什么不简单地做:

read VAR1 VAR2 < file.txt
echo $VAR1
echo $VAR2

This would read the first line of your file

这将读取文件的第一行

回答by Red Cricket

try this ...

尝试这个 ...

#!/bin/bash

var1="unknown"
var2="unknown"

while read VAR1 VAR2
do
        var1=$VAR1
        var2=$VAR2
done < file.txt
echo "v1=$var1 v2=$var2"

Update: I think dvhh's answer is the correct one.

更新:我认为 dvhh 的答案是正确的。

回答by mklement0

dvhh's answercontains the crucial pointer:

dvhh 的答案包含关键指针:

If you're reading only a singleline, there's no reason to use a whileloopat all.

如果你正在阅读只是一个单一的线,有没有理由使用一个while循环的。

Your whileloop leaves $VAR1and $VAR2empty, because the lastattempt to readfrom the input file invariably failsdue to having reached EOF, causing the variables to be set to the empty string.

您的while循环离开$VAR1并为$VAR2空,因为最后一次尝试read从输入文件中总是失败,因为已达到EOF,导致变量设置为空 string

If, by contrast, you truly needed to read values from multiplelines and needed to access them after the loop, you could use a bash array:

相比之下,如果您确实需要从行读取值并需要在循环后访问它们,则可以使用 bash数组

aVar1=() aVar2=() # initialize arrays
while read var1 var2
do
   aVar1+=( "$var1")
   aVar2+=( "$var2")
done < file.txt

# ${aVar1[@]} now contains all $var1 values, 
# ${aVar2[@]} now contains all $var2 values.

Note that it's good practice not to use all-uppercase names for shellvariables so as to avoid conflicts with environmentvariables.

请注意,最好不要对shell变量使用全大写的名称,以避免与环境变量发生冲突。

回答by Soheil Hashemi


first of you should obtain your separator in your script in this my example the uid and ... separate with ":"


首先你应该在你的脚本中获得你的分隔符,在这个我的例子中,uid 和 ... 用“:”分隔

 uid=$( IFS=":" ;while read uid   REST ; do  echo $uid  ; done </etc/passwd )
passwd=$( IFS=":" ;while read uid passwd   REST ; do   echo $passwd  ; done </etc/passwd )

IFS is internal field separator and default is white space and you should change it you can put script in varibale=$( script ) very good thing for scripting

IFS 是内部字段分隔符,默认为空格,您应该更改它,您可以将脚本放在变量中=$( script ) 非常适合脚本编写

you can see with

你可以看到

 echo $uid
echo $passwd