Bashから渡された変数の評価

Bashから渡された変数の評価

変数をbashスクリプトにパラメータとして渡し、bashスクリプトで評価する方法はありますか?

与えられた:

# cat /path/to/file/of/host/names
bob
tom
joe
etc...


# dofor
FILE=$1
shift
CMD=$*

while read host; do
    # recursively turns "ssh \$host hostname" from $@ into:
    # ssh bob hostname
    # ssh tom hostname
    # ssh joe hostname
    # etc...
    eval $CMD
done < $FILE

いつ:

# dofor /path/to/file/of/host/names "ssh \$host hostname"

それからssh host hostname:/path/to/file/of/host/namesにリストされている各ホスト名に対して実行結果を受け取ります。たとえば、

bob.example.com
tom.example.com
joe.example.com
etc...

ベストアンサー1

この試み:

#!/bin/bash

FILE=$1
shift
CMD=$*
cmds=""

source <({
cat $FILE | while read host; do
    echo $CMD | sed -e "s|\$host|$host|g"
done
})

おすすめ記事