dialog工具,让脚本迈向图形化
如果你知道你的脚本程序只需要运行在linux控制台上,则可以使用dialog工具命令,它以一种非常整洁的方式润色你的脚本程序。这个命令使用文本模式的图形和色彩,但它的确提供了友好的面向图形的解决方案。
#!/bin/sh dialog --title "Linux environment deploy" --menu "select soft:" 20 40 10 1 "mysql" 2 "nginx" 3 "php" 4 "php-extension" 5 "mongodb" 6 "redis" 7 "memcached" 8 "set ulimit" 9 "set sysctl" 10 "varnish" 2>_1.txt PACKAGES=$(cat _1.txt) install_mysql(){ if [ ! -f ./mysql-${MYSQL_VERSION}.tar.gz ];then _V=$(echo ${MYSQL_VERSION} | awk -F. '{print $2}') wget http://cdn.mysql.com//Downloads/MySQL-5.$_V/mysql-${MYSQL_VERSION}.tar.gz 2>&1 | \ stdbuf -o0 awk '/[.] +[0-9][0-9]?[0-9]?%/ { print substr($0,63,3) }'| \ dialog --gauge "Download mysql-${MYSQL_VERSION}.tar.gz" 10 100 fi if [ ! -d ${INSTALL_PATH} ]; then dialog --infobox "decompression mysql-${MYSQL_VERSION}.tar.gz" 5 40 tar zxf mysql-${MYSQL_VERSION}.tar.gz cd mysql-${MYSQL_VERSION} cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PATH} \ -DMYSQL_DATADIR=${INSTALL_PATH}/var \ -DSYSCONFDIR=/etc \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=all if [ "$?" -eq 0 ];then make && make install else exit 1 fi if [ ! `cat /etc/group | awk -F: '{print $1}'| grep mysql` ];then groupadd mysql fi if [ ! `cat /etc/passwd | awk -F: '{print $1}'| grep mysql` ];then useradd -M -g mysql -s /sbin/nologin mysql fi cp ../profile.d/mysql.sh /etc/profile.d/ echo "${INSTALL_PATH}/lib" > /etc/ld.so.conf.d/mysql.conf ldconfig source /etc/profile.d/mysql.sh echo echo "init ..." echo mkdir ${INSTALL_PATH}/var chown -R mysql:mysql ${INSTALL_PATH}/var if [ -f /etc/my.cnf ];then rm -rf /etc/my.cnf fi ${INSTALL_PATH}/scripts/mysql_install_db --user=mysql --basedir=${INSTALL_PATH} --datadir=${INSTALL_PATH}/var --explicit_defaults_for_times if [ "$?" -eq 0 ];then echo "init success." else exit 1 fi cp support-files/mysql.server /etc/rc.d/init.d/mysqld cp ../conf/my.cnf /etc/ chmod a+x /etc/rc.d/init.d/mysqld mkdir ${INSTALL_PATH}/mysql_binlog mkdir ${INSTALL_PATH}/logs chown -R mysql.mysql ${INSTALL_PATH}/logs chown -R mysql.mysql ${INSTALL_PATH}/mysql_binlog /etc/init.d/mysqld start chkconfig --add mysqld chkconfig mysqld on else echo "mysql directory is exist. will be quit" exit 0 fi } if [ "$PACKAGES" = "1" ]; then dialog --title "MySQL Version" --inputbox "输入mysql版本(如:5.7.9,会从官网下载):" 10 50 2>_1.txt MYSQL_VERSION=$(cat _1.txt) dialog --title "ENTER INSTALL PATH" --inputbox "请输入安装路径:" 10 40 2>_1.txt INSTALL_PATH=$(cat _1.txt) install_mysql else exit 1 fi