Script to iterate a task to all users (example: renew manuals after serverupgrade)

Every Nextcloud Version comes with a new Manual shipped in the skeleton-directory but the upgrade procedure does not look for old outdated manuals in user dirs.
Maybe this job could be done by occ in the future.
For so long this script can do the job if you like.
First you have to install the Command-line JSON processor “jq”:
sudo apt-get install jq
The script can handle User-Names with spaces.
Run it as the ht-user (www-data, www-run, depending on your server), not as root:

# simple script to update the "Nextcloud Manual.pdf" in the user dirs after an upgrade
# Copyright 2019, Raphael Gradenwitz <>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

## Colors, change this if you like:
# . /usr/local/share/bashlib/colors

### !!
## change this dir for your needs:

ncc="php -f ${ncdir}/occ"
# skeletondir="`$ncc config:system:get skeletondirectory`"
datadir="`$ncc config:system:get datadirectory`"
ncman='Nextcloud Manual.pdf'
echo -e "${Lightgray}md5sum${NORMAL} from skeleton ${ncmanBLUE}    : ${YELLOW}`md5sum "$skeletondir/$ncman" | awk '{ print $1 }'`${NORMAL}"
echo -e "${Lightgray}stat -c %y${NORMAL} from skeleton ${ncmanBLUE}: ${YELLOW}`stat -c %y "$skeletondir/$ncman"`${NORMAL}"
echo -e "checking for old $ncmanBLUE:"
while read user_id; do
    echo -e "Processing User ${Lila}$user_id${NORMAL}:"
    cd "${datadir:=$ncdir/data}/$user_id/files"
    echo -e "    entered directory ${LILA}`pwd`${NORMAL}"
#    echo "output of 'ls -l' :"
#    ls -l
#    echo
    [ -f "$ncman" ] && {
        echo -e "    - ${Lightgray}md5sum${NORMAL} from local $ncmanBLUE    : ${YELLOW}`md5sum "$ncman" | awk '{ print $1 }'`${NORMAL}"
        echo -e "    - ${Lightgray}stat -c %y${NORMAL} from local $ncmanBLUE: ${YELLOW}`stat -c %y "$ncman"`${NORMAL}"
        echo "`md5sum "$skeletondir/$ncman" | awk '{ print $1 }'` "$ncman""| md5sum --status -c -
        [ $? -ne 0 ] && {
            echo -e "    - local $ncmanBLUE ${RED}not up to date!${NORMAL}"
            echo -e "    - ${Lightgray}removing${NORMAL} old $ncmanBLUE..."
            rm "$ncman"
            echo -e "    - ${Lightgray}copying${NORMAL} new $ncmanBLUE from skeleton on its place..."
            cp -a "$skeletondir/$ncman" .
            $ncc files:scan --path="/$user_id/files/$ncman"
        } || {
            echo -e "    - ${GREEN}local $ncmanBLUE ${GREEN}up to date, there is nothing to be changed.${NORMAL}"
    } || {
        echo -e "    - ${RED}no local $ncmanBLUE ${RED}found!${NORMAL}"
        echo -e "    - ${Lightgray}copying $ncmanBLUE from skeleton on its place..."
        cp -a "$skeletondir/$ncman" .
        $ncc files:scan --path="/$user_id/files/$ncman"
`$ncc user:list --output=json | jq -M 'keys' | sed -e '/\[/d' -e '/\]/d' -e 's/,$//g' -e "s/\"//g" -e 's/^[ \t]*//g'`

[edit] some basics:
Put this script in a file called “renew-manuals” in your favourite path for scripts. You can either make that file executable or add this line to the file .bash_profile of your privileged user (assuming your htuser as www-data):

alias renew-manuals='su www-data -s /bin/bash /[path/to]/renew-manuals'

or if your htuser has its own login-shell environment with its own .bash_profile (change path to your needs):

alias renew-manuals='/bin/bash -f /home/www-data/bin/renew-manuals'

Now you only have to run that alias.