第三周 培訓重點內容:
Linux操作起來真的非常好玩;
我喜歡敲鍵盤的感覺,但是自己玩的不留
我希望每天都盡力去玩Linux,希望有所成就吧!我希望我能堅持下去!
以下是第三周整理的內容:
第三周重點內容
腳本:
[root@Centos7/app]#./infosystem.sh
hello world
The host is Centos7.4.localdomain
The kernel is 3.10.0-693.el7.x86_64
The cpu is Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
MemTotal: 2031912 kB
[root@Centos7/app]#vim reset.sh
[root@Centos7~]#mkdir bin
[root@Centos7~]#mv /app/infosystem.sh ~/bin/
[root@Centos7~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/sun
[root@Centos7~/bin]#bash -x infosystem.sh
[root@Centos7~/bin]#bash -n infosystem.sh
變量:
[root@Centos7~]#echo $$ 當前進程:
[root@Centos7~]#echo $PPID 上級進程:
[root@Centos7~]# name=whoami
[root@Centos7~]#echo $name
root
[root@Centos7~]#echo $name
\S Kernel \r on an \m
[root@Centos7~]#cat /etc/issue
\S
Kernel \r on an \m
[root@Centos7~]#echo "$name"
\S
Kernel \r on an \m
[root@Centos7/bin]#vim ps.sh
#!/bin/bash
name=parent
son.sh
[root@Centos7/bin]#vim son.sh
#!/bin/bash
name=son
echo "ps.sh:name $name"
sleep 100
執行結果:
[root@Centos7/bin]#chmod +x *[root@Centos7/bin]#ps.sh
br/>[root@Centos7/bin]#ps.sh
ps.sh:name son
全局變量
export name=son
set 變量
env
export
declare -r
declare -x
[root@Centos7~/bin]# name=test;(echo $name ; name=112;echo $name); echo $name
test
112
test
[root@Centos7/etc]#cd /etc/;pwd
/etc
[root@Centos7/etc]#cd
[root@Centos7~]#(cd /etc/;pwd)
/etc
[root@Centos7~]#
[root@Centos7~]#(umask 066; touch /app/f1)
[root@Centos7~]#ll /app/f1
-rw-------. 1 root root 0 May 27 14:23 /app/f1[root@Centos7~]#umask
br/>[root@Centos7~]#umask
[root@Centos7/app]#( ls;pwd ) > /app/all.log
[root@Centos7~]#(name=haha;touch /app/$name.bak)
腳本練習
[root@Centos7~/bin]#vim infosystem.sh
#Author:sun
#Date 2018-5-27
#Description:show system info
echo "hello world"
echo "The host is hostname
"
echo "The kernel is uname -r
"
echo "The cpu is lscpu |grep "Model name:"|tr -s ‘ ‘ |cut -d: -f2
"
cat /proc/meminfo |head -n1
cat /etc/centos-release
echo "HardWare is lsblk |grep -w sda |tr -s " "|cut -d‘ ‘ -f1,4,6
"
echo "The IP is ifconfig ens33|grep netmask |tr -s " "|cut -d" " -f3
"
[root@Centos7~/bin]#
echo $? 0表示成功
[root@centos ~/bin]#i=10
[root@centos ~/bin]#j=10
[root@centos ~/bin]#let sum=$i+$j
[root@centos ~/bin]#echo $sum
20
腳本:
[root@centos ~/bin]#var=haha;[ -n "$var" ] && echo true ||echo false
true
[root@centos ~/bin]#unset var[ -n "$var" ] && echo true ||echo false
false
[root@centos ~/bin]#var=haha; [[ "$var" =~ ha ]] && echo true || echo false
true
=~ 支持正則表達式:
[root@centos ~/bin]#var==haha; [[ "$var" =~ h+ ]] && echo true || echo false
true
[root@centos ~/bin]#var=haha;[[ "$var" =~ h? ]] && echo true || echo false
true
[root@centos ~/bin]#var=hac;[[ "$var" =~ .. ]] && echo true || echo false
true
[root@centos ~/bin]#var=hac;[[ "$var" =~ ^..$ ]] && echo true || echo false
false
判斷文件以sh結尾:執行:
[root@centos ~/bin]#filename=f1.sh ;[[ "$filename" =~ .sh$ ]] && echo true ||echo false
IP地址的判斷:
[root@centos ~]#ip="172.18.19.11"; [[ "$ip" =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" ]] && echo true ||echo false
IP地址判斷有效性:
[root@centos ~]#ip="172.18.19.159" ;[[ "$ip" =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] && echo true ||echo false
匹配數字全數字:
[root@centos ~]#var=132 ; [[ "$var" =~ ^[0-9]+$ ]] && echo true ||echo false
判斷正整數:
[root@centos ~]#var=1 ; [[ "$var" =~ ^[1-9][0-9]$ ]] && echo true ||echo false
true
判斷正整數:[root@centos ~]#var=01 ; [[ "$var" =~ ^0[1-9][0-9]*$ ]] && echo true ||echo false
判斷手機號:
[root@centos ~]#mobile=18512554331; [[ "$mobile" =~ ^1[3456789][0-9]{9} ]] && echo true ||echo false
true
系統自帶的腳本:
[root@centos ~]#vim /etc/rc.d/rc.sysinit
效果如下:
Print a text banner.
echo -en $"\t\tWelcome to "
read -r system_release < /etc/system-release
if [[ "$system_release" == "Red Hat" ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\033[0;31m"
echo -en "Red Hat"
[ "$BOOTUP" = "color" ] && echo -en "\033[0;39m"
PRODUCT=$(sed "s/Red Hat (.) release./\1/" /etc/system-release)
echo " $PRODUCT"
elif [[ "$system_release" == Fedora ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\033[0;34m"
echo -en "Fedora"
[ "$BOOTUP" = "color" ] && echo -en "\033[0;39m"
PRODUCT=$(sed "s/Fedora (.) \?release./\1/" /etc/system-release)
echo " $PRODUCT"
elif [[ "$system_release" =~ "CentOS" ]]; then
[ "$BOOTUP" = "color" ] && echo -en "\033[0;36m"
echo -en "CentOS"
[ "$BOOTUP" = "color" ] && echo -en "\033[0;39m"
判斷數字大小:判斷是之前是不是數字:
[root@centos ~/bin]#num=10; [ "$num" -eq 10 ] && echo true ||echo false^C
[root@centos ~/bin]#
[root@centos ~/bin]#num=10; [ "$num" -ne 10 ] && echo true ||echo false
false
[root@centos ~/bin]#num=10; [ "$num" -ne 10 ] && echo true ||echo false
false
[root@centos ~/bin]#num=10; [ "$num" -ne 1 ] && echo true ||echo false
true
判斷變量是否被定義過:
[root@centos ~/bin]#unset var ;[ -v var ] && echo true ||echo false
[root@centos ~/bin]#var=" "; [ -v var ] && echo true ||echo false
查看顏色:
echo -e "\033[1;5;31;43mHi,dangerous!\033[0m"
vim /etc/profile.d/env.sh 配置環境
查看目錄是否存在
[root@centos ~/bin]$[ -d /app/backa ] ||mkdir /app/backa && cp /etc/passwd /app/
查看文件是否存在
[root@centos ~/bin]$[ -e /etc/passwd ] && echo true || echo false
true
讀寫權限:
[root@centos ~/bin]$[ -r /etc/passwd -a -w /etc/passwd ] && echo ture
ture
[root@centos ~/bin]$[ -r /etc/passwd -o -w /etc/passwd ] && echo ture
ture
實驗:寫一個創建用戶的腳本,會詢問用戶用戶名及密碼,要求密碼輸入過程不可見
方法一:
#!/bin/bash
read -p "Please Enter your username: " name
echo -n "Please Enter the password for $name: "
stty -echo
read password
stty echo
echo
useradd $name
echo $password |passwd --stdin $name &> /dev/null
echo "$name created"
方法二:
#!/bin/bash
read -p "Please Enter your username: " name
read -sp "Please Enter the password for $name: " password
echo
useradd $name
echo $password |passwd --stdin $name &> /dev/null
echo "$name created"
多條件判斷
方法一:
[liubei@instructor_v7(nanyibo) ~]$ [ -r /app/passwd ] || [ -w /app/passwd ] && echo true
[liubei@instructor_v7(nanyibo) ~]$ [ -r /app/passwd ] && [ -w /app/passwd ] && echo true
方法二:
[liubei@instructor_v7(nanyibo) ~]$ [ -r /app/passwd -a -w /app/passwd ] && echo true
[liubei@instructor_v7(nanyibo) ~]$ [ -r /app/passwd -o -w /app/passwd ] && echo true
[liubei@instructor_v7(nanyibo) ~]$ [ ! -e /etc/passwda ] && echo true
true
實驗:寫一個創建用戶的腳本,會詢問用戶用戶名及密碼,要求密碼輸入過程不可見
方法一:
#!/bin/bash
read -p "Please Enter your username: " name
echo -n "Please Enter the password for $name: "
stty -echo
read password
stty echo
echo
useradd $name
echo $password |passwd --stdin $name &> /dev/null
echo "$name created"
方法二:靜默輸出
#!/bin/bash
read -p "Please Enter your username: " name
read -sp "Please Enter the password for $name: " password
echo> /dev/null
echo "$name created"
useradd $name
echo $password |passwd --stdin $name &
輸入5個數:
[root@centos /app]$read -N 5 -p "hello: " name
hello: 12345[root@centos /app]$echo $name
12345
實驗:輸入密碼小於等於8位數:
第8天:
判斷文件
[root@centos ~]$touch a
[root@centos ~]$chmod g+s a
[root@centos ~]$[ -g a ] && echo true
true
[root@centos ~]$[ -g a ] && echo true^C
[root@centos ~]$touch a.txt
[root@centos ~]$[ -g a.txt ] && echo true
[root@centos ~]$ls /proc/$$/fd
0 1 2 255
compress 壓縮文件:
umask:chmod 000 rw
權限會 被修改:
666-umask=600
gzip :
-c; 壓縮文件原文件不變,權限會根據uMask 變化
-cd 標準輸出,umask 會變
gzip :可以改變壓縮比
for n in {1..9};do gzip -$n -c passwd >passwd.gz.$n;done
while true ;do ;done
壓縮比:默認是6.數字越小,壓縮比越小速度快
gzip -$n -c passwd
bzip2:
bzip2 -k passwd :原文件不丟,權限也在
bzip2 -dk passwd :
bzip2 -cd passwd : 解壓縮文件,預覽解壓後的文件標準輸出
文件描述符:打開一個文件有多少個描述符:
[root@centos /app]$ls /proc/$$/fd
0 1 2 255
[root@centos /app]$ls /proc/$$/fd |wc -l
4
判斷軟連接及路徑: 先判斷是不是軟軟連接
[root@Centos7~]#[ -d /lib ] &&echo true
true
[root@Centos7~]#[ -L /lib ] &&echo true
true
判斷所有者和所屬組:
[root@centos ~]$[ -O /etc/passwd ] && echo true
true
[root@centos ~]$[ -G /etc/shadow ] && echo true
true
[root@centos ~]$a=10;[ $a -eq 10 ] && echo true ||echo false
true
判斷文件是不是可寫,可讀:
[root@centos ~]$[ -r /etc/issue ] && [ -w /etc/shadow ] && echo true
true
[root@centos ~]$[ -r /etc/issue ] && [ -x /etc/shadow ] && echo true
[root@centos ~]$[ -r /etc/issue ] && [ -x /etc/shadow ] && echo true
判斷空文件賦值:
vim /etc/rc.d/rc.sysinit
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
HOSTNAME=localhost
查看文件系統:類型
[root@centos ~]$[ -f /bin/cat -a -x /bin/cat ] && cat /etc/fstab
root也沒有執行權限:切記:
[root@centos ~]$[ -x /etc/shadow ] && echo true ||echo false
false
[ ! -r /etc/shadow ] && [ ! -w /etc/shadow ] && echo true
摩根定律:
[root@centos ~]$ [ ! ( -r /etc/shadow -o -w /etc/shadow ) ] && echo true
[root@centos ~]$[ ! -r /etc/shadow -a ! -w /etc/shadow ] && echo true
普通用戶不能登陸:
touch /etc/nologin
echo disable common user login > /etc/nologin
靜默輸入密碼:
[root@Centos7~/bin]#read -s -p "please input your password: " passwd
輸入密碼的次數:
[root@Centos7~/bin]#read -n 5 -p "please input you name: " username
判斷yes /no
[root@Centos7~]#yes=yn;[[ "$yes" =~ ^Yy?$ ]] && echo true ||echo false
false
[root@Centos7~]#n=no; [[ "$n" =~ ^[Nn][oO]?$ ]]&& echo true ||echo false
true
整體判斷:
[root@Centos7~]#n=n; [[ "$n" =~ ^Yy?$|^[Nn][oO]?$ ]]&& echo true ||echo false
true
判斷yes no 腳本:
read -p "Do you agree ? yes or no: " yes
[[ "$n" =~ ^Yy?$|^[Nn][oO]?$ ]]&& echo true ||echo false
判斷yes no 的腳本
#!/bin/bash
read -p "Do you agree ? yes or no: " choice
choice=$(echo "$choice" | tr "[ :upper: ]" "[ :lower: ]")
[ "$choice" = "yes" -o "$choice" = "y" ] && echo "you enter yes " && exit 1
[ "$choice" = "no" -o "$choice" = "n" ] && echo "you enter no " && exit 2 ||
echo "you enter wrong "
最後一行命令結束,自動退出終端,切記:
[root@Centos7/app]#echo "your cost : \$5.00"
your cost : $5.00
函數庫:
/etc/rc.d/init.d/functions
查看文件系統:
/etc/fstab
User specific environment and startup programs
#cat .bash_profile 更改路勁
PATH=$PATH:$HOME/bin:/home/sun
User specific aliases and functions
vim ~/.bashrc
source 與bash 的區別
source 影響當前進程:
bash 開啟子進程:
[root@Centos7~/bin]#source procss_bash.sh
var=haha
var=xxx
[root@Centos7~/bin]#bash procss_bash.sh
var=
var=xxx
[root@Centos7~/bin]#
用戶退出提示:
~/.sh_logout
echo -e "\033[1;5;31;43m 辛苦了親!每天好心情,day day up!\033[0m"
$-變量值:
[root@Centos7~]#echo $-
himBH
h hash
i 交互式:
m:monitor:
b: braceexpand 大括號擴張
h:history
腳本中:
[root@Centos7~/bin]#f1.sh
hB
數據庫查找:
locate
[root@Centos7~]#ll /var/lib/mlocate/mlocate.db
更新數據庫:
[root@Centos7~]#ll /var/lib/mlocate/mlocate.db
-rw-r-----. 1 root slocate 3588970 May 31 09:27 /var/lib/mlocate/mlocate.db
[root@Centos7~]# updatedb
[root@Centos7~]#locate newf1.sh
/root/newf1.sh
locate 支持正則表達式:
[root@Centos7~/bin]# locate -n 3 -r ".conf$"
/app/httpd-2.4.33/docs/doxygen.conf
/app/httpd-2.4.33/docs/conf/httpd.conf
/app/httpd-2.4.33/docs/conf/extra/httpd-autoindex.conf
find 命令:
查找路徑:
查找條件
處理動作
查找最大2層,最小2層
[root@Centos7~/bin]#find /etc -maxdepth 2 -mindepth 2 -name "network"
/etc/sysconfig/network
查看鏈接數:
[root@Centos7/app]#find -samefile a
./a
./d1/aa
查找文件以.sh結尾
[root@Centos7/app]#find /root -regex "..txt$"
查看問價以.sh結尾或.txt結尾的文件
根據屬組查找:
-user USERNAME:查找屬主為指定用戶(UID)的文件
-group GRPNAME: 查找屬組為指定組(GID)的文件
-uid UserID:查找屬主為指定的UID號的文件
查找所屬主 及文件
[root@Centos7~]#find /home -user sun -name ".sh"
即不是王的文件也不是.sh 結尾的文件
[root@Centos7/app]#find /home !( -user sun -o - name "*.sh")
find /home -not -user wang -o -not -user bai
不是王的,或者不是白的,那就是全部文件
ias.txt
./d1
[root@Centos7/app]#dd if=/dev/zero of=f1 bs=1 count=1024
生成文件
查看1k 的文件:
[root@Centos7/app]#find -size lk
查看文件大小來查看;
6k
6k:(5k,6k]
-6k:[0,5k]
+6k:(6k,無窮]
大於50M 小於100根目錄下
find / -size +50m -size -101m
根據時間查找文件:
-atime
:[#,#+1)
+#[#+1,無窮]
-#[0,#)
-amin
-mmin
-cmin
find /etc/ -mmin -l 一分鐘時間
-mtime
-ctime
權限查找:權限644,文件類型為 f (精確匹配)
[root@Centos7/app]#find -perm 644 -type f
./f1
並且:
[root@Centos7/app]#find -perm -444 -type f
[root@Centos7/app]#find -perm -440 -type f
./f1 (0:表示不關心)
或
[root@Centos7/app]#find -perm /444 -type f
./f1
任何人都投寫權限
[root@Centos7/app]#find -perm /222 -type f
./f1
全部匹配:表示不關心
[root@Centos7/app]#find -perm -000 -type f
./f1
find -perm /002 -002 -type f 一樣的
一位
找到文件刪掉:
[root@Centos7/app]#find -perm -020 -type f -delete
文件重定向: 動作:
[root@Centos7/app]#find -perm -020 -type f -ls > list.txt
文件備份:提示:不提示:
[root@Centos7/app]# find -name ".conf" -ok cp {} /root/{}.bak \;
不提示:
[root@Centos7/app]#find -name ".conf" -exec cp {} /root/{}.bak \;
刪除查找到的文件:
[root@Centos7/app]#find -name "*.conf" -exec rm {} \;
批量創建文件:
echo test{1..10}|xargs touch
壓縮:
gzip passwd
gzip -d passwd.gz
bzip2 passwd
bzip2 -d passwd.bz2
bzip2 -k passwd 保留源文件
xz passwd
xz -d passwd.xz
壓縮文件:
zip -r sysconfig(目標文件) /testdir/sysconfig (源文件)
unzip
[root@Centos7/app]#cat /var/log/message |zip messages.zip - (代表前面的內容)
打包文件:
[root@Centos7/app]#tar -cvf /app/sysconfig.tar /etc/sysconfig/ (將/etc/syscinfig 打包)
查看文件大小:
[root@Centos7/app]#du -sh /etc/sysconfig
tar 工具打包:
打包並壓縮:
[root@Centos7/app]#tar -zcvf /app/sysconfig.tar.gz /etc/sysconfig/
[root@Centos7/app]#tar -Jcvf /app/sysconfig.tar.xz /etc/sysconfig/
[root@Centos7/app]#tar -jcvf /app/sysconfig.tar.bz2 /etc/sysconfig/
查看壓縮文件有哪些東西:
tar -tvf /app/sysconfig.tar.xz
解壓縮文件:
[root@Centos7/app]#tar -xvf /app/sysconfig.tar.bz2 -C /app
批量查看文件類型;
[root@Centos7/app]#file -f filelist.txt
vim 讀取文件:
[root@Centos7/app]#:r! ls /root/anacond a-ks.cfg
指定打包文件;
[root@Centos7/app]#tar -T filelist.txt -Jcvf a.tar.xz
tar: Removing leading `/‘ from member names
/root/anaconda-ks.cfg
/etc/issue
sed 腳本:
經典多行編輯:
[root@Centos7~]#sed -e ‘/^#EnableMMAP\ off/s/#//‘ -e‘/^<IfModule mime_magic_module>/,/^<\/IfModule>/ s/#//‘ /etc/httpd/conf/httpd.conf
多點編輯
sed -e
[root@Centos7~]#sed -n -e ‘2p‘ -e ‘3p‘ f1
2
3
多數行打印:
[root@Centos7~]#sed -n -e ‘2p‘ -e ‘6p‘ f1
2
6
[root@Centos7~]#sed ‘n;D‘ f1 顯示奇數行;n 是覆蓋的意思
[root@Centos7~]#sed -n ‘1~2p‘ f1 顯示奇數行:
[root@Centos7~]#sed -n ‘n;p‘ f1 顯示偶數行;
[root@Centos7~]#sed -n ‘2~2p‘ f1
除了第二行全部刪掉:
[root@Centos7~]#sed ‘2!d‘ f1
2
從文件中讀取命令到。bashrc 文件文件中:
[root@Centos7~]#sed ‘/User/r /root/bin/sed.txt‘ .bashrc
腳本重定向:
cat > /root/bin/sed.txt << end
alias p=poweroff
alias cdnet="cd /etc/sysconfig/network-scripts"
end
全局添加東西;
[root@Centos7~/bin]#sed -r ‘s/(.*)/ \1&mage/g‘ /etc/passwd
替換
[root@Centos7~]#sed -r ‘s@/(bin/bash)$@/s\1@g‘ /etc/passwd
替換:
改寫網卡名字:
/etc/default/grub 影響網卡名字:
sed -r ‘s@(GRUB_CMDLINE_LINUX=".*)"$@\1 4566666xyzzzzz"@‘ /etc/default/grub
偶數行:
[root@Centos7/media/Packages]#seq 1 10 |sed -n ‘n;p‘
[root@Centos7/media/Packages]#sed -n ‘2~2‘
rpm架構:
[root@Centos7/media/Packages]#ls | egrep -o "[^.]+.rpm"|cut -d. -f1 |sort |uniq -c
2、
[root@Centos7/media/Packages]#ls |sed -r ‘s/..(.).rpm$/\1/‘ |sort |uniq -c
2141 i686
3076 noarch
1 TRANS.TBL
4374 x86_64
3、[root@Centos7/media/Packages]#ls |rev |cut -d. -f2 |rev |sort |uniq -c
2141 i686
3076 noarch
1 TRANS
4374 x86_64
4、將文本中的n和n+1行合並為一行,n 為奇數行:
cat seq 1 10 |sed "/^$/d;G" 刪除空行,然後給每行添加一個空行:
stream editor 行編輯器
sed gnu 工具
sed [option].... "script"(地址命令)
sed 語法
[root@Centos7~]#sed -n ‘p‘ /etc/issue
\S
Kernel \r on an \m
[root@Centos7~]#ifconfig |sed -n ‘2p‘ |tr -s " "|cut -d" " -f3
192.168.137.142
sed 支持標準輸入:
sed -n "2p"
[root@Centos7~]#sed -n ‘2p‘
a
b
b
c
d
顯示文件行號"
[root@Centos7~]# nl /etc/fstab
地址:
[root@Centos7~]#sed -n "8,12p" /etc/fstab
[root@Centos7~]#sed -n "8,+3p" /etc/fstab 8行之後3行打印出來
[root@Centos7~]#sed -n "3,/^f/p" /etc/passwd^C
[root@Centos7~]#sed -n ‘/^r/,/^f/p‘ /etc/passwd
奇數偶數:
[root@Centos7~]#cat -n /etc/passwd |sed -n ‘3 ~2p‘
[root@Centos7~]#sed -n ‘3~2p‘ /etc/passwd
[root@Centos7~]#sed -n ‘2~2p‘ /etc/passwd
添加空格:之後添加數字
[root@Centos7~]#cat -n /etc/passwd |sed ‘3,6a\ ============‘
在第幾行添加文本:
sed ‘7a alias cdnet="cd /etc/sysconfig/network-scripts"‘ ~/.bashrc
最後一行添加文本,並且備份:
[root@Centos7/app]# sed -i.bak ‘$a alias cdnet="cd /etc/sysconfig/network-scripts"‘ ~/.bashrc
替換:文本添加內榮:
[root@Centos7~]#sed ‘/^# Source/c\xxxxxx x‘ ~/.bashrc
修改配置文件;
[root@Centos7~]#sed -i ‘/^SELINUX=/c\SELINUX=disabled‘ /etc/selinux/config
多行添加內容:
[root@Centos7~]#sed ‘/^# Source/ixxxxxx\nyyyy\nzzzz‘ ~/.bashrc
sed :正則表達式計算
[root@Centos7~]#sed -r ‘s@(/bin/bash)@#\1@g‘ /etc/passwd
寫入到文件中:
[root@Centos7~]#sed ‘/^alias/w /app/alias.txt‘ .bashrc
顯示所在的行:
[root@Centos7~]#sed ‘/^alias/=‘.bashrc
打印用戶以bash結尾的行:
[root@Centos7~]#sed -n ‘/bash$/!p‘ /etc/passwd
全局替換;
[root@Centos7~]#sed ‘s@/bin/bash$@/sbin/nologin@g‘ /etc/passwd
替換:
[root@Centos7~]#sed -i ‘s@SELINUX=disabled@SELINUX=enforcing@‘ /etc/selinux/config
sed 截取ip 地址:
[root@Centos7~]#ifconfig ens33|sed -n ‘2p‘|sed ‘s@.inet@@g‘| sed ‘s@netmask.@@g‘ 192.168.137.142
[root@Centos7~]#ifconfig ens33|sed -n ‘2p‘|sed -r ‘s@.inet (.) netmask.@\1@‘
192.168.137.142
取ip 地址:
[root@Centos7~]#ifconfig |sed -n ‘2p‘|sed -r ‘s@(.inet )(.)( net.)@\2@‘
[root@Centos7~]#ifconfig |sed -r ‘2!d; s@(.inet )(.)( netmask .*)@\2@‘
192.168.137.142
[root@Centos7~]#ifconfig |sed -n ‘2p‘|sed -r ‘s@.inet @@‘ |sed -r ‘s@ netmask.@@‘
192.168.137.142
經典:配置文件:
[root@Centos7~]#sed -n ‘/^b/,/^f/p‘ /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
經典去除配置文件前面的#號:
ip 後向引用:
[root@Centos7~]#ifconfig ens33|sed -r -n ‘2s@.inet (.) netmask.*@\1@p‘
192.168.137.142
取版本號:
[root@Centos7~]#sed -n -r ‘s@.release ([^.]+).@\1@p‘ /etc/centos-release
7
在後面補文字:
sed -r ‘s@(quiet )@\1i love you @‘ /etc/default/grub
替換雙引號:
[root@Centos7~/bin]#sed -r ‘s/(")$/ \1/p‘ /etc/default/grub
包含:
[root@Centos7~/bin]#sed -r ‘/LINUX/s/(")$/ \1/p‘ /etc/default/grub
取出ip
[root@Centos7~/bin]#ifconfig ens33 |sed -r ‘2!d ; s/.inet //;s/ netmask.//‘
192.168.137.142
取出基名:
[root@Centos7~/bin]#echo "/etc/sysconfig/network-scripts/" |sed -nr ‘s@./(.+)/?@\1@p‘
network-scripts/
取目錄名:實驗效果
[root@Centos7~/bin]#echo "/etc/sysconfig/network-scripts/" |sed -nr ‘s@(./)(.+)/?@\1@p‘
/etc/sysconfig/
[root@Centos7~]#echo ‘/etc/sysconfig/network‘ |sed -r ‘s@(./)([^]+/?)@\1@‘
/etc/sysconfig/
[root@Centos7~]#echo ‘/etc/sysconfig/network‘ |sed -r ‘s@(./)([^]+/?)@\2@‘
network
後向引用
[root@Centos7/app]#cat mobile.txt |sed -r ‘s@([0-9]{4})([0-9]{3})([0-9]{3})@\1****\3@‘
yum :abi是指:運行在操作系統上,必須遵守操作系統標準:
api: 涉及到庫的標準; 不同的操作系統,不同的ABI
靜態庫:插入一個庫到程序中:
動態庫:調用動態庫:
ldd /bin/ls
seq 1 10 |xargs -n2
查找文件:
find -name "*.c" |wc
sed -i ‘s///g‘
cat /etc/httpd/conf/httpd.conf 很重要的配置文件
[root@Centos7/app]#sed -r ‘s/^[^#]|^$/#/g‘ /etc/fstab
解決軟件包的問題:
[root@Centos7/misc/cd/Packages]#ls *.rpm |egrep -o "[^.]+.rpm$"|cut -d. -f1 |sort|uniq -c
破壞安裝包,怎麽解決問題 :
[root@Centos7/misc/cd/Packages]#rm /usr/bin/tree
rm: remove regular file ‘/usr/bin/tree’? y
[root@Centos7/misc/cd/Packages]#rpm -i vsftpd-3.0.2-22.el7.x86_64.rpm
[root@Centos7/misc/cd/Packages]#rpm -i tree-1.6.0-10.el7.x86_64.rpm
package tree-1.6.0-10.el7.x86_64 is already installed
解決方案:覆蓋安裝
[root@Centos7/misc/cd/Packages]#rpm -ivh --replacepkgs tree-1.6.0-10.el7.x86_64.rpm
查看時間:
ll /var/lib/rpm
判斷安裝包名:
[root@Centos7/misc/cd/Packages]#rpm -q vsftpd &> /dev/null && echo true ||rpm -ivh vsftpd-3.0.2-22.el7.x86_64.rpm &> /dev/null
true
查看包來源哪個文件:
[root@Centos7/misc/cd/Packages]#rpm -qf /etc/passwd
setup-2.8.71-7.el7.noarch
:顯示包的安裝信息
rpm -qi bash
rpm -ql bash
校驗包:
rpm -import/run/media/root/centos \7\x86/RPM -GPG-KEY-CENTOS-7
rpm -k tree.....rpm
第三周 培訓重點內容: