朋友找我在一台机器上帮忙安装下discuz。想着搭建过好几次的lnmp了,但是还没有使用过“一键安装”的自动化脚本,去网上有搜索出来,但是运行的时候发现用root运行别人的脚本还是真危险的事情,于是明白这不是个轮子,而是个井,每个程序员有必要自己挖一口的了。所以想着自己写个自动化安装lnmp的脚本lnmp_auto。
1
2
3
4
5
<code>if</code> <code>[</code><code>$uid</code> <code>-ne 0 ]</code>
<code>then</code>
<code> </code><code>echo</code> <code>"error: you must be root to run the script!"</code>
<code> </code><code>exit</code> <code>1</code>
<code>fi</code>
有很多种方法,但是如果要输出最简单明了的估计是这种方法了:
<code>machine_long_bit=$(getconf long_bit)</code>
6
7
8
9
10
11
12
13
<code>if</code> <code>grep -q mysql /etc/group</code>
<code> </code><code>echo</code> <code>"mysql group exists"</code>
<code>else</code>
<code> </code><code>groupadd mysql</code>
<code>if</code> <code>groups mysql | grep -q -e</code><code>' mysql(\s|$)'</code>
<code> </code><code>echo</code> <code>"mysql user exists"</code>
<code> </code><code>useradd -r -g mysql mysql</code>
<code>filelist=`ls</code><code>$binary_mysql_path</code><code>/bin`</code>
<code>for</code> <code>filename in</code><code>$filelist</code>
<code>do</code>
<code> </code><code>ln -sn --force</code><code>$binary_mysql_path</code><code>/bin/</code><code>$filename</code> <code>/bin/</code><code>$filename</code>
<code>done</code>
我们一般使用sed "s/xxxx/oooo/" file来做替换
但是如果xxxx中包含/并且还有可能包含变量的话,就需要将/符号替换成:了
<code>sed -i</code><code>"s:/usr/local/mysql/data:${binary_mysql_path}/data:"</code> <code>mysql.server.init</code>
在shell中注释一行代码是#,那么注释一段代码就需要使用:<<block
<code>:<<block</code>
<code>binary_mysql_path=</code><code>"$root_path"</code><code>/binary/mysql</code>
<code>if</code> <code>[ ! -d</code><code>"$binary_mysql_path"</code> <code>]</code>
<code> </code><code>mkdir</code> <code>-p</code><code>$binary_mysql_path</code>
<code>block</code>
使用read命令将输出记录到一个变量中
<code>checkinstall=</code><code>"n"</code>
<code>read -p</code><code>"are you sure want to install php into service?(y/n):"</code> <code>checkinstall</code>
<code>if</code> <code>[</code><code>$checkinstall</code><code>=</code><code>"y"</code> <code>]</code>
<code>…</code>
<code>sed -i</code><code>"s/enforcing/disabled/"</code> <code>/etc/selinux/config</code>
这里的i参数就是在当前文件直接做替换,如果觉得不保险可以先做备份
这里不是获取当前的路径(当前路径直接使用pwd就可以获取),比如我在/home/yejianfeng,运行sh lnmp_auto/lnmp_auto.sh,如何获取到lnmp_auto.sh的文件夹路径/home/yejianfeng/lnmp_auto/
<code>$(cd</code><code>"$(dirname "</code><code>$0</code><code>")"</code><code>; pwd)</code>