第一阶段笔记整理7.23

 
 
 
nginx 配置文件:/etc/nginx/sites-availbale/default
 
集群搭建    
    1、部署nginx反向代理三个web服务,调度算法使用加权轮询;
    Answer:      (ubuntu位置:/etc/nginx/nginx.conf)
 
     http {
          upstream pythonweb {
            server 192.168.2.101:80;
            server 192.168.2.102:80;
            server 192.168.2.103:80;
              }
        
      }
       server {
       listen 80;
       listen [::]:80;
 
       server_name www;
 
       root /var/www/index.nginx-debian.html
        index index.html;
 
        location / {
                proxy_pass http://pythonweb;
        }
 
 
round-robin 轮询
least-connected 最少链接
ip-hash            哈稀,会话保持;
 
 #nginx -v
 
lsof -i:80 查看80端口运行的程序;
#################################################################################
crontab -e -u root  
echo `date +%F_%T`
 
****** /usr/bin/sh /root/check.sh
 
 
/usr/bin 全局执行命令;
 
#################################################################################
关于 if [ "$1" = 'install'] 字符串比较最好加双引号 " "
 
if [ "$1" = 'install' ]
then
    echo '....'
 
数字加减没有空格[1+2]
function start(){
    echo '===========';
    res=$[1+2];
    return $res
}
start
 
CASE 使用:
 
read -p '-->>:' uname
case $uname in
root)
echo "welcome $uname"
;;
seker)
echo "welcome $uname"
;;
default)
echo "welcome $uname"
;;
*)
echo "no user $uname"
esac
 
 
 
 
 
ping -c1 192.168.2.254 &> /dev/null    黑洞文件; 把ping执行的结果产生的信息drop to /dev/null
 
for i in {1..10}
do
    echo $i
done
 
for i in `ls /boot`  取得ls目录下的文件列表;
do
    echo $i
done
 
if [ $name = 'alex' -a $psd = 'alex4714'] 方括号内字符串与=必须有空格; -a 不能用&&
if [ $name = 'alex' ] && [ $psd = 'alex4714' ]
 
报错解决: 参数太多!
字符串如果比较单个字符串;  但是出入的字符串如果超过2个 如: cd /  需要用双引号表示”软引用“ "cd /"----> "$cmd"
  if [ "$cmd" = 'quit' ]
 
echo "aaa\nbbb\nccc"         aaa\nbbb\nccc
echo -e "aaa\nbbb\nccc"      aaa
                             bbb
                             ccc
 
cat /etc/passwd |cut -d: -f2
awk -F: '{print $(NF-1)}' b 最后第二段;
 
echo $0 脚本文件名;
echo '$$' $$ 脚本进程号 $# 总共有多少个参数;(./5.sh 1 2 3 4 5 6 7 8 )  
 
 
 
向脚本传递参数:
./4.sh a b c d           .sh后面参数为$1 $2 $3  
#!/bin/bash
echo $0
echo $1
 
文件放在/usr/bin  即可运行如:1.sh
#!/bin/bash
echo $1
if [ -f $1 ]
     then
        echo "$1 is regular file"
elif [ -b $1 ]
      then
          echo "$1 is block"
fi
 
在终端里面写命令行,需要有;分隔如:  
#x=1
#if [ $x eq 1 ];then echo 'x is 1';fi
 
test 文件测试;字符串;整形数字;
测试命令: test[] [[]] (())
test -d /etc    #echo $?   #0
[ -d /etc ]  #echo $?  #0   测试目录存在 -d
[ -e /etc/ ]    -e 目录下的文件是否存在
[ -f /etc/passwd ] -f 目录下的某个文件是否存在;
[ -h /tmp/a.txt ]  -h 查询目录下是否有某个链接文件
-w 可写;[ -w /etc/passwd ]
-r 可读
-s 文件存在并且内容非空[ -s /etc/passwd ] 看文件大小 ll -h /etc/passwd
 
