2016-10-23 7 views
0

エリクシール初心者はここ!私は設定ファイルに基づいてbashスクリプトを生成しようとしています。私が設定を反復し、生成する文字列を出力すると、それはすべて問題ありません。しかし、私が連結したり、リストに追加しようとすると、何も戻ってきていません!エリクシールでエリクサー生成量

def generate_post_destroy_content(node_config) do 
    hosts = create_post_destroy_string(:vmname, node_config) 
    ips = create_post_destroy_string(:ip, node_config) 
    content = "#!/bin/bash\n\n#{hosts}\n\n#{ips}" 
    IO.puts content 
end 

def create_post_destroy_string(key, node_config) do 
    # content = "" 
    content = [] 
    for address <- node_config, do: 
    # content = content <> "ssh-keygen -f ~/.ssh/known_hosts -R #{address[key]}"] 
    content = content ++ ["ssh-keygen -f ~/.ssh/known_hosts -R # {address[key]}"] 
    # IO.puts ["ssh-keygen -f ~/.ssh/known_hosts -R #{address[key]}"] 
    content = Enum.join(content, "\n") 
    content 
end 

私の出力

#!/bin/bash 




=========END========= 

答えて

1

変数は不変です。あなたのコードはforのすべての反復で新しいcontentを作成しています。

def create_post_destroy_string(key, node_config) do 
    for address <- node_config do 
    "ssh-keygen -f ~/.ssh/known_hosts -R #{address[key]}" 
    end |> Enum.join("\n") 
end 

あなただけのいくつかの条件にリストを追加するなど、より複雑な計算を行う必要がある場合や:この特定のコードのために、あなたはfordoブロックの評価値のリストを返すという事実を利用します/または複数のものを複数追加する場合は、Enum.reduce/2を使用できます。詳細については、this answerをご覧ください。

+0

ありがとう@Dogbert!これは明らかだったはずです.. – rolandvarga

関連する問題