バッチスクリプトにデータファイルを「含め」たい。私がバッチスクリプトで何を達成しようとしているのかについて疑いの余地がないように、Unixシェルスクリプトでそれをどのように行うかについて説明しましょう。
#!/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に環境変数を設定し、後で作成する各バッチスクリプトでそのファイルを「ソース」することを目的としています。これは、複数のバッチスクリプトを変更する代わりに、1つのファイル(data.txt)を変更するだけで、環境変数を変更するのにも役立ちます。何か案は?