bash Shell脚本从txt文件读取变量?

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

Shell script read variable from txt file?

bashshell

提问by jxn

i have a text file that looks like this:

我有一个看起来像这样的文本文件:

test.txt:

测试.txt:

date="2015-01-02"
week="3"

I also have a shell script that takes in the variable form the above text file:

我还有一个 shell 脚本,它采用上述文本文件的变量形式:

test.sh

测试文件

#!/bin/sh
source test.txt
echo $week

When i run ./test.sh, i get this error: week: command not found

当我运行时./test.sh,我收到此错误: week: command not found

I thought i could follow this elegant solution here: https://askubuntu.com/questions/367136/how-do-i-read-a-variable-from-a-file

我想我可以在这里遵循这个优雅的解决方案:https: //askubuntu.com/questions/367136/how-do-i-read-a-variable-from-a-file

but it seems like that only works for bash script.

但它似乎只适用于 bash 脚本。

How do i do it for shell script?

我如何为shell脚本做这件事?

回答by jgreve

I think your "source" statement may be having a path issue. Consider the following output:

我认为您的“来源”声明可能存在路径问题。考虑以下输出:

I added a few commands, like ps -f (to convince myself it really was running in sh, not bash). And some echos to indicated what is going on.

我添加了一些命令,比如 ps -f(为了说服自己它确实在 sh 中运行,而不是在 bash 中运行)。还有一些回声表明发生了什么。

(as an after thought, I also tried it with "xyz.txt" instead of "bar.sh", still same behavior.)

(作为事后的想法,我也用“xyz.txt”而不是“bar.sh”尝试了它,仍然是相同的行为。)

output

输出

sh-4.1$ ./foo.sh
--- begin ps ---
UID        PID  PPID  C STIME TTY          TIME CMD
userabc   3352  3350  0 18:05 pts/0    00:00:00 /bin/bash
userabc   3523  3352  0 18:09 pts/0    00:00:00 sh
userabc   3778  3523  0 18:18 pts/0    00:00:00 /bin/sh ./foo.sh
userabc   3779  3778  0 18:18 pts/0    00:00:00 ps -f
--- end ps ---
Trying just 'bar.sh'
./foo.sh: line 8: source: bar.sh: file not found
week=
date=
Now trying './bar.sh' (note the leading ./ )
week=3
date=2015-01-02
Done.
sh-4.1$ 

The big difference is the two source statements; looks like source needs a qualified path. Usually I would just say . ./bar.shbut that is more or less habit because I always use ./whatever.sh to invoke a given shell script.

最大的区别是两个源语句;看起来源需要一个合格的路径。通常我只会说。./bar.sh但这或多或少是习惯,因为我总是使用 ./whatever.sh 来调用给定的 shell 脚本。

Test scripts follow...

测试脚本如下...

foo.sh

文件名

#!/bin/sh

echo --- begin ps ---
ps -f
echo --- end ps ---

echo "Trying just 'bar.sh'"
source bar.sh
echo week=$week
echo date=$date


echo "Now trying './bar.sh' (note the leading ./ )"
source ./bar.sh
echo week=$week
echo date=$date

echo Done.

bar.sh

酒吧.sh

date="2015-01-02"
week="3"

回答by fcnorman

Your test.txt is fine.

你的 test.txt 没问题。

For test.sh, change it to:

对于 test.sh,将其更改为:

#!/bin/bash
source ./test.txt
echo $week

Output is: 3

输出为:3