Richard_Grant Posted September 24, 2014 Share Posted September 24, 2014 This is a simple permutation function that i was converting from php: string_gen(){ declare -a chars=('a' 'b') max=4 let length=${#chars[@]}-1 n=0 combination=" " for i in `seq 0 $max`; do let n=($n*($length))+($length) done remainder=0 for i in `seq 0 $n`; do current=$i combination=" " while [[ $current > 0 ]] do remainder=($current%$length) echo remainder if [ $remainder -eq 0 ]; then combination="$combination"${chars[$length]} else combination="$combination"${chars[$remainder]} current = $current/$length fi done done echo $combination } What is wrong with the syntax? Link to PHP version: http://stackoverflow.com/questions/12293870/algorithm-to-get-all-possible-string-combinations-from-array-up-to-certain-lengt Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted September 24, 2014 Share Posted September 24, 2014 Shebang maybe? Right permission on the file? What errors do you get? Quote Link to comment Share on other sites More sharing options...
trq Posted September 24, 2014 Share Posted September 24, 2014 http://www.catb.org/esr/faqs/smart-questions.html Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted September 25, 2014 Share Posted September 25, 2014 (edited) Following the logic of your script, I modified it a little and got 4 bbbb's. Here it is, freaks_bash #!/bin/bash string_gen(){ declare -a chars=('a' 'b') remainder=0 current=1 combination=" " max=4 n=0 let length=${#chars[@]}-1 for i in `seq 0 $max`; do let n=$n*$length+$length done while [[ $n -gt $current ]]; do remainder=$current%$length if [[ $remainder -eq 0 ]]; then combination="$combination"${chars[$length]} else combination="$combination"${chars[$remainder]} current = $current/$length fi let current=current+1 done echo $combination; } string_gen # call the function Result: [lxc@lxc-box ~]$ ./freaks_bash bbbb What result are you expecting to get? Edited September 25, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.