The problem
Sometimes you need to create multiple config files for a project or want to test different parameters on a template. If you need to automate the creation of files while changing the content, Bash is there for you. Given three lists, you can create files with permutations of the content of those lists. Let’s see an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
list_x=(1 2 3 4 5) # this one set also the subfolder
list_y=(1 2 3 4 5)
list_z=(1 2 3 4 5)
size_y="${#list_y[@]}"
size_z="${#list_z[@]}"
size=$((size_y*size_z-1))
grid_name_seq=($(seq -w 0 $size)) # second () loads as array
grid_index=0
for x_value in "${list_x[@]}"; do
grid_folder=grid"${grid_name_seq[$grid_index]}"
mkdir -p "$grid_folder"
y_index=0
index=0 # corresponds to y-z plane
for y_value in "${list_y[@]}"; do
z_index=0
for z_value in "${list_z[@]}"; do
# the filename is: gridXY/gridFileXY.yaml
fileHere="$grid_folder"
fileHere="$fileHere"/gridFile
fileHere="$fileHere""${grid_name_seq[$index]}"
fileHere="$fileHere".yaml
rm $fileHere
echo "x_value: $x_value" >> "$fileHere" # x eman
echo "y_value: $y_value" >> "$fileHere" # y part
echo "z_value: $z_value" >> "$fileHere" # z child
z_index=$((z_index+1))
index=$((z_index+y_index))
done
y_index=$((y_index+size_z))
done
grid_index=$((grid_index+1))
done
|
Let’s run it
You can see for yourself the amount of files created using the tree command in Bash.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
.
βββ extra.md
βββ grid00
βΒ Β βββ gridFile00.yaml
βΒ Β βββ gridFile01.yaml
βΒ Β βββ gridFile02.yaml
βΒ Β βββ gridFile03.yaml
βΒ Β βββ gridFile04.yaml
βΒ Β βββ gridFile05.yaml
βΒ Β βββ gridFile06.yaml
βΒ Β βββ gridFile07.yaml
βΒ Β βββ gridFile08.yaml
βΒ Β βββ gridFile09.yaml
βΒ Β βββ gridFile10.yaml
βΒ Β βββ gridFile11.yaml
βΒ Β βββ gridFile12.yaml
βΒ Β βββ gridFile13.yaml
βΒ Β βββ gridFile14.yaml
βΒ Β βββ gridFile15.yaml
βΒ Β βββ gridFile16.yaml
βΒ Β βββ gridFile17.yaml
βΒ Β βββ gridFile18.yaml
βΒ Β βββ gridFile19.yaml
βΒ Β βββ gridFile20.yaml
βΒ Β βββ gridFile21.yaml
βΒ Β βββ gridFile22.yaml
βΒ Β βββ gridFile23.yaml
βΒ Β βββ gridFile24.yaml
βββ grid01
βΒ Β βββ gridFile00.yaml
βΒ Β βββ gridFile01.yaml
βΒ Β βββ gridFile02.yaml
βΒ Β βββ gridFile03.yaml
βΒ Β βββ gridFile04.yaml
βΒ Β βββ gridFile05.yaml
βΒ Β βββ gridFile06.yaml
βΒ Β βββ gridFile07.yaml
βΒ Β βββ gridFile08.yaml
βΒ Β βββ gridFile09.yaml
βΒ Β βββ gridFile10.yaml
βΒ Β βββ gridFile11.yaml
βΒ Β βββ gridFile12.yaml
βΒ Β βββ gridFile13.yaml
βΒ Β βββ gridFile14.yaml
βΒ Β βββ gridFile15.yaml
βΒ Β βββ gridFile16.yaml
βΒ Β βββ gridFile17.yaml
βΒ Β βββ gridFile18.yaml
βΒ Β βββ gridFile19.yaml
βΒ Β βββ gridFile20.yaml
βΒ Β βββ gridFile21.yaml
βΒ Β βββ gridFile22.yaml
βΒ Β βββ gridFile23.yaml
βΒ Β βββ gridFile24.yaml
βββ grid02
βΒ Β βββ gridFile00.yaml
βΒ Β βββ gridFile01.yaml
βΒ Β βββ gridFile02.yaml
βΒ Β βββ gridFile03.yaml
βΒ Β βββ gridFile04.yaml
βΒ Β βββ gridFile05.yaml
βΒ Β βββ gridFile06.yaml
βΒ Β βββ gridFile07.yaml
βΒ Β βββ gridFile08.yaml
βΒ Β βββ gridFile09.yaml
βΒ Β βββ gridFile10.yaml
βΒ Β βββ gridFile11.yaml
βΒ Β βββ gridFile12.yaml
βΒ Β βββ gridFile13.yaml
βΒ Β βββ gridFile14.yaml
βΒ Β βββ gridFile15.yaml
βΒ Β βββ gridFile16.yaml
βΒ Β βββ gridFile17.yaml
βΒ Β βββ gridFile18.yaml
βΒ Β βββ gridFile19.yaml
βΒ Β βββ gridFile20.yaml
βΒ Β βββ gridFile21.yaml
βΒ Β βββ gridFile22.yaml
βΒ Β βββ gridFile23.yaml
βΒ Β βββ gridFile24.yaml
βββ grid03
βΒ Β βββ gridFile00.yaml
βΒ Β βββ gridFile01.yaml
βΒ Β βββ gridFile02.yaml
βΒ Β βββ gridFile03.yaml
βΒ Β βββ gridFile04.yaml
βΒ Β βββ gridFile05.yaml
βΒ Β βββ gridFile06.yaml
βΒ Β βββ gridFile07.yaml
βΒ Β βββ gridFile08.yaml
βΒ Β βββ gridFile09.yaml
βΒ Β βββ gridFile10.yaml
βΒ Β βββ gridFile11.yaml
βΒ Β βββ gridFile12.yaml
βΒ Β βββ gridFile13.yaml
βΒ Β βββ gridFile14.yaml
βΒ Β βββ gridFile15.yaml
βΒ Β βββ gridFile16.yaml
βΒ Β βββ gridFile17.yaml
βΒ Β βββ gridFile18.yaml
βΒ Β βββ gridFile19.yaml
βΒ Β βββ gridFile20.yaml
βΒ Β βββ gridFile21.yaml
βΒ Β βββ gridFile22.yaml
βΒ Β βββ gridFile23.yaml
βΒ Β βββ gridFile24.yaml
βββ grid04
βΒ Β βββ gridFile00.yaml
βΒ Β βββ gridFile01.yaml
βΒ Β βββ gridFile02.yaml
βΒ Β βββ gridFile03.yaml
βΒ Β βββ gridFile04.yaml
βΒ Β βββ gridFile05.yaml
βΒ Β βββ gridFile06.yaml
βΒ Β βββ gridFile07.yaml
βΒ Β βββ gridFile08.yaml
βΒ Β βββ gridFile09.yaml
βΒ Β βββ gridFile10.yaml
βΒ Β βββ gridFile11.yaml
βΒ Β βββ gridFile12.yaml
βΒ Β βββ gridFile13.yaml
βΒ Β βββ gridFile14.yaml
βΒ Β βββ gridFile15.yaml
βΒ Β βββ gridFile16.yaml
βΒ Β βββ gridFile17.yaml
βΒ Β βββ gridFile18.yaml
βΒ Β βββ gridFile19.yaml
βΒ Β βββ gridFile20.yaml
βΒ Β βββ gridFile21.yaml
βΒ Β βββ gridFile22.yaml
βΒ Β βββ gridFile23.yaml
βΒ Β βββ gridFile24.yaml
βββ gridFileCreator.sh
5 directories, 127 files
|
In this video, we can peek at the content of the files:
With a few lines and one execution, it was possible to create 125 files with unique parameters, all in 5 separate folders.