I would like to generate list of all strings of the abcd1234
. Needless to say, abcd
would loop through all alphabets a
to z
(4 times) and 1234
would loop through 0 to 9 (4 times). All these strings I am storing in a text file. As a second wish, I would like to have this program to have a resume feature. i.e. if the program is stopped, then it should start from the where it was left off.
Via brute-force method (i.e. by running 8 loops), I am able to achieve the first part. (Code below). I am aware that this is the worst strategy. Any thoughts on the improvements?
For the second part, I through of picking the last line of the output file and then pick each character. However in my method, I am missing out many strings. I know where the bug (in my thinking/method) is, but I am not able to rectify it. Any kind of help is appreciated!
Thanks in advance.
My code:
#!/bin/bash
outfile=$1
if [ ! -f $outfile ]
then
echo "aaaa0000" > $outfile
fi
lastword=$(tail -1 $outfile)
a_beg=${lastword:0:1}
b_beg=${lastword:1:1}
c_beg=${lastword:2:1}
d_beg=${lastword:3:1}
i_beg=${lastword:4:1}
j_beg=${lastword:5:1}
k_beg=${lastword:6:1}
l_beg=${lastword:7:1}
# echo $a_beg
# echo $b_beg
# echo $c_beg
# echo $d_beg
# echo $i_beg
# echo $j_beg
# echo $k_beg
# echo $l_beg
# echo ""
echo "$a_beg$b_beg$c_beg$d_beg$i_beg$j_beg$k_beg$l_beg"
## This removes the $lastword, as the following loop repeats it!
sed -i '$ d' $outfile
for a in $(eval echo {$a_beg..z})
do
for b in $(eval echo {$b_beg..z})
do
for c in $(eval echo {$c_beg..z})
do
for d in $(eval echo {$d_beg..z})
do
for i in $(eval echo {$i_beg..9})
do
for j in $(eval echo {$j_beg..9})
do
for k in $(eval echo {$k_beg..9})
do
for l in $(eval echo {$l_beg..9})
do
echo "$a$b$c$d$i$j$k$l" >> $outfile
done
done
done
done
done
done
done
done
I guess I can answer first part of your question. The second part seems little complicated :-(.
For the first part, instead of running these loops, you can use GNU parallel utility.
The following command should do the job:
Hope this helps!
-- Mike