我有bash脚本来安装tomcat和java,这将基于用户选择输入而发生,如我们所料,它运行良好。当java和tomcat不存在时,它将按照以下代码一一配置。如果tomcat和Java已经存在,它将通过终端中的错误进行处理。
#!/bin/bash
tomcat_base_dir=/sys/workers
java_base_dir=/usr/lib/java
software_location=/home/softwares/
PS3='Please enter your choice: '
clear
echo "==========================="
echo "Softeare Setup"
echo "==========================="
options=("Option 1: Tomcat Installation" "Option 2: Java Installation" "Option 3: Reply" "Quit")
COLUMNS=12
select opt in "${options[@]}"; do
case $opt in
"Option 1: Tomcat Installation")
echo "you chose choice 1"
echo "Tomcat Installation"
echo "================================================================"
if [ ! -d $tomcat_base_dir ]; then
echo "Error directory $tomcat_base_dir does not exists!"
mkdir -p /sys/workers
cp -r $software_location/apache*.gz $tomcat_base_dir
echo "tomcat file copied"
cd $tomcat_base_dir && tar zxf apache*.gz -C . && rm -rf apache*.gz && mv a* tomcat
echo "Tomcat tar file removed and renamed as well"
echo "Tomcat Version"
tomcat_output="$(cd $tomcat_base_dir/bin && ./version.sh | grep -i "Server version")"
echo "============================"
echo "${tomcat_output}"
echo "============================"
else
echo "$tomcat_base_dir directory found!"
fi
;;
"Option 2: Java Installation")
echo "you chocie 2"
echo "Java Installation"
echo "================================================================"
if [ ! -d $java_base_dir ]; then
echo "Error directory $java_base_dir does not exists!"
mkdir -p /usr/lib/java
cp -r $software_location/jre*.gz $java_base_dir
echo "java file copied"
cd $java_base_dir && tar zxf jre*.gz -C . && rm -rf jre*.gz && mv j* jre
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/java/jre/bin/java" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/java/jre/bin/javaws" 1
echo "Java Runtime Environment installtion completed"
java_output="$(java -version 2>&1 | head -n 1 | cut -d\" -f 2)"
echo "============================"
echo "${java_output}"
echo "============================"
else
echo "$java_base_dir directory found!"
fi
;;
"Option 3: Reply")
echo "Reply"
echo "================================================================"
echo "you chose choice $REPLY which is $opt"
;;
"Quit")
break
;;
*) echo "invalid option $REPLY" ;;
esac
done
现在的问题是,当我选择选项1时,我必须选择我在机箱内拥有的所有选项,例如(选项1 java安装和选项2 tomcat安装)。当我使用选项2 tomcat时会安装。
现在我要使用all选项安装两个软件。
答案 0 :(得分:1)
为每个单独的安装创建功能或脚本,并添加用于安装这两种功能的脚本:
select opt in "${options[@]}"; do
case $opt in
"Option 1: Tomcat Installation")
install_tomcat
;;
"Option 2: Java Installation")
install_java
;;
"Option GREEDY: Install everything")
install_tomcat; install_java
;;
....