字符串测试:
=  x='hello' y='world' [ $x = $y ]  
!=  
-z 空串    
-n 非空串  
 
数值测试: -eq  -ne (不等于) -gt 大于  -lt 小于 -ge大于等于, -le小于等于
 
 
 
 
 
men_total=` free | awk 'NR==2{print $2}' `
men_use=`free | awk 'NR==2{print $3}'`
echo "scale=2;$men_use/$men_total"|bc -l
 
echo 'scale=2;30/1000' | bc -l   分号; scale保留小树位数;
 
#x=1
#y=2
#expr $x + $y
#res=`expr $x + $y
#echo $res
#3`
 
 
x=x+1   x+=1
x=x*3    x*=3
x=x/3    x/=3
x=10
((x%=3))
echo $x
 
((i+=1))
 
 
#x=1
#x=$[$x+1]
#echo $x
#2
 
(()) 可以正常使用比较符号;
#((2>10))
#echo $?
#1
 
#[ 2 > 1 ]  中括号号的语法结构跟test一样; [ 2 -gt 1]
#echo $?
#0
 
test===========>[]
 
设置全局变量: export money=1000
su - zjf  进入zjf bash
# env 系统变量
#cd - 返回上一次的目录
echo 显示 ,$是取值;
x=1 赋值, 取消赋值 unset x
 
统计某一个目录下文件的大小; du -sh /home/zjf/
寻找普通文件 -type f     #find / -type f
寻找文件名称 -name "*.txt" # find / -name "*.txt"
找大于30M文件 find / -size +30M
 
cat a |sort 内容显示排序;  
cat  a |sort |uniq  去重;  
cat  a |sort |uniq -c   -c看去了重复行有多少;
简单处理:cut
cat a | cut -d: -f1,3  取 第一,第三字段;F后面没有空格; d后面的冒号为:以冒号为分隔符; 取后面的第几部分;
 
 
不指定 F 以空格为单位;取ifconfig 的IP
ifconfig | awk 'NR==2{print $2}'
反引号`` 引用 保存给变量 : ip_addr=`ifconfig | awk 'NR==2{print $2}'`   
                          echo $ip_addr
 
自己定义变量,把变量传进去; count=7  -V 定义变量;
awk -v x=$count -F: '$3>x{print NR,$3 }' test
awk -F: '$1=="root"{print NR,$1,$3}' test 第一行是root 打印$1,$3
awk -F: '$1~/^r.*t$/{print NF,$3}' test
awk -F: '/nologin$/{print $1}' test 以nologin结尾的,打印用户名; // 正则定制;
awk -F: 'NR<=3 || NR >=5{print NR,"-------",$1}' test  
awk -F: 'NR>=3 && NR<=5{print NR,"-------",$1}' test  
awk -F: 'NR<=3{print NR,"-------",$1}' test 单引号内使用双引号隔开;
awk -F: 'NR==1{print $1,NR}' test   
awk -F: 'NR<=3{print $1,NR}' test
 
 
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
内容匹配正则: $1~//     根据分隔符匹配的某一段内容:$2 , $1 等来匹配//  
 
awk -F: '$1~/^r.*t$/{print NR, $3}' test
 
#awk -F: '{print $1,NR}' test  NR 行号;
#awk -F: '{print $1,NF}' test
 
#root 7   NF 代表有几段内容; NR 行号;
 
awk -F: '定址+{命令}' /etc/passwd 命令如:print   $0 取所有; $NF 最后一段;
 
#awk -F: '{print $1,$4}' test 取以冒号为分隔符; 第一个,第四个;
 
取最后一段: awk -F: '{print $1,$NF}' test 取第一段,最后一段,以冒号为分隔符 ;
 
 
#root 0
       
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 
 
 
egrep 'r[abc123\-\/]t' b.txt --------->r1t r2t
egrep 'r.t' b.txt -------------------->r1t r2t r/t r-t  -必须在结尾才能转义出来;
.是任意一个,[]是定义一个范围;
egrep 'r[a-zA-Z]t' b.txt 等价 egrep 'r[a-Z]t' b.txt rOt
egrep '^[0-9]' test  0-9开头的字符行;
 
