Implement a Bash script that manages the installation of specific system extensions, ensuring proper execution and error handling.
Functionality to Build
The script should first check if the environment variable DEBIAN_EXTENSION_HOME is set and not empty. If it is empty, the script should output an error message and exit with an error code of 10.
The script must then iterate over an array containing the items "dbus" and "libpam-systemd".
For each item in the array, the script should execute a corresponding installation script located within the $DEBIAN_EXTENSION_HOME/session/ directory, named install_<ITEM>.sh, where <ITEM> is the name of the current array element (e.g., install_dbus.sh, install_libpam-systemd.sh).
If any installation script returns an error code (non-zero), the main script should output an error message specific to the failed item and exit with an error code starting from 200, incremented by the index of the item in the array (e.g., 200 for the first item, 201 for the second).
Upon successful completion of all installation scripts, the main script should output a success message: "install session success."
Inputs and Outputs
Input:
Environment variable $DEBIAN_EXTENSION_HOME should be set to a valid directory path.
Output:
Error message if $DEBIAN_EXTENSION_HOME is empty or if an installation script fails.
Success message if all installations complete successfully.
File Locations and Naming
The solution should be implemented in the file /app/install_session.sh.
Tests
The following test script will be used to verify your solution:
bash
#!/bin/bash
echo "install session..."
ERROR_CODE_EXTENSION_HOME=10
ERROR_CODE_INSTALL=200
if test -z $DEBIAN_EXTENSION_HOME; then
echo "Debian extension home path must be not empty!"
exit $ERROR_CODE_EXTENSION_HOME
fi
ARRAY=(\
"dbus" \
"libpam-systemd")
SIZE=${#ARRAY[@]}
for ((i = 0; i < SIZE; i++)); do
ITEM="${ARRAY[$i]}"
$DEBIAN_EXTENSION_HOME/session/install_${ITEM}.sh
if test $? -ne 0; then
echo "Install \"$ITEM\" error!"
exit $((ERROR_CODE_INSTALL + i))
fi
done
echo "install session success."
exit 0