本帖最后由 hlymlv 于 2015-12-21 22:17 编辑
分离一个文件的当前目录和文件名:
#/bin/bash
PATHNAME=/root/1/2/3/4/1.txt
echo -e "The whole pathname:\n\t $PATHNAME"
FILENAME=$( basename $PATHNAME)
echo -e "The file name: \n\t $FILENAME"
DIRNAME=$(dirname $PATHNAME)
echo -e "The directory name: \n\t $DIRNAME"
exit 0
basename 去除文件中的目录,dirname 脱去文件中非目录的后缀(需要掌握的就是这两个)
basename命令还可以去除文件名的后缀,只要在命令行最后添加要去除的后缀即可(如basename 1.txt .txt)
echo -n 不换行输出
echo -e 处理特殊字符:
若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:
\a 发出警告声;
\b 删除前一个字符;
\c 最后不加上换行符号;
\f 换行但光标仍旧停留在原来的位置;
\n 换行且光标移至行首;
\r 光标移至行首,但不换行;
\t 插入tab;
\v 与\f相同;
\\ 插入\字符;
哪么shell有三种执行方式你们知道不? 它们的各自区别呢
新人学习, 感谢和欢迎各位的指点
大家有啥不错的shell,也可以拿出来一起学习学习
分离一个文件的当前目录和文件名:
#/bin/bash
PATHNAME=/root/1/2/3/4/1.txt
echo -e "The whole pathname:\n\t $PATHNAME"
FILENAME=$( basename $PATHNAME)
echo -e "The file name: \n\t $FILENAME"
DIRNAME=$(dirname $PATHNAME)
echo -e "The directory name: \n\t $DIRNAME"
exit 0
basename 去除文件中的目录,dirname 脱去文件中非目录的后缀(需要掌握的就是这两个)
basename命令还可以去除文件名的后缀,只要在命令行最后添加要去除的后缀即可(如basename 1.txt .txt)
echo -n 不换行输出
echo -e 处理特殊字符:
若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:
\a 发出警告声;
\b 删除前一个字符;
\c 最后不加上换行符号;
\f 换行但光标仍旧停留在原来的位置;
\n 换行且光标移至行首;
\r 光标移至行首,但不换行;
\t 插入tab;
\v 与\f相同;
\\ 插入\字符;
哪么shell有三种执行方式你们知道不? 它们的各自区别呢
新人学习, 感谢和欢迎各位的指点
大家有啥不错的shell,也可以拿出来一起学习学习
0
本帖最后由 hlymlv 于 2015-12-22 21:22 编辑
通过shell 标准输入重定向:
#!/bin/bash
linenumber=1
read oneline
while [ "$oneline" != "" ]
do
echo -e "$linenumber:$oneline\n"
linenumber=`expr $linenumber + 1`
read oneline
done
exit 0
执行命令:sh 1.sh < file1(该shell用于按行号输出一个文件,但是匹配空行就会结束)
linenumber,oneline为自己设定,可更改
read oneline 读取一行,expr 增量计数(该shell中体现的,还有其他功能),在循环中用于增量计算
语句块的输入输出重定向:
#!/bin/bash
read -p "please enter the pathname:" n
c=0
filename=`basename $n`
while read l
do
c=`expr $c + 1`
echo "$c:$l"
done<$n>"$filename.line"
echo "output file is $filename.line"
exit
要掌握的就是这个<>,这个我的理解就是将文件重定向到while循环和 文件名.line中 不是很懂{:4_99:}
通过shell 标准输入重定向:
#!/bin/bash
linenumber=1
read oneline
while [ "$oneline" != "" ]
do
echo -e "$linenumber:$oneline\n"
linenumber=`expr $linenumber + 1`
read oneline
done
exit 0
执行命令:sh 1.sh < file1(该shell用于按行号输出一个文件,但是匹配空行就会结束)
linenumber,oneline为自己设定,可更改
read oneline 读取一行,expr 增量计数(该shell中体现的,还有其他功能),在循环中用于增量计算
语句块的输入输出重定向:
#!/bin/bash
read -p "please enter the pathname:" n
c=0
filename=`basename $n`
while read l
do
c=`expr $c + 1`
echo "$c:$l"
done<$n>"$filename.line"
echo "output file is $filename.line"
exit
要掌握的就是这个<>,这个我的理解就是将文件重定向到while循环和 文件名.line中 不是很懂{:4_99:}
0
本帖最后由 hlymlv 于 2016-1-4 14:17 编辑
计算字符串长度:
#!/bin/bash
echo "This script will tell you the length of your input!"
#使用空命令冒号构建无限循环
while :
do
read -p "Please Enter a Sting(or quit):"
case "$REPLY" in
[Qq]|[Qq][Uu][Ii][Tt])
echo "Bye."
#在输入大小写quit时,退出
exit
;;
*)
len=$(expr length "$REPLY")
#使用expr命令计算字符串的长度
echo "Length:$len"
;;
esac
done
exit
要掌握的:expr length
执行效果:
[root@lazy ~]# sh 1.sh
please enter the string(or quit):daksl
Length:5
please enter the string(or quit):dsfkl;
Length:6
please enter the string(or quit):re ewr
Length:6
please enter the string(or quit):q
Bye.
计算字符串长度:
#!/bin/bash
echo "This script will tell you the length of your input!"
#使用空命令冒号构建无限循环
while :
do
read -p "Please Enter a Sting(or quit):"
case "$REPLY" in
[Qq]|[Qq][Uu][Ii][Tt])
echo "Bye."
#在输入大小写quit时,退出
exit
;;
*)
len=$(expr length "$REPLY")
#使用expr命令计算字符串的长度
echo "Length:$len"
;;
esac
done
exit
要掌握的:expr length
执行效果:
[root@lazy ~]# sh 1.sh
please enter the string(or quit):daksl
Length:5
please enter the string(or quit):dsfkl;
Length:6
please enter the string(or quit):re ewr
Length:6
please enter the string(or quit):q
Bye.
0
本帖最后由 hlymlv 于 2016-1-4 14:41 编辑
select 快速创建菜单:
#/bin/bash
echo "which directory do you want to list:"
echo "(Press \"Enter\" directory to show menu again)"
select dir in /home /tmp /usr/local /usr/share quit
do
if [ -n "$dir" ] ;then
if [ "$dir" = "quit" ]
then
echo " You entered quit,Byebye!"
break;
else
echo "You selected directory \"$dir\",it contains following files:"
ls -l $dir
fi
else
echo "Error,invalid selection \"$REPLY\".choose again!"
fi
echo "which directory do you want to list:"
echo "(Press \"Enter\" directory to show menu again)"
done
exit
执行效果:[root@lazy victor]# sh 5.sh
which directory do you want to list:
(Press "Enter" directory to show menu again)
1) /home
2) /tmp
3) /usr/local
4) /usr/share
5) quit
#?
1
You selected directory "/home",it contains following files:
total 24
drwx------ 2 502 502 4096 Dec 22 20:08 lazy
drwx------ 2 user1 user1 4096 Dec 25 17:03 user1
drwx------ 2 user2 user2 4096 Dec 25 17:03 user2
drwx------ 2 user3 user3 4096 Dec 25 17:03 user3
which directory do you want to list:
(Press "Enter" directory to show menu again)
#?
1) /home
2) /tmp
3) /usr/local
4) /usr/share
5) quit
#? 5
You entered quit,Byebye!
要掌握的就是这个格式:select dir in
do
done
select 快速创建菜单:
#/bin/bash
echo "which directory do you want to list:"
echo "(Press \"Enter\" directory to show menu again)"
select dir in /home /tmp /usr/local /usr/share quit
do
if [ -n "$dir" ] ;then
if [ "$dir" = "quit" ]
then
echo " You entered quit,Byebye!"
break;
else
echo "You selected directory \"$dir\",it contains following files:"
ls -l $dir
fi
else
echo "Error,invalid selection \"$REPLY\".choose again!"
fi
echo "which directory do you want to list:"
echo "(Press \"Enter\" directory to show menu again)"
done
exit
执行效果:[root@lazy victor]# sh 5.sh
which directory do you want to list:
(Press "Enter" directory to show menu again)
1) /home
2) /tmp
3) /usr/local
4) /usr/share
5) quit
#?
1
You selected directory "/home",it contains following files:
total 24
drwx------ 2 502 502 4096 Dec 22 20:08 lazy
drwx------ 2 user1 user1 4096 Dec 25 17:03 user1
drwx------ 2 user2 user2 4096 Dec 25 17:03 user2
drwx------ 2 user3 user3 4096 Dec 25 17:03 user3
which directory do you want to list:
(Press "Enter" directory to show menu again)
#?
1) /home
2) /tmp
3) /usr/local
4) /usr/share
5) quit
#? 5
You entered quit,Byebye!
要掌握的就是这个格式:select dir in
do
done
0
计算新年(元旦)还需要多少个星期:
#!/bin/bash
echo "Today is $(date +%F)"
#获得今天是这一年的第几天
date=$(date +%j)
echo "There is $(((365-$date)/ 7)) weeks before New Year."
#在$date 的两侧需要有空格
echo "Have passed $(expr $date / 7 ) weeks in this Year."
#let命令的算数表达式不能包含任何空格
let weeks=(365-$date)/7
echo "There is $weeks weeks before New Year."
exit
#!/bin/bash
echo "Today is $(date +%F)"
#获得今天是这一年的第几天
date=$(date +%j)
echo "There is $(((365-$date)/ 7)) weeks before New Year."
#在$date 的两侧需要有空格
echo "Have passed $(expr $date / 7 ) weeks in this Year."
#let命令的算数表达式不能包含任何空格
let weeks=(365-$date)/7
echo "There is $weeks weeks before New Year."
exit
0
本帖最后由 hlymlv 于 2015-12-29 22:10 编辑
嗯嗯 相互学习 {:4_109:}
shell的引用:使用 \ 进行脱意:
如:[victor@lazy ~]$ echo A: \ is my floppy drive;OS:windows
A: is my floppy drive
-bash: OS:windows: command not found
正确方式:
[victor@lazy ~]$ echo A: \\ is my floppy drive\;OS:windows
A: \ is my floppy drive;OS:windows
使用 ' 进行处理:
[victor@lazy ~]$ echo 'A: \ is my floppy drive;OS:windows'
A: \ is my floppy drive;OS:windows
使用" 进行处理:
[victor@lazy ~]$ echo "A: \ is my floppy drive;OS:windows"
A: \ is my floppy drive;OS:windows
以上为三种引用方式,去掉特殊字符的含义
\:反斜杠,转义,去除其后紧跟的元字符或通配符的特殊意义。
'':单引号,硬转义,其内部所有的shell元字符、通配符都会被关掉。注意,硬转义中不允许出现’(单引号)。
"":双引号,软转义,其内部只允许出现特定的shell元字符($,`,\):$用于变量值替换、`用于命令替换、\用于转义单个字符
命令拆分:[victor@lazy ~]$ echo hello,\
> world
hello,world
[victor@lazy ~]$ echo 'hello,
> world'
hello,
world
[victor@lazy ~]$ echo "hello,
> world"
hello,
world
happyzj520 发表于 2015-12-29 15:03
我也去开一贴去。不错
嗯嗯 相互学习 {:4_109:}
shell的引用:使用 \ 进行脱意:
如:[victor@lazy ~]$ echo A: \ is my floppy drive;OS:windows
A: is my floppy drive
-bash: OS:windows: command not found
正确方式:
[victor@lazy ~]$ echo A: \\ is my floppy drive\;OS:windows
A: \ is my floppy drive;OS:windows
使用 ' 进行处理:
[victor@lazy ~]$ echo 'A: \ is my floppy drive;OS:windows'
A: \ is my floppy drive;OS:windows
使用" 进行处理:
[victor@lazy ~]$ echo "A: \ is my floppy drive;OS:windows"
A: \ is my floppy drive;OS:windows
以上为三种引用方式,去掉特殊字符的含义
\:反斜杠,转义,去除其后紧跟的元字符或通配符的特殊意义。
'':单引号,硬转义,其内部所有的shell元字符、通配符都会被关掉。注意,硬转义中不允许出现’(单引号)。
"":双引号,软转义,其内部只允许出现特定的shell元字符($,`,\):$用于变量值替换、`用于命令替换、\用于转义单个字符
命令拆分:[victor@lazy ~]$ echo hello,\
> world
hello,world
[victor@lazy ~]$ echo 'hello,
> world'
hello,
world
[victor@lazy ~]$ echo "hello,
> world"
hello,
world
0
本帖最后由 hlymlv 于 2015-12-30 18:44 编辑
linux中shell变量$#,$@,$0,$1,$2的含义解释 :
变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
示例:
#!/bin/bash
printf "The complete list is %s\n" "$$"
printf "The complete list is %s\n" "$!"
printf "The complete list is %s\n" "$?"
printf "The complete list is %s\n" "$*"
printf "The complete list is %s\n" "$@"
printf "The complete list is %s\n" "$#"
printf "The complete list is %s\n" "$0"
printf "The complete list is %s\n" "$1"
printf "The complete list is %s\n" "$2"
[victor@lazy ~]$ sh 9.sh 123456 QQ
The complete list is 19754
The complete list is
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456#
The complete list is QQ#这两行为$@输出的值
The complete list is 2
The complete list is 9.sh
The complete list is 123456
The complete list is QQ
linux中shell变量$#,$@,$0,$1,$2的含义解释 :
变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
示例:
#!/bin/bash
printf "The complete list is %s\n" "$$"
printf "The complete list is %s\n" "$!"
printf "The complete list is %s\n" "$?"
printf "The complete list is %s\n" "$*"
printf "The complete list is %s\n" "$@"
printf "The complete list is %s\n" "$#"
printf "The complete list is %s\n" "$0"
printf "The complete list is %s\n" "$1"
printf "The complete list is %s\n" "$2"
[victor@lazy ~]$ sh 9.sh 123456 QQ
The complete list is 19754
The complete list is
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456#
The complete list is QQ#这两行为$@输出的值
The complete list is 2
The complete list is 9.sh
The complete list is 123456
The complete list is QQ
0
本帖最后由 hlymlv 于 2015-12-30 20:43 编辑
查看一个进程是否运行,并获得该进程的pid:
#!/bin/bash
pidof_process()
{
if [ $# -ne 1 ]
then
echo "Error,Function pidof_process() need one argument!"
exit 1
fi
#通过命令pidof获得进程的pid
local pid=`pidof "$1" |tr ' ' '\n'`
if [ -z "$pid" ]
then
echo "$1 is not running!"
return 1
else
echo "$pid"
return 0
fi
}
read -p "please enter the process's name:" n
PID=`pidof_process "$n"`
if [ $? -eq 0 ]
then
echo -e "Pid of process \"$n\":\n$PID"
else
echo -e "Pid of process\"$n\":NOT found"
fi
exit 0
[root@lazy victor]# sh 9.sh
please enter the process's name:httpd
Pid of process "httpd":
28095
28094
28031
28030
28029
28028
28027
2636
查看一个进程是否运行,并获得该进程的pid:
#!/bin/bash
pidof_process()
{
if [ $# -ne 1 ]
then
echo "Error,Function pidof_process() need one argument!"
exit 1
fi
#通过命令pidof获得进程的pid
local pid=`pidof "$1" |tr ' ' '\n'`
if [ -z "$pid" ]
then
echo "$1 is not running!"
return 1
else
echo "$pid"
return 0
fi
}
read -p "please enter the process's name:" n
PID=`pidof_process "$n"`
if [ $? -eq 0 ]
then
echo -e "Pid of process \"$n\":\n$PID"
else
echo -e "Pid of process\"$n\":NOT found"
fi
exit 0
[root@lazy victor]# sh 9.sh
please enter the process's name:httpd
Pid of process "httpd":
28095
28094
28031
28030
28029
28028
28027
2636
0
本帖最后由 hlymlv 于 2016-1-4 20:15 编辑
求阶乘(20以内):
#!/bin/bash
recursive()
{
if [ $1 -eq 1 ]
then
echo "1"
return 0
fi
#构建减一等式
m=`expr $1 - 1 `
#运用递归,实现依次减一,直至满足上面if语句
n=`recursive $m`
#阶乘de实现,n!=n*(n-1)*(n-2)*(n-3)…*1
result=`expr $1 \* $n`
echo $result
return 0
}
read -p "Enter a number(<20)to do n! operation(type "quit" to exit):" i
until [ "$i" = "quit" ]
do
case $i in
[1-9]|[1][0-9]|20)
outcome=`recursive "$i"`
echo "$i!=$outcome"
;;
*)
echo "Not a valid number,Enter again!"
;;
esac
read -p "Enter a number(<20)to do n! operation(type "quit" to exit):" i
done
echo "Bye."
exit 0
[victor@lazy ~]$ sh 10.sh
Enter a number(<20)to do n! operation(type quit to exit):11
11!=39916800
Enter a number(<20)to do n! operation(type quit to exit):quit
Bye.
while循环构建(后半部分):
while :
do
read -p "Enter a number(<20)to do n! operation(type "quit" to exit):" i
case $i in
[Qq]|[Qq][Uu][Ii][Tt])
echo "Bye."
exit
;;
[1-9]|[1][0-9]|[20])
result=`recursive $i`
echo "$i!=$result"
;;
*)
echo "Not a valid number,Enter again!"
;;
esac
done
exit 0
求阶乘(20以内):
#!/bin/bash
recursive()
{
if [ $1 -eq 1 ]
then
echo "1"
return 0
fi
#构建减一等式
m=`expr $1 - 1 `
#运用递归,实现依次减一,直至满足上面if语句
n=`recursive $m`
#阶乘de实现,n!=n*(n-1)*(n-2)*(n-3)…*1
result=`expr $1 \* $n`
echo $result
return 0
}
read -p "Enter a number(<20)to do n! operation(type "quit" to exit):" i
until [ "$i" = "quit" ]
do
case $i in
[1-9]|[1][0-9]|20)
outcome=`recursive "$i"`
echo "$i!=$outcome"
;;
*)
echo "Not a valid number,Enter again!"
;;
esac
read -p "Enter a number(<20)to do n! operation(type "quit" to exit):" i
done
echo "Bye."
exit 0
[victor@lazy ~]$ sh 10.sh
Enter a number(<20)to do n! operation(type quit to exit):11
11!=39916800
Enter a number(<20)to do n! operation(type quit to exit):quit
Bye.
while循环构建(后半部分):
while :
do
read -p "Enter a number(<20)to do n! operation(type "quit" to exit):" i
case $i in
[Qq]|[Qq][Uu][Ii][Tt])
echo "Bye."
exit
;;
[1-9]|[1][0-9]|[20])
result=`recursive $i`
echo "$i!=$result"
;;
*)
echo "Not a valid number,Enter again!"
;;
esac
done
exit 0
0
简单的函数库调用:
library.lib(需加x权限,并执行才能被调用,不能)
#!/bin/echo Warning:this is a library which should be sourced !
system()
{
local l=`uname -s`
platform=$l
}
12.sh,调用library.lib函数库:
#!/bin/bash
source ./library.lib
system
echo "Our running platform is $platform"
exit 0
[victor@lazy ~]$ sh 12.sh
Our running platform is Linux
library.lib(需加x权限,并执行才能被调用,不能)
#!/bin/echo Warning:this is a library which should be sourced !
system()
{
local l=`uname -s`
platform=$l
}
12.sh,调用library.lib函数库:
#!/bin/bash
source ./library.lib
system
echo "Our running platform is $platform"
exit 0
[victor@lazy ~]$ sh 12.sh
Our running platform is Linux
0
本帖最后由 hlymlv 于 2016-1-4 20:49 编辑
修改多个文件中的站点(自动化方式解决):
#!/bin/bash
for file in "$@"
do
sed 's/jerry.com/ace.com/g' "$file" > "$file.$$"
if [ -f $file.$$ ];then
mv -f "$file.$$" "$file"
fi
done
echo " All Done. "
exit 0
执行命令:sh 14.sh *.html
sed 实现多个命令编辑:
sed -e 's/root/lazy/g' -e 's/2015/2016/g' 1.txt
sed 's/root/lazy/g;s/2015/2016/g' 1.txt
#文件形式
sed -f file 1.txt
cat file
's/root/lazy/g'
's/2015/2016/g'sed
打印奇数行:sed -n '1~3'p 1.txt
sed打印偶数行:sed -n '0~2'p 1.txt
sed打印1到4行:sed -n '1,+3'p 1.txt
sed打印1到4行以外,!用于取反:sed -n '1,+3!'p 1.txt
注意用法 ,以上仅做参考
修改多个文件中的站点(自动化方式解决):
#!/bin/bash
for file in "$@"
do
sed 's/jerry.com/ace.com/g' "$file" > "$file.$$"
if [ -f $file.$$ ];then
mv -f "$file.$$" "$file"
fi
done
echo " All Done. "
exit 0
执行命令:sh 14.sh *.html
sed 实现多个命令编辑:
sed -e 's/root/lazy/g' -e 's/2015/2016/g' 1.txt
sed 's/root/lazy/g;s/2015/2016/g' 1.txt
#文件形式
sed -f file 1.txt
cat file
's/root/lazy/g'
's/2015/2016/g'sed
打印奇数行:sed -n '1~3'p 1.txt
sed打印偶数行:sed -n '0~2'p 1.txt
sed打印1到4行:sed -n '1,+3'p 1.txt
sed打印1到4行以外,!用于取反:sed -n '1,+3!'p 1.txt
注意用法 ,以上仅做参考
0
本帖最后由 hlymlv 于 2016-1-2 10:59 编辑
使用表格形式输出用户及其id并添加标题:
14.sh
BEGIN{
printf "%-15s%s\n","username","uid";
printf "------------\n";
}
{
printf "%-15s%s\n",$1,$3;
}
执行效果:
[victor@lazy ~]$ awk -F ':' -f 14.sh /etc/passwd
username uid
------------
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
uucp 10
%-15s 显示最小长度为15个字符串,不足的话右边补空格;%s表示参数字符串
使用表格形式输出用户及其id并添加标题:
14.sh
BEGIN{
printf "%-15s%s\n","username","uid";
printf "------------\n";
}
{
printf "%-15s%s\n",$1,$3;
}
执行效果:
[victor@lazy ~]$ awk -F ':' -f 14.sh /etc/passwd
username uid
------------
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
uucp 10
%-15s 显示最小长度为15个字符串,不足的话右边补空格;%s表示参数字符串
0
一键安装lanmp,nginx:80 apache:81
#!/bin/bash
echo "It will install lanmp."
sleep 3
##check last command is OK or not.
check_ok() {
if [ $? != 0 ]
then
echo "Error, Check the error log."
exit 1
fi
}
##get the archive of the system,i686 or x86_64.
ar=`uname -m`
##close seliux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then
setenforce 0
fi
##close iptables
iptables-save > /etc/sysconfig/iptables_`date +%F`
iptables -F
service iptables save
##if the packge installed ,then omit.
myum() {
if ! rpm -qa|grep -q "^$1"
then
yum install -y $1
check_ok
else
echo $1 already installed.
fi
}
## install some packges.
for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel
do
myum $p
done
##install epel.
if rpm -qa epel-release >/dev/null
then
rpm -e epel-release
fi
if ls /etc/yum.repos.d/epel-6.repo* >/dev/null 2>&1
then
rm -f /etc/yum.repos.d/epel-6.repo*
fi
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
##function of installing mysqld.
install_mysqld() {
cd /usr/local/src
[ -f mysql-5.1.73-linux-$ar-glibc23.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-x86_64-glibc23.tar.gz
tar zxf mysql-5.1.73-linux-$ar-glibc23.tar.gz
check_ok
[ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`
mv mysql-5.1.73-linux-$ar-glibc23 /usr/local/mysql
check_ok
if ! grep -q '^mysql:' /etc/passwd
then
useradd -M mysql -s /sbin/nologin
check_ok
fi
myum compat-libstdc++-33
[ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
check_ok
/bin/cp support-files/my-huge.cnf /etc/my.cnf
check_ok
sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
/bin/cp support-files/mysql.server /etc/init.d/mysqld
sed -i 's#^data#dir=#datadir=/data/mysql#' /etc/init.d/mysqld
sed -i 's#^basedir=#basedir=/usr/local/mysql#' /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
check_ok
break
}
##function of install httpd.
install_httpd() {
echo "Install apache version 2.2."
cd /usr/local/src
[ -f httpd-2.2.31.tar.gz ] || wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.31.tar.gz
tar zxf httpd-2.2.31.tar.gz && cd httpd-2.2.31
check_ok
./configure \
--prefix=/usr/local/apache2 \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre
check_ok
make && make install
check_ok
sed -in 's/Listen 80/Listen 81/g' /usr/local/apache2/conf/httpd.conf
sed -in 's/\#ServerName www.example.com:80/ServerName www.example.com:81/g' /usr/local/apache2/conf/httpd.conf
/usr/local/apache2/bin/apachectl start
}
##function of install lanmp's php.
install_php() {
cd /usr/local/src/
[ -f php-5.4.45.tar.bz2 ] || wget 'http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror' -O php-5.4.45.tar.bz2
tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
for p in openssl-devel bzip2-devel \
libxml2-devel curl-devel libpng-devel \
libjpeg-devel freetype-devel libmcrypt-devel\
libtool-ltdl-devel perl-devel
do
myum $p
done
check_ok
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6
check_ok
make && make install
check_ok
[ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production /usr/local/php/etc/php.ini
break
}
##function of apache and php configue.
join_apa_php() {
sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' /usr/local/apache2/conf/httpd.conf
check_ok
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' /usr/local/apache2/conf/httpd.conf
check_ok
cat > /usr/local/apache2/htdocs/index.php <
phpinfo();
?>
EOF
if /usr/local/php/bin/php -i |grep -iq 'date.timezone => no value'
then
sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"' /usr/local/php/etc/php.ini
fi
/usr/local/apache2/bin/apachectl restart
check_ok
}
##function of install nginx
install_nginx() {
cd /usr/local/src
[ -f nginx-1.8.0.tar.gz ] || wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
myum pcre-devel
./configure --prefix=/usr/local/nginx
check_ok
make && make install
check_ok
if [ -f /etc/init.d/nginx ]
then
/bin/mv /etc/init.d/nginx /etc/init.d/nginx_`date +%s`
fi
curl http://www.apelearn.com/study_v2/.nginx_init -o /etc/init.d/nginx
check_ok
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
curl http://www.apelearn.com/study_v2/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
check_ok
service nginx start
check_ok
}
##function of check service is running or not, example nginx, httpd, mysqld.
check_service() {
if [ "$1" == "httpd" ]
then
s="httpd"
else
s=$1
fi
n=`ps aux |grep "$s"|wc -l`
if [ $n -gt 1 ]
then
echo "$1 service is already started."
else
if [ -f /etc/init.d/$1 ]
then
/etc/init.d/$1 start
check_ok
else
install_$1
fi
fi
}
##function of install lanmp
lanmp() {
check_service mysqld
check_service httpd
check_service nginx
install_php
join_apa_php
echo "LANMP done,Please use 'http://your ip/index.php' to access."
}
lanmp
exit 0
#!/bin/bash
echo "It will install lanmp."
sleep 3
##check last command is OK or not.
check_ok() {
if [ $? != 0 ]
then
echo "Error, Check the error log."
exit 1
fi
}
##get the archive of the system,i686 or x86_64.
ar=`uname -m`
##close seliux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "enforcing" ]
then
setenforce 0
fi
##close iptables
iptables-save > /etc/sysconfig/iptables_`date +%F`
iptables -F
service iptables save
##if the packge installed ,then omit.
myum() {
if ! rpm -qa|grep -q "^$1"
then
yum install -y $1
check_ok
else
echo $1 already installed.
fi
}
## install some packges.
for p in gcc wget perl perl-devel libaio libaio-devel pcre-devel zlib-devel
do
myum $p
done
##install epel.
if rpm -qa epel-release >/dev/null
then
rpm -e epel-release
fi
if ls /etc/yum.repos.d/epel-6.repo* >/dev/null 2>&1
then
rm -f /etc/yum.repos.d/epel-6.repo*
fi
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
##function of installing mysqld.
install_mysqld() {
cd /usr/local/src
[ -f mysql-5.1.73-linux-$ar-glibc23.tar.gz ] || wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-x86_64-glibc23.tar.gz
tar zxf mysql-5.1.73-linux-$ar-glibc23.tar.gz
check_ok
[ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`
mv mysql-5.1.73-linux-$ar-glibc23 /usr/local/mysql
check_ok
if ! grep -q '^mysql:' /etc/passwd
then
useradd -M mysql -s /sbin/nologin
check_ok
fi
myum compat-libstdc++-33
[ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
check_ok
/bin/cp support-files/my-huge.cnf /etc/my.cnf
check_ok
sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
/bin/cp support-files/mysql.server /etc/init.d/mysqld
sed -i 's#^data#dir=#datadir=/data/mysql#' /etc/init.d/mysqld
sed -i 's#^basedir=#basedir=/usr/local/mysql#' /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
check_ok
break
}
##function of install httpd.
install_httpd() {
echo "Install apache version 2.2."
cd /usr/local/src
[ -f httpd-2.2.31.tar.gz ] || wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.31.tar.gz
tar zxf httpd-2.2.31.tar.gz && cd httpd-2.2.31
check_ok
./configure \
--prefix=/usr/local/apache2 \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre
check_ok
make && make install
check_ok
sed -in 's/Listen 80/Listen 81/g' /usr/local/apache2/conf/httpd.conf
sed -in 's/\#ServerName www.example.com:80/ServerName www.example.com:81/g' /usr/local/apache2/conf/httpd.conf
/usr/local/apache2/bin/apachectl start
}
##function of install lanmp's php.
install_php() {
cd /usr/local/src/
[ -f php-5.4.45.tar.bz2 ] || wget 'http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror' -O php-5.4.45.tar.bz2
tar jxf php-5.4.45.tar.bz2 && cd php-5.4.45
for p in openssl-devel bzip2-devel \
libxml2-devel curl-devel libpng-devel \
libjpeg-devel freetype-devel libmcrypt-devel\
libtool-ltdl-devel perl-devel
do
myum $p
done
check_ok
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6
check_ok
make && make install
check_ok
[ -f /usr/local/php/etc/php.ini ] || /bin/cp php.ini-production /usr/local/php/etc/php.ini
break
}
##function of apache and php configue.
join_apa_php() {
sed -i '/AddType .*.gz .tgz$/a\AddType application\/x-httpd-php .php' /usr/local/apache2/conf/httpd.conf
check_ok
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html index.htm/' /usr/local/apache2/conf/httpd.conf
check_ok
cat > /usr/local/apache2/htdocs/index.php <
?>
EOF
if /usr/local/php/bin/php -i |grep -iq 'date.timezone => no value'
then
sed -i '/;date.timezone =$/a\date.timezone = "Asia\/Chongqing"' /usr/local/php/etc/php.ini
fi
/usr/local/apache2/bin/apachectl restart
check_ok
}
##function of install nginx
install_nginx() {
cd /usr/local/src
[ -f nginx-1.8.0.tar.gz ] || wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
myum pcre-devel
./configure --prefix=/usr/local/nginx
check_ok
make && make install
check_ok
if [ -f /etc/init.d/nginx ]
then
/bin/mv /etc/init.d/nginx /etc/init.d/nginx_`date +%s`
fi
curl http://www.apelearn.com/study_v2/.nginx_init -o /etc/init.d/nginx
check_ok
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
curl http://www.apelearn.com/study_v2/.nginx_conf -o /usr/local/nginx/conf/nginx.conf
check_ok
service nginx start
check_ok
}
##function of check service is running or not, example nginx, httpd, mysqld.
check_service() {
if [ "$1" == "httpd" ]
then
s="httpd"
else
s=$1
fi
n=`ps aux |grep "$s"|wc -l`
if [ $n -gt 1 ]
then
echo "$1 service is already started."
else
if [ -f /etc/init.d/$1 ]
then
/etc/init.d/$1 start
check_ok
else
install_$1
fi
fi
}
##function of install lanmp
lanmp() {
check_service mysqld
check_service httpd
check_service nginx
install_php
join_apa_php
echo "LANMP done,Please use 'http://your ip/index.php' to access."
}
lanmp
exit 0
编辑回复