我想在批處理腳本中「包含」一個數據文件。讓我來解釋一下,我將如何在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