shell的基本语法和shell文字颜色(转载)以及LNMP全指令的搭建

Shell编程

执行脚本

1 	 bash file
2 	 sh file 
3  	 ./ file
4 	 source file
read -p "显示" num 
echo 

运算

expr
$(())
$[]
let
echo "scale=2;6/4" | bc 保留两位小数

环境变量

export 定义环境变量,当前shell和子shell
使变量在所有shell生效,可以把变量放到~/.bash_profile
所有用户都生效 /etc/profile
env 查看所有环境变量 + grep 
pstree

位置变量和预定义变量

echo "该程序名为$0"
echo "该程序使用了$#个参数"
$* 列出所有参数 =$@

$0:Shell 的命令本身
$1$9:表示 Shell 的第几个参数
$? :显示最后命令的执行情况
$#:传递到脚本的参数个数
$$:脚本运行的当前进程 ID 号
$*:以一个单字符串显示所有向脚本传递的参数
$!:后台运行的最后一个进程的 ID 号
$-:显示 Shell 使用的当前选项
原文链接:`https://blog.csdn.net/jake_tian/article/details/97274630`
练习
`输入用户名设置初始密码`
#!/bin/bash
read -p "请输入用户名 " user
read -p "请输入密码 " pass

useradd $user
echo "$pass" | passwd --stdin $user &> /dev/null
echo "$user 已创建,$user 的密码是 $pass"
history  历史命令

fdisk /dev/sdb << EOF
n
p
3

+500M
w
EOF

echo 转意字符

附上转义字符的序列:
\a      警示字符
\b      退格
\c      忽略输出中最后的换行符号。这个参数之后的任何字符,包括后面的参数都会被忽略掉。
\f      清除屏幕
\n      换行
\r      回车
\t      水平制表符号
\v      垂直制表符号
\\      反斜杠字符

文字颜色

shell echo 显示颜色 - 知乎 (zhihu.com)

格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 

例如: 
echo -e "\033[41;36m something here \033[0m" 

其中41的位置代表底色, 36的位置是代表字的颜色 