egrep '^[^0-9]' test  非0-9开头的字符行;
egrep 'compan(y|ies)' a.txt
egrep -e '' -e ''     -e指定多个规则;
 
 
 
 
 
egrep 'ab?' a.txt 打印?左边字母所在的行;
 
egrep 'ab{3}' a.txt  #ab{3}-->abbb abbbb
egrep -w 'ab{3}' a.txt ------>abbb   -w匹配单词;  
或者 egrep 'ab{3}$' a.txt
egrep 'ab{2,4}' a.txt  指定2个到4个b  
egrep 'ab{2,}' a.txt 2个无穷个b;
 
 
 
$dpkg -l查看版本号;
执行卸载:$sudo apt-get remove virtualbox-4.2
#########################################################################################################
正则定位:
 分两步: 1、定址 2、操作 '/定址/操作(命令) 内容' sed '/^s/c 11111' test
 
sed -r '/^[0-9]([a-Z]+)xsb$/ s/sb/SB/g' test2
sed -r '/^[0-9][a-Z]{3}xsb$/ s/sb/SB/g' test2 正则// + 命令///
 
sed -r 扩展正则;
******************************************************
命令的正则使用: (部分课后作业)
 
s/// ----------> s/()()/\1/ 取()() 的第二部分
例: sed -r 's/^([a-Z]+)([^a-Z])/\2/g' test
去除行最后一个单词(保留非字母符号)
sed -r 's/([^a-Z])([a-Z]+)$/\1/g' test
 
第一部分 跟 第三部分 调换:
sed -r 's /^([a-Z]+)([^a-Z]+)([a-Z]+)([^a-Z])/\3\2\1\4/g' test
第一个单词与行最后一个单词交换位置;
sed -r 's/^([a-Z]+)([^a-Z])(.*)([^a-Z])([a-Z]+)$/\5\2\3\4\1/' test
去除数字:(所有行)
sed -r 's/[0-9]//g' test
删除字母前面的空格:
sed -r 's/^(\ +)([a-Z]+)/\2/g' test
把所有大写字母用括号()括起来
sed -r 's/[A-Z]/(&)/g' test
每行打印三次  
sed -r 'p;p' test
隔行删除 (从第一行开始删,2d每隔一行删,3d每隔2行删;)
sed '1~2d' test
去除每行第一个字符:
sed -r 's/^(.)(.*)/\2/' test
去除第二个字符:
sed -r 's/^(.)(.)([a-Z]+)/\1\3/' test
 
去除第二个单词 password 文件的copy test
sed -r '/^([a-Z]+)([^a-Z])([a-Z]+)([^a-Z])/\1\2\4/' test
 
只显示第一个单词: (.*)
sed -r 's/^([a-Z]+)([^a-Z])(.*)/\1/' test
 
显示第一个,第三个单词
sed -r 's/^([a-Z]+)([^a-Z]+)([a-Z]+)([^a-Z]+)([a-Z]+)([^a-Z]+)(.*)/\1\ \3/' test
 
去除每行第二个字符
sed -r 's/^(.)(.)([a-Z])/\1\3/' test
去除最后一个字符;
sed -r '/([^a-Z])([a-Z]+)([a-Z]$)/\1\2/' test
交换没一行第一个,第二个字符;
sed -r '/^(.)(.)([a-Z]+)([^a-Z])/\2\1\3\4/' test
 
 
 
 
 
 
 
