1. 程式人生 > >select+case選擇語句

select+case選擇語句

select case lamp

(1)case語句

語法:

case $1 in

pattern)

語句1

;;

pattern)

語句2

;;

pattern)

語句3

;;

esac


(2) case案例

case $1 in

1)

echo "安裝apache"

;;

2)

echo "安裝mysql"

;;

3)

echo "安裝php"

;;

4)

echo "整合LAMP"

;;

*)

echo "please enter Number1|2|3|"

esac


read -p "please enter Number:" number

case $number in

1)

echo -e "\033[36m安裝apache\033[0m"

;;

2)

echo -e "\033[36m安裝mysql\033[0m"

;;

3)

echo -e "\033[36m安裝php\033[0m"

;;

4)

echo -e "\033[36mplease enter number $0|1|2|3|help\033[0m"

;;

*)

echo "please enter Number1|2|3|"

esac

小結:

case語句中的變量,可以作為腳本的第一個參數傳入,也可以通過read -p 通過鍵盤輸入參數


selece語句

語法:

select i in (表達式)

do

語句

done


案例:

PS3="please enter select menu:"

select i in install_apache install_php install_mysql quit

do

case $i in

install_apache)

echo "安裝apache"

;;

install_php)

echo "安裝php"

;;

install_mysql)

echo "安裝mysql"

;;

quit)

echo "已退出安裝"

exit 0

;;

*)

echo "please enter menu $0 install_apache|install_php|install_mysql"

;;

esac

done


註釋:

(1) select語句和case語句可以嵌套使用


(2) select作為選擇菜單,case接收到選擇結果後,去執行出結果


select+case+function 一鍵安裝LAMP

#!/bin/bash

#Auto install LAMP

#By author jfedu.net 2017

#Define Path variables

#Httpd define path variable

H_FILES=httpd-2.2.32.tar.bz2

H_FILES_DIR=httpd-2.2.32

H_URL=http://mirrors.cnnic.cn/apache/httpd/

H_PREFIX=/usr/local/apache2/

#MySQL define path variable

M_FILES=mysql-5.5.20.tar.gz

M_FILES_DIR=mysql-5.5.20

M_URL=http://down1.chinaunix.net/distfiles/

M_PREFIX=/usr/local/mysql/

#PHP define path variable

P_FILES=php-5.3.28.tar.bz2

P_FILES_DIR=php-5.3.28

P_URL=http://mirrors.sohu.com/php/

P_PREFIX=/usr/local/php5/

function httpd_install(){

if [[ "$1" -eq "1" ]]then

wget -c $H_URL/$H_FILES && tar -jxvf $H_FILES && cd $H_FILES_DIR &&./configure --prefix=$H_PREFIX

if [ $? -eq 0 ]then

make && make install

fi

fi

}

function mysql_install(){

if [[ "$1" -eq "2" ]]then

wget -c $M_URL/$M_FILES && tar -xzvf $M_FILES && cd $M_FILES_DIR &&yum install cmake ncurses-devel -y cmake . -DCMAKE_INSTALL_PREFIX=$M_PREFIX \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DMYSQL_DATADIR=/data/mysql \

-DSYSCONFDIR=/etc \

-DMYSQL_USER=mysql \

-DMYSQL_TCP_PORT=3306 \

-DWITH_XTRADB_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_READLINE=1 \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_EXTRA_CHARSETS=1 \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DEXTRA_CHARSETS=all \

-DWITH_BIG_TABLES=1 \

-DWITH_DEBUG=0

if [ $? -eq 0 ]then

make && make install

echo -e "\n\033[32m-----------------------------------------------\033[0m"

echo -e "\033[32mThe $M_FILES_DIR Server Install Success !\033[0m"

else

echo -e "\033[32mThe $M_FILES_DIR Make or Make install ERROR,Please Check......"

exit 0

fi

/bin/cp support-files/my-small.cnf /etc/my.cnf

/bin/cp support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on

fi

}

function php_install(){

if [[ "$1" -eq "3" ]]then

yum install libxml2-devel perl-devel perl libtool* -y

wget -c $P_URL/$P_FILES && tar -jxvf $P_FILES && cd $P_FILES_DIR &&./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc --with-mysql=$M_PREFIX --with-apxs2=$H_PREFIX/bin/apxs

if [ $? -eq 0 ]then

make ZEND_EXTRA_LIBS='-liconv' && make install

echo -e "\n\033[32m-----------------------------------------------\033[0m"

echo -e "\033[32mThe $P_FILES_DIR Server Install Success !\033[0m"

else

echo -e "\033[32mThe $P_FILES_DIR Make or Make install ERROR,Please Check......"

exit 0

fi

fi

}

function lamp_config(){

if [[ "$1" -eq "4" ]]then

sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf

$H_PREFIX/bin/apachectl restart

echo "AddType application/x-httpd-php .php" >>$H_PREFIX/conf/httpd.conf

IP=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f2`

echo "You can to access http://$IP/"

cat >$H_PREFIX/htdocs/index.php<<EOF

<?php

phpinfo()

?>

EOF

fi

}

PS3="Please enter you select install menu:"

select i in http mysql php config quit

do

case $i in

http)

httpd_install 1

;;

mysql)

mysql_install 2

;;

php)

php_install 3

;;

config)

lamp_config 4

;;

quit)

exit

esac

done





select+case選擇語句