1. 程式人生 > >第一階段筆記整理7.23

第一階段筆記整理7.23

abc 單個字符 開始 特殊 上一條 令行 第一個 int dip




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 óDIP?ú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)éè???a?ú???ˉ£?

cat /etc/exports
/share 192.168.3.1.0/24(rw,sync,fsid=0)











blog.csdn.net/striker_v?viewmode=contents
13585030330

第一階段筆記整理7.23