Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Shell脚本,三组名称,每个组合_Bash_List_Shell_Combinations - Fatal编程技术网

Bash Shell脚本,三组名称,每个组合

Bash Shell脚本,三组名称,每个组合,bash,list,shell,combinations,Bash,List,Shell,Combinations,我正在尝试编写一个shell脚本,它包含三个名称列表,并从列表中找出每个组合,每个列表上可能有不同数量的名称 列表1 迈克 汤姆 哈利 史蒂夫 列表2 黛博拉 莎拉 珍妮弗 列表3 亚历克斯 乔 凯利 阿曼达 将 菲利普 大卫 它将从列表1中选择Mike,然后从列表2中选择Deborah,然后从列表3中选择所有的名字。然后是列表1中的迈克,列表2中的莎拉,然后是列表3中的所有名字,等等,直到找到所有可能的组合 在思考如何完成此任务时遇到一些困难,如有任何帮助,我们将不胜感激。您可以使用for循环

我正在尝试编写一个shell脚本,它包含三个名称列表,并从列表中找出每个组合,每个列表上可能有不同数量的名称

列表1
迈克
汤姆
哈利
史蒂夫

列表2
黛博拉
莎拉
珍妮弗

列表3
亚历克斯

凯利
阿曼达

菲利普
大卫

它将从列表1中选择Mike,然后从列表2中选择Deborah,然后从列表3中选择所有的名字。然后是列表1中的迈克,列表2中的莎拉,然后是列表3中的所有名字,等等,直到找到所有可能的组合


在思考如何完成此任务时遇到一些困难,如有任何帮助,我们将不胜感激。

您可以使用for循环。设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

您可以使用for循环。设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

根据存储列表x的方式,您可以简单地使用大括号扩展将所有三个列表排列在一起,例如:

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
$ 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
或者,如果需要空间,只需在扩展中添加一个:

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
$ 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

如果您无法控制脚本本身中的列表,则循环解决方案可以正常工作。

根据您存储列表x的方式,您可以简单地使用大括号扩展将所有三个列表排列在一起,例如:

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
$ 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
或者,如果需要空间,只需在扩展中添加一个:

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
$ 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

如果您无法控制脚本本身中的列表,则循环解决方案可以正常工作。

您尝试了什么?你犯了什么错误?列表存储在哪里?您尝试了什么?你犯了什么错误?列表存储在哪里?这非常有效。特别选择这个,因为我以前使用过循环,所以我理解它。这非常有效。特别选择这个,因为我以前使用过循环,所以我理解它。