-1

これは bash では非常に簡単なはずですが、なぜ苦労しているのかわかりません。私はbash初心者なので、優しくしてください。

擬似コード:

    read a configuration file, extract the first line beginning with a key/value pair
    in the format exec=/path/to/myprog -opt1 -opt2 $var1 $var2 ...
    check that the /path/to/myprog is executable
    if executable then
       replace $var1, ... with the contents of the same bash variables in the script
       check that all variables were replaced with existing bash variables
       if aok 
           execute the command and be happy
       else
           complain echoing the partially-substituted command string
       fi
    else
       complain echoing the un-substituted command string
    fi

私が試したことは何もうまくいかないようです。私はいろいろなことを試すのに十分な時間を費やしてきました。提案、誰か?

4

1 に答える 1

1

conf ファイル:

exec=/bin/ls -l $var1 $var2

バッシュファイル:

#!/bin/bash

CONFIG="tmp.conf"

var1=./
var2=helo

function readconf() {
    args=()
    while IFS=' ' read -ra argv; do
        exec=${argv[0]#*=}
        `command -v ${exec} >/dev/null 2>&1 || { echo >&2 "I require ${exec} but it's not installed.  Aborting."; exit 1; }`
        for i in "${argv[@]:1}"; do
            if [[ $i == \$* ]]; then
                sub=${i:1}
                args+=(${!sub})
            fi
        done
    done < $CONFIG
    echo ${args[@]}
}

readconf

上記のコードは、何を何を実装するために必要な主要コンポーネントを提供します。少なくとも私はそう思う。このスケルトンに基づいてロジックを追加できます。

次の URL が役立つ場合があります。

プログラムが Bash スクリプトから存在するかどうかを確認する

bashで、文字列が何らかの値で始まるかどうかを確認するにはどうすればよいですか?

Bashの区切り文字で文字列を分割するにはどうすればよいですか?

Bash: キーを指定せずに配列に値を追加する

変数名を変数名として使用

于 2013-03-23T22:42:03.173 に答える