Koha/debian/scripts/koha-foreach
Evan Giles d9849aaa3a
Bug 34653: Make koha-foreach return the correct status code
I think the correct behavior for this script should be that koha-foreach
will return 0 (success) if all the commands it tried to run succeeded, but
1 (failure) if any of the commands failed.

To test:
1. $ koha-create --create-db test
2. $ vi test.sh
if [ $USER = 'kohadev-koha' ]; then
    echo "FAILED";
    exit 1;
else
    echo "SUCCESS";
    exit 0;
fi

3. $ debian/scripts/koha-foreach sh test.sh
FAILED
kohadev: 1 status returned by "sh test.sh"
SUCCESS
4. $ echo $?
5. Note that the exit status is 0 (success)

6. Apply patch

7. $ debian/scripts/koha-foreach sh test.sh
FAILED
kohadev: 1 status returned by "sh test.sh"
SUCCESS
8. $ echo $?
9. Note that the exit status is 1 (failure)

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-09-25 10:56:40 -03:00

77 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
# koha-foreach -- run a command for each Koha instance
# Copyright 2010 Catalyst IT, Ltd
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
# Read configuration variable file if it is present
[ -r /etc/default/koha-common ] && . /etc/default/koha-common
# include helper functions
if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
. "/usr/share/koha/bin/koha-functions.sh"
else
echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
exit 1
fi
chdir="no"
starting_dir=$(pwd)
listopts=""
while [ ! -z "$1" ]
do
case "$1" in
--chdir) chdir="yes";;
--email) listopts="$listopts --email";;
--noemail) listopts="$listopts --noemail";;
--enabled) listopts="$listopts --enabled";;
--disabled) listopts="$listopts --disabled";;
--sip) listopts="$listopts --sip";;
--nosip) listopts="$listopts --nosip";;
--plack) listopts="$listopts --plack";;
--noplack) listopts="$listopts --noplack";;
--letsencrypt) listopts="$listopts --letsencrypt" ;;
--noletsencrypt) listopts="$listopts --noletsencrypt" ;;
*) break;;
esac
shift
done
rv=0
for name in $(koha-list $listopts)
do
# Replace the __instancename__ placeholder for the instance name (Bug 8566)
cmd=`echo "$@" | sed -e s/__instancename__/${name}/g`
if [ "${cmd}" != "" ]; then
# Change to the instance's home dir if required
[ "$chdir" != "no" ] && eval cd ~$name"-koha"
if koha-shell ${name} -c "${cmd}"; then
: #noop
else
echo "${name}: $? status returned by \"${cmd}\""
rv=1
fi
# Go back to the original dir if required
[ "$chdir" != "no" ] && cd "$starting_dir"
fi
done
exit $rv