我想在批处理脚本中“包含”一个数据文件。让我向您解释一下我将如何在 Unix shell 脚本中执行此操作,这样就不用怀疑我在批处理脚本中要实现的目标。
#!/bin/bash
. data.txt # Load a data file.
# Sourcing a file (dot-command) imports code into the script, appending to the script
# same effect as the #include directive in a C program).
# The net result is the same as if the "sourced" lines of code were physically present in the body of the script.
# This is useful in situations when multiple scripts use a common data file or function library.
# Now, reference some data from that file.
echo "variable1 (from data.txt) = $variable1"
echo "variable3 (from data.txt) = $variable3"
这是data.txt:
# This is a data file loaded by a script.
# Files of this type may contain variables, functions, etc.
# It may be loaded with a 'source' or '.' command by a shell script.
# Let's initialize some variables.
variable1=22
variable2=474
variable3=5
variable4=97
message1="Hello, how are you?"
message2="Enough for now. Goodbye."
在批处理脚本中,我的意图是在 data.txt 中设置环境变量,并在后面创建的每个批处理脚本中“源”该文件。这也将帮助我通过仅修改一个文件(data.txt)而不是修改多个批处理脚本来更改环境变量。有任何想法吗?