0

I have a while loop in a for loop to create user accounts in drupal using drush and it's not iterating through the sequence properly. It creates the accounts or all the domains fine, but the while loop does not create the user roles after the first account. Anyone see my mistake?

#add the user
USERLIST=(
        "$USER,page_creator" 
        #"$USER,layout_manager"
        "$USER,page_reviewer" 
        "$USER,landing_page_creator"
        "$USER,landing_page_reviewer"
        "$USER,media_manager"
        "$USER,block_creator"
        #"$USER,site_builder"
        "$USER,block_manager"
    )

count=0

DOMAINLIST=("lmn" "pdq" "xyz")
for SITE in "${DOMAINLIST[@]}"
do
    echo "Creating account for $USER"
    drush "@company-acsf."$SITE ucrt $USER --password="$PW" --mail="$USER"
        while [ "x${USERLIST[count]}" != "x" ]
        do
            count=$(( $count + 1 ))
            IFS=',' read -ra LINE <<< "${USERLIST[count]}"
            USERNAME=${LINE[0]}
            USERROLE=${LINE[1]}     

            if [[ -n "$USERNAME"  && -n "$USERROLE" ]] ; then
                echo "Updating account for $USERNAME with role \"$USERROLE\""
                drush "@company-acsf."$SITE urol "${USERROLE}" $USERNAME
            fi
        done
done
exit 0
4
  • If you put some debug (an echo statement) in the inner loop does it get executed? With the right values? What values are actually being tested by the innermost if - are they what you'd expect? Commented Feb 27, 2020 at 22:15
  • 1
    In general terms, don't use all-caps variable names - you could easily end up clashing with reserved names. Also, use #!/bin/bash as the first line. Finally, don't mix [ ... ] and [[ ... ]] - use one or the other - and learn about [[ -z "$var" ]] instead of the ancient [ "x$var" = "x" ] thing. Commented Feb 27, 2020 at 22:17
  • 1
    It's like two different people have written this code, but used different styles (and years). Good maintenance style is to be consistent: copy what's already there or update it all to the modern style. Commented Feb 27, 2020 at 22:19
  • Good eye roaima. 2 diff people did write this. I figured I could just import a block of code and that would be it.
    – ajburch
    Commented Feb 28, 2020 at 12:08

1 Answer 1

1

As already noted, the count variable is initialized outside the for-loop, but not reset after the first while-loop. Another issue is that the count variable is incremented too early and role page_creator is skipped. The password PW is also not set, but I guess you only showed us a part of the script.

You could change the while-loop to a for-loop like this (and some minor improvements, see comments):

#!/bin/bash
### ^^^^^^^ add shebang

#add the user
USERLIST=(
        "$USER,page_creator"
        #"$USER,layout_manager"
        "$USER,page_reviewer"
        "$USER,landing_page_creator"
        "$USER,landing_page_reviewer"
        "$USER,media_manager"
        "$USER,block_creator"
        #"$USER,site_builder"
        "$USER,block_manager"
    )

### remove
#count=0
DOMAINLIST=("lmn" "pdq" "xyz")
for SITE in "${DOMAINLIST[@]}"; do

    ### add SITE name to echo
    echo "Creating account for $USER, site \"$SITE\""
    drush "@company-acsf."$SITE ucrt $USER --password="$PW" --mail="$USER"

    ### change while-loop to for-loop
    for ((count=0; count < ${#USERLIST[@]}; count++)); do
    #while [ "x${USERLIST[count]}" != "x" ]
    #do
        ### remove
        #count=$(( $count + 1 ))
        IFS=',' read -ra LINE <<< "${USERLIST[count]}"
        USERNAME=${LINE[0]}
        USERROLE=${LINE[1]}
        if [[ -n "$USERNAME"  && -n "$USERROLE" ]]; then
            echo "Updating account for $USERNAME with role \"$USERROLE\""
            drush "@company-acsf."$SITE urol "${USERROLE}" $USERNAME
        fi
    done
done

### probably not needed, exit status is the status of the last command
#exit 0

Left as exercise: Change your variables to lower-case and quote all your variables. Use shellcheck.net to find bugs in your shell scripts.

1
  • Thanks for the assist. It works well after cleaning up the vars and using your code suggestions.
    – ajburch
    Commented Feb 28, 2020 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .