我想在批处理脚本中“包含”一个数据文件。让我来解释一下,我将如何在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)而不是修改多个批处理脚本来更改环境变量。有任何想法吗?
+1。谢谢。是否可以在data.bat中定义函数,以便这些函数可用于调用data.bat的脚本? – 2012-04-22 15:34:23
@SachinS:见我的答案中的附录... – Aacini 2012-04-25 02:32:11
谢谢。这真的很好。 – 2012-04-26 13:16:28