2017-11-08 17 views
0

私は3つの名前のリストを取り、リストのすべての組み合わせを思いつくシェルスクリプトを作成しようとしています。シェルスクリプト、3組の名前、すべての組み合わせ

リスト1
マイク
トム
ハリー
スティーブ

リスト2
デボラ
サラ
ジェニファー

リスト3
アレックス
ジョー
ケリー
アマンダ
ウィル
フィリップ
デビッド

からそれはリスト1からマイクを取るだろうし、リスト2からデボラは再びリスト1からマイク・次に、リスト3からすべての名前をリスト、サラリスト2、リスト3からのすべての名前が可能になるすべての組み合わせが出てくるまで。

私はこれをどのように遂行し、どのような援助をしているか考えていくことが大変である。

+2

何を試しましたか?あなたはどんなエラーを出していますか?リストはどこに保存されていますか? –

答えて

1

forループを使用できます。 3つのリストを含むファイルをf1、f2、およびf3とします。その後:

例えば
for a in `cat f1`;do 
for b in `cat f2`;do 
    for c in `cat f3`;do 
    echo $a $b $c; 
    done; 
done; 
done 

$ cat f1 
red 
green 
cat 

$ cat f2 
rice 
bread 
cat f 

$ cat f3 
tomato 
onion 


$ for a in `cat f1`;do for b in `cat f2`;do for c in `cat f3`;do echo $a $b $c; done; done; done 
red rice tomato 
red rice onion 
red bread tomato 
red bread onion 
green rice tomato 
green rice onion 
green bread tomato 
+0

これは完全に機能しました。私はそれを理解する前に私はループのために使用したので、特にこれを選んだ。 – nbucko

1

あなたはlist xが保存されてい方法に応じて、あなたは、単に一緒にすべての3つのリストを置換するためにブレース展開を使用することができ、例えば:

printf "%s\n" {Mike,Tom,Harry,Steve}\ 
{Deborah,Sarah,Jennifer}\ 
{Alex,Joe,Kelly,Amanda,Will,Phillip,David} 

使用例/出力

$ bash brexpperm.sh 
MikeDeborahAlex 
MikeDeborahJoe 
MikeDeborahKelly 
MikeDeborahAmanda 
MikeDeborahWill 
MikeDeborahPhillip 
MikeDeborahDavid 
MikeSarahAlex 
MikeSarahJoe 
MikeSarahKelly 
MikeSarahAmanda 
MikeSarahWill 
MikeSarahPhillip 
MikeSarahDavid 
MikeJenniferAlex 
MikeJenniferJoe 
MikeJenniferKelly 
MikeJenniferAmanda 
MikeJenniferWill 
MikeJenniferPhillip 
MikeJenniferDavid 
TomDeborahAlex 
TomDeborahJoe 
TomDeborahKelly 
TomDeborahAmanda 
TomDeborahWill 
TomDeborahPhillip 
TomDeborahDavid 
TomSarahAlex 
TomSarahJoe 
TomSarahKelly 
TomSarahAmanda 
TomSarahWill 
TomSarahPhillip 
TomSarahDavid 
TomJenniferAlex 
TomJenniferJoe 
TomJenniferKelly 
TomJenniferAmanda 
TomJenniferWill 
TomJenniferPhillip 
TomJenniferDavid 
HarryDeborahAlex 
HarryDeborahJoe 
HarryDeborahKelly 
HarryDeborahAmanda 
HarryDeborahWill 
HarryDeborahPhillip 
HarryDeborahDavid 
HarrySarahAlex 
HarrySarahJoe 
HarrySarahKelly 
HarrySarahAmanda 
HarrySarahWill 
HarrySarahPhillip 
HarrySarahDavid 
HarryJenniferAlex 
HarryJenniferJoe 
HarryJenniferKelly 
HarryJenniferAmanda 
HarryJenniferWill 
HarryJenniferPhillip 
HarryJenniferDavid 
SteveDeborahAlex 
SteveDeborahJoe 
SteveDeborahKelly 
SteveDeborahAmanda 
SteveDeborahWill 
SteveDeborahPhillip 
SteveDeborahDavid 
SteveSarahAlex 
SteveSarahJoe 
SteveSarahKelly 
SteveSarahAmanda 
SteveSarahWill 
SteveSarahPhillip 
SteveSarahDavid 
SteveJenniferAlex 
SteveJenniferJoe 
SteveJenniferKelly 
SteveJenniferAmanda 
SteveJenniferWill 
SteveJenniferPhillip 
SteveJenniferDavid 

それとも、あなたがスペースを必要とする場合、あなたは単に拡張に1を追加することができます

printf "%s\n" {'Mike ','Tom ','Harry ','Steve '}\ 
{'Deborah ','Sarah ','Jennifer '}\ 
{Alex,Joe,Kelly,Amanda,Will,Phillip,David} 

使用例/出力

$ bash brexpperm.sh 
Mike Deborah Alex 
Mike Deborah Joe 
Mike Deborah Kelly 
Mike Deborah Amanda 
... 
Steve Jennifer Amanda 
Steve Jennifer Will 
Steve Jennifer Phillip 
Steve Jennifer David 

あなたがコントロールの上に持っていない場合あなた自身のスクリプト内のリストであれば、ループ解がうまく動作します。

関連する問題