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 Link to comment https://forums.phpfreaks.com/topic/291263-bash-syntax/ 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? Link to comment https://forums.phpfreaks.com/topic/291263-bash-syntax/#findComment-1491981 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 Link to comment https://forums.phpfreaks.com/topic/291263-bash-syntax/#findComment-1492005 Share on other sites More sharing options...
jazzman1 Posted September 25, 2014 Share Posted September 25, 2014 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? Link to comment https://forums.phpfreaks.com/topic/291263-bash-syntax/#findComment-1492009 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.