*******************************************************
sed '/alex/s/sb/SB/g' test2 正则定位+命令#!/bin/bash
echo "开始安装......."
yum install rpcbind -y  
yum install nfs-utils -y
echo "安装完成,开始更改配置文件......"
echo "挂载共享文件......."
a=`exportfs |awk  '{print $1}'`
mount -t nfs 192.168.16.254:$a /usr/share/nginx/html
echo "配置完成,启动服务......"
systemctl start rpcbind
systemctl start nfs
exportfs
sed '/^root/d' test  定址+操作  
sed '/sb.*$/d' test2  定址+操作
sed '/^s/c 11111' test 定址+操作+内容
##########################################################################################################
sed '4s/sb/SB/g' test2 明确行定位+命令
sed 's/sb/SB/g' test2 匹配行内所有符合字符串; 命令无定位
sed 's/sb/SB/' test2 只匹配一行的第一个;
sed '1d;3d' test  删1跟第3行
sed '1,3d' test   删1-3行;
sed 's///' test2
**********************************************************
-n 取消默认输出
sed -n '3p' test  取消源文件输出,打印源文件第三行;
sed '3p' test 第三行输出再次一遍;不影响原第三行;
sed '3c 111111' test 第三行前插入c后面的值;sed '3i 111111' test  
sed '3a 111111' test 第三行后面追加插入c后面的值;
sed '3d' test  '3' 代表第三行, d 删除  
 
#############################################################################
sed '' test
sed -n '' test   静默模式,取消默认输出(源文件内容);
sed -e '' test    多个规则;
sed -i '' test      内容写到文件里面;对文件直接修改;不加打印效果
################################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
lsblk 系统硬盘
chown -R 用户名称 [文件或目录]
chown -R [用户名称:组名称][文件或目录]
 
 
1、取以^开头的行: grep '^s' /etc/passwd
2、以。。。结尾的  grep 'bash$' /etc/passwd
3、'.' 表示任意一个字符;grep '^b.n' /etc/passwd
4、 *   以*左边(第一个)开头匹配,*左边有0个或无穷个; (必须第一个就有才能匹配;)grep 'ab*' a.txt   必须以a开头的或ab开头的才能匹配;
5、 '+' (+左边那一个字符)有一个或无穷个以上;egrep 'ab+' a.txt 以ab开头的行;
6、?:左边的那个字符有0个或1个;
7、{N}左边那个字符有N个;
8、{N,M}左边那个字符有N-M个;
9、{N,M}左边那个字符有N-无穷个;
10、[a-z]:所有小写字母
11、[A-Z]:所有大写字母
12、[a-Z]:所有大写字母 [a-zA-Z]  
13、[^a-Z]+ 不是字母的单词
 
 
在非KDE桌面环境下,如果安装了fcitx-module-kimpanel,可能会导致Fcitx输入中文时不显示候选词框,移除该组件,然后重启Fcixt。
 
$ sudo apt remove fcitx-module-kimpanel
 
 
取文件开头前10行copy到新文件:    head -10 /etc/passwd > a.txt
 
grep 'root' /etc/passwd
 -n 匹配行显示
-o 只显示匹配内容;
-q 不打印;过滤内容 grep -q 'adfadfaf' /etc/passwd   echo  $?  1 内容没有;
-A 显示过滤内容及其后几行内容: grep -A 2 'root' /etc/passwd
-B 显示过滤内容及其前几行内容   grep -B 2 'root' /etc/passwd
-C 显示过滤内容及其前后几行内容 grep -C 2 'root' /etc/passwd
-c 显示匹配的行数
-i 过滤大小写内容        grep -i 'hello' a.txt
-v 取反(内容)            grep -iv 'hello' a.txt
 
 
 
