Linux/Unix では、perl または awk を使用できます (どちらもほとんどのディストリビューションで標準のユーティリティです)。Python または Ruby も候補ですが、ターゲット システムにインストールされていない可能性があります。Lex と Yacc を使用して独自のターゲット パーサーを作成し、インストーラーと共に出荷することもできます。ただし、あなたのニーズからすると、それは確かにやり過ぎです。
スクリプト/バッチ ファイルの実行アクションで考えられる awk ソリューションの例を次に示します。
#!/bin/bash
awk '
# Process lines that begin with our variable name,
# preceded by optional spaces or tabs.
/^[ \t]*$TARGET_VARIABLE_NAME$=.+/ {
# Split the current line on "=" into
# an array called result
split($0, result, "=")
value = result[1]
# Look for trailing comments and remove them.
offset = index(value, "#")
if (offset > 0) {
value = substr(value, 1, offset - 1)
}
# Remove any possible leading spaces and quotes.
# Note that the single-quote is escaped. That escape
# is for bash, not for awk. I am doing this from
# memory and do not have access to IA right now.
# you may have to play with the escaping.
gsub(/^[\'" ]*/, "", value)
# Remove any possible trailing spaces and quotes.
# See above regarding the escaped single-quote.
gsub(/[\'" ]*$/, "", value)
# send "value" to stdout
print value
}
' < $SHELL_INPUT_FILE$
print value
行 (末尾近く) は stdout に送信しvalue
ます。
Execute Script/Batch File Action 設定では、スクリプト アクションによって生成された stdout および stderr ストリームを受け取る変数を指定できます。デフォルトでは、stdout ストリームは に保存され$EXECUTE_STDOUT$
ます。これを任意の変数名に変更できます。
上記の例では、$TARGET_VARIABLE_NAME$
と$SHELL_INPUT_FILE$
はそれぞれ、検索する変数の名前と解析するファイルの名前を保持する InstallAnywhere 変数です。これらの変数は、アクションが実行される前に値に置き換えられます。
/home/fred/hello.sh
次のコードを含むというスクリプトがあるとします。
#!/bin/bash
WIFE='Wilma'
NEIGHBOR="Barney Rubble"
echo "Hello to $WIFE and $NEIGHBOR from $PWD"
スクリプト/バッチ ファイルの実行アクションを実行する前に、スクリプト ファイルの名前を$SHELL_INPUT_FILE$
( /home/fred/hello.sh
) に詰め込みます。次に、 の値$TARGET_VARIABLE_NAME$
を検索したい変数 (たとえばNEIGHBOR
) に設定します。アクションが完了すると$EXECUTE_STDOUT$
、InstallAnywhere に が含まれますBarney Rubble
。
このアイデアに基づいて、スクリプトの実行/バッチ ファイル アクションで任意の複雑なファイルを解析できます。awk (または perl/Ruby/Python) スクリプトを必要なだけ複雑にします。
注: InstallAnywhereで Unix シェル スクリプトを作成する場合は、常に [不明な変数を置換しない] オプションをチェックしてください。そうしないと、InstallAnywhere は、漠然と InstallAnywhere 変数のように見えるものを静かに空白に変換します... これは非常に面倒です。
Windows ソリューションの場合は、awk または perl のスタンドアロン Windows バージョンを見つけて、インストールに含めます。次に、上記のソリューションを拡張して、バッチ ファイルで機能するようにします。
1 つは Linux/Unix 用のルールで、もう 1 つは Windows 用のルールで、2 つのスクリプト/バッチ ファイル実行アクションを作成します。ただし、このアクションを呼び出す前に、Windows の awk または perl 実行可能ファイルをインストールする必要があります。また、awk/perl 実行可能ファイルへのパスを完全修飾する必要があります。最後に、実際のスクリプトは、バッチ構文とシェル構文の違いに敏感である必要があります。
以下は、バッチ変数定義を探すように変更された awk スクリプトです。パターンが変更され、埋め込まれたコメントについて心配する必要がなくなります。
$PATH_TO_AWK_EXE$ '
# This pattern looks for optional spaces, the word SET
# with any capitalization, the target variable, more
# optional spaces and the equals sign.
/^[ \t]*[Ss][Ee][Tt][ \t]*$TARGET_VARIABLE_NAME$[ \t]*=.+/ {
# Split the current line on "=" into
# an array called result
split($0, result, "=")
value = result[1]
# No trailing comments in Batch files.
# Remove any possible leading spaces and quotes.
# Note that the single-quote is escaped. That escape
# is for bash, not for awk. I am doing this from
# memory and do not have access to IA right now.
# you may have to play with the escaping.
gsub(/^[\'" ]*/, "", value)
# Remove any possible trailing spaces and quotes.
# See above regarding the escaped single-quote.
gsub(/[\'" ]*$/, "", value)
# send "value" to stdout
print value
}
' < $SHELL_INPUT_FILE$
上記の IA 変数は$PATH_TO_AWK_EXE$
、awk がインストールされた場所を指しています。$USER_INSTALL_FOLDER$、場合によっては他のディレクトリ名、および awk.exe ファイルの名前の組み合わせとして設定されます。$PATH_TO_AWK_EXE$
必要に応じて、後で awk 実行可能ファイルを削除するために使用できます。