那些ascii code 是对颜色调用的始末. 
\033[ ; m …… \033[0m 



字背景颜色范围:40----49 
40:黑 
41:深红 
42:绿 
43:黄色 
44:蓝色 
45:紫色 
46:深绿 
47:白色 

字颜色:30-----------39 
30:黑 
31:红 
32:绿 
33:黄 
34:蓝色 
35:紫色 
36:深绿 
37:白色 

===============================================ANSI控制码的说明 
\33[0m 关闭所有属性 
\33[1m 设置高亮度 
\33[4m 下划线 
\33[5m 闪烁 
\33[7m 反显 
\33[8m 消隐 
\33[30m -- \33[37m 设置前景色 
\33[40m -- \33[47m 设置背景色 
\33[nA 光标上移n行 
\33[nB 光标下移n行 
\33[nC 光标右移n行 
\33[nD 光标左移n行 
\33[y;xH设置光标位置 
\33[2J 清屏 
\33[K 清除从光标到行尾的内容 
\33[s 保存光标位置 
\33[u 恢复光标位置 
\33[?25l 隐藏光标 
\33[?25h 显示光标

read

-p 打印信息 
-t 限定时间
-s 不回显
-n 输入字符个数

数组

#查看数组
declare -a
#访问数组元素
echo ${A[0]}  访问第一个元素
echo ${A[@]}  访问数组中所有的元素 等同于echo ${A[*]}
echo ${#a[@]} 统计数组元素个数
echo ${!A[@]} 获得数组元素的索引
echo ${A[@]:1} 从数组下标1开始
echo ${A[@]:1:3} 从数组下标1开始,访问三个元素

关联数组

declare -A aaaa

aaaa=([name]='ayitule' [age]=20)
echo ${aaaa[name]}

shell流程控制

if判断语句

数学运算
-eg 等于
-gt 大于
-lt 小于
-ge 大于等于
-le 小于等于
-ne 不等于
man test
小数比较 test `echo "1.5*10" | bc | cut -d '.' -f1` -eq $((2*10));echo $?
查看shell脚本执行过程 bash -x a.sh
文件比较与检查
-d 检查文件是否存在且为目录
-e 检查文件是否存在
-f 检查文件是否存在且为文件
-s 检查文件是否存在且不为空
-w 检查文件是否存在且可写
-x 检查文件是否存在且可执行
-o 检查文件是否存在并且被当前用户拥有
-G 检查文件是否存在并且默认足为当前用户组
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
	-ef 相同 

stat file1 查看文件时间
test -d /root/abc; echo $?
字符串比较运算
== 等于
!= 不等于
-n 检查字符串的长度是否大于0
-z 检查字符串的长度是否为0
逻辑运算
&& 与运算
|| 或运算
! 逻辑非运算
if语法
if [ 语法 ]
	then
		代码
fi 


if [ ]
	then
		aa
else
		aaa
fi

if [ $1 -gt $2 ]
	then
		echo "$1>$2"
elif [ $1 -eq $2 ]
	then
	echo "$1=$2"
else
	echo "$1<$2"
fi


if [[ $a==$b ]]
if ((1+3>0))

for循环语句

语法
for i in `seq 1 9` 
	do
		echo $i
done


for (( a=1,b=2;a<10;a++,b-- ))
	do
		echo $a $b
done

LNMP

[root@yue ~]# systemctl stop firewalld
[root@yue ~]# systemctl disable firewalld
[root@yue ~]# setenforce 0
setenforce: SELinux is disabled
[root@yue ~]# vim /etc/selinux/config 
SELINUX=disabled
[root@yue ~]# yum install -y nginx
[root@yue ~]# systemctl start nginx
[root@yue ~]# netstat -anpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7952/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      7952/nginx: master  
#nginx网站默认目录
[root@yue ~]# ls /usr/share/nginx/html/
404.html  50x.html  en-US  icons  img  index.html  nginx-logo.png  poweredby.png
[root@localhost ~]# yum -y install php-fpm php-mysql php-gd
[root@localhost ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@localhost ~]# netstat -anpt | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      8057/php-fpm: maste 
[root@localhost ~]# vim /usr/share/nginx/html/index.php
<?php
phpinfo();
?>
#主配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf
 46         location / {
 47         }
 48 location ~ \.php$ {
 49 root /usr/share/nginx/html;
 50 fastcgi_pass 127.0.0.1:9000;
 51 fastcgi_index index.php;
 52 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 53 include fastcgi_params;
 54 }
[root@localhost ~]# systemctl restart nginx
`http://192.168.11.129/index.php
[root@localhost ~]# vim /etc/nginx/nginx.conf
46         location / {
47         index index.php index.html
48         }
[root@localhost ~]# systemctl restart nginx
`http://192.168.11.129
[root@localhost ~]# yum install -y mariadb-server mariadb
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@localhost ~]# mysqladmin password '123456'
[root@localhost ~]# mysql -uroot -p'123456'
MariaDB [(none)]> create database bbs;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on bbs.* to  phptest@'192.168.11.129' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# vim /usr/share/nginx/html/index.php 
<?php
$link=mysql_connect('192.168.11.129','hphtest','123456');
if($link)
        echo "Successfuly";
else
        echo "Faile";
mysql_close();
?>
`http://192.168.11.129
[root@localho[root@localhost ~]# unzip wordpress-4.9.1-zh_CN.zip 
[root@localhost ~]# unzip wordpress-4.9.1-zh_CN.zip 
[root@localhost ~]# rm -rf /usr/share/nginx/html/*
[root@localhost ~]# cp -rf /root/wordpress/* /usr/share/nginx/html/
[root@localhost ~]# chown -R nginx.nginx /usr/share/nginx/html/*
[root@localhost html]# chmod -R 777 /usr/share/nginx/html/
`http://192.168.11.129

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值