############
/etc/profile ----> /etc/bash.bashrc ------>/etc/profile.d/*.sh
用户/profile 执行用户下面的 .bashrc
######################################################################
 
/etc/profile               ____________ubuntu has
/etc/bashrc
/root/.bashrc 当前用户目录下的__________ubuntu has
/root/.bash_profile 当前用户目录下的
 
但是万事都不是一样的,debain系列 的是不同的,如ubuntu
 /etc/profile-->/etc/environment-->$HOME/.profile
 
Ubuntu Linux系统环境变量配置文件介绍在Ubuntu中有如下几个文件可以设置环境变量
 
/etc/profile:在登录时,操作系 统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。
 
/etc /environment:在登录时操作系统使用的第二个文件, 系统在读取你自己的profile前,设置环境文件的环境变量。
 
~/.profile: 在登录时用到的第三个文件 是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认 情况下,他设置一些环境变量,执行用户的.bashrc文件。
 
/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.
 
~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。
 
 
 
 
grep
sed
awk
 
 
? 代表任意一个字符;
ls ?.doc
ls ?.txt
 
while :;do echo 123;sleep 0.5;done
类似:
while :;
    do
    echo 123;
    sleep 0.5;
    done
 
 
 
: 永远为真
echo $?
0
 
 
;分割多个命令
ls;pwd;echo 123;pwd
 
 
''硬引用;''内特殊符号都没有特殊含义;
""软引用;""内的特殊符号都有特殊意义;
 
\转义字符;
x=1
echo "$x"
echo "\$x"
$x
 
{}如:添加
money=10
echo $money
10
echo ${money}0000
100000
 
 
(x=2) () 为子进程运行内容;   
 
|| (逻辑或)
pwd || echo 123  (第一个执行成功了,第二个不执行;第一个执行不成功,执行第二个;)
 
firefox & 后台执行; &
pwd && echo 123  在左边命令执行成功的情况下才执行右边的;(逻辑与)
引用: make && make install
 
[]整数的加减乘除;
echo $[1+10] 例;
 
y=2
x=1
[ $y>$x ]
echo &?
0
 
 
echo $?  如果是0证明是执行成功;非0 上一条命令执行失败;
# 0   
 
touch {a..c}.txt
touch {1..10}.txt
ls [!0-9].txt
 
ls [abcd].txt 每样取一个;
#a.txt b.txt c.txt d.txt
取AA.TXT
ls[abcd][a].txt
aa.txt
 
ls [a-z].txt
 
 
x=`ls /tmp`(反引号的嵌套有问题;)
echo $x
 
y=$(ls)
echo $y
 
 
fucnction cd () { echo 123; }
cd
123
取消CD命令:
unset cd
 
 
read命令:
read -p 'pls input your hostname:' name
#echo $name
取name值:echo $name
 
# function test() { hostnamectl set-hostname www;hostname; }
 
hostnamectl set-hostname www
 
source a.sh  没有执行权限也能运行;
 
Linux修改用户所在组方法
强行设置某个用户所在组
usermod -g 用户组 用户名
把某个用户改为 group(s)  
usermod -G 用户组 用户名
把用户添加进入某个组(s),注意:原来的用户组还存在
usermod -a -G 用户组 用户名
 
ifconfig ens33 192.168.16.48/24 改ip
 
route add default gw 192.168.2.254 netmask 255.255.255.0
route -n
 
which ifconfig
/usr/sbin/ifconfig
rpm -qf /usr/sbin/ifconfig
net-tools-2.0-0.17.20131004git.el7.x86_64
 
arping -I enss 192.168.2.152  ÓÐIPžúMAC
sudo ufw status
 
service rpcbind restart
service nfs-kernel-server restart
 
systemctl status rpcbind
systemctl status nfs-kernel-server
 
exportfs
showmount -e 查看自己共享的;showmount -e 192.168.2.100
showmount -a  查看已经挂载的;  
 
mount 192.168.16.147:/share /opt/nfs_folder
 
 
systemctl enable nfs-server.service(centos)ÉèÖÿª»úÆô¶¯£»
 
cat /etc/exports
/share 192.168.3.1.0/24(rw,sync,fsid=0)
 
 
 
 
 
 
 
 
 
 
 
blog.csdn.net/striker_v?viewmode=contents
13585030330

原文地址:https://www.cnblogs.com/santizhou/p/7225088.html