shell入门16-八大扩展功能

2023-01-01  本文已影响0人  万州客

温故知新

一,八大扩展功能

二,花括号扩展

image.png

练习

[root@127 ~]# echo {r,f,j}
r f j
[root@127 ~]# echo {hello,sky}
hello sky
[root@127 ~]# echo {a..}
{a..}
[root@127 ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@127 ~]# echo {a..z..2}
a c e g i k m o q s u w y
[root@127 ~]# echo {1..9..2}
1 3 5 7 9
[root@127 ~]# echo t{i,o}p
tip top
[root@127 ~]# echo t{o,e{a,m}}p
top teap temp
[root@127 ~]# mkdir -p /test/t{o,e{a,m}}p
[root@127 ~]# ls /test/
teap  temp  top
[root@127 ~]# touch /tmp/t{e{a,m},o}p/{a,b,c,d,e}t.txt
touch: cannot touch ‘/tmp/teap/at.txt’: No such file or directory
touch: cannot touch ‘/tmp/teap/bt.txt’: No such file or directory
touch: cannot touch ‘/tmp/teap/ct.txt’: No such file or directory
touch: cannot touch ‘/tmp/teap/dt.txt’: No such file or directory
touch: cannot touch ‘/tmp/teap/et.txt’: No such file or directory
touch: cannot touch ‘/tmp/temp/at.txt’: No such file or directory
touch: cannot touch ‘/tmp/temp/bt.txt’: No such file or directory
touch: cannot touch ‘/tmp/temp/ct.txt’: No such file or directory
touch: cannot touch ‘/tmp/temp/dt.txt’: No such file or directory
touch: cannot touch ‘/tmp/temp/et.txt’: No such file or directory
touch: cannot touch ‘/tmp/top/at.txt’: No such file or directory
touch: cannot touch ‘/tmp/top/bt.txt’: No such file or directory
touch: cannot touch ‘/tmp/top/ct.txt’: No such file or directory
touch: cannot touch ‘/tmp/top/dt.txt’: No such file or directory
touch: cannot touch ‘/tmp/top/et.txt’: No such file or directory
[root@127 ~]# touch /test/t{e{a,m},o}p/{a,b,c,d,e}t.txt
[root@127 ~]# touch /test/t,{e{a,m},o}p/{a,b,c,d,e}t.txt
touch: cannot touch ‘/test/t,eap/at.txt’: No such file or directory
touch: cannot touch ‘/test/t,eap/bt.txt’: No such file or directory
touch: cannot touch ‘/test/t,eap/ct.txt’: No such file or directory
touch: cannot touch ‘/test/t,eap/dt.txt’: No such file or directory
touch: cannot touch ‘/test/t,eap/et.txt’: No such file or directory
touch: cannot touch ‘/test/t,emp/at.txt’: No such file or directory
touch: cannot touch ‘/test/t,emp/bt.txt’: No such file or directory
touch: cannot touch ‘/test/t,emp/ct.txt’: No such file or directory
touch: cannot touch ‘/test/t,emp/dt.txt’: No such file or directory
touch: cannot touch ‘/test/t,emp/et.txt’: No such file or directory
touch: cannot touch ‘/test/t,op/at.txt’: No such file or directory
touch: cannot touch ‘/test/t,op/bt.txt’: No such file or directory
touch: cannot touch ‘/test/t,op/ct.txt’: No such file or directory
touch: cannot touch ‘/test/t,op/dt.txt’: No such file or directory
touch: cannot touch ‘/test/t,op/et.txt’: No such file or directory
[root@127 ~]# tree /test/
-bash: tree: command not found
[root@127 ~]# yum install tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                               | 3.6 kB  00:00:00     
docker-ce-stable                                                                                   | 3.5 kB  00:00:00     
epel                                                                                               | 4.7 kB  00:00:00     
extras                                                                                             | 2.9 kB  00:00:00     
mysql-connectors-community                                                                         | 2.6 kB  00:00:00     
mysql-tools-community                                                                              | 2.6 kB  00:00:00     
mysql56-community                                                                                  | 2.6 kB  00:00:00     
updates                                                                                            | 2.9 kB  00:00:00     
(1/2): epel/x86_64/updateinfo                                                                      | 1.0 MB  00:00:24     
(2/2): epel/x86_64/primary_db                                                                      | 7.0 MB  00:00:36     
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================
 Package                   Arch                        Version                            Repository                 Size
==========================================================================================================================
Installing:
 tree                      x86_64                      1.6.0-10.el7                       base                       46 k

Transaction Summary
==========================================================================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Is this ok [y/d/N]: y
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                                                                                                       |  46 kB  00:00:22     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tree-1.6.0-10.el7.x86_64                                                                                                               1/1 
  Verifying  : tree-1.6.0-10.el7.x86_64                                                                                                               1/1 

Installed:
  tree.x86_64 0:1.6.0-10.el7                                                                                                                              

Complete!
[root@127 ~]# tree /test/
/test/
├── teap
│   ├── at.txt
│   ├── bt.txt
│   ├── ct.txt
│   ├── dt.txt
│   └── et.txt
├── temp
│   ├── at.txt
│   ├── bt.txt
│   ├── ct.txt
│   ├── dt.txt
│   └── et.txt
└── top
    ├── at.txt
    ├── bt.txt
    ├── ct.txt
    ├── dt.txt
    └── et.txt

3 directories, 15 files

三,波浪号扩展

image.png

练习

[root@127 ~]# echo ~
/root
[root@127 ~]# echo ~/test
/root/test
[root@127 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chengang:x:1000:1000:chengang:/home/chengang:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
redis:x:998:994:Redis Database Server:/var/lib/redis:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
telegraf:x:996:993::/etc/telegraf:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
sky:x:1001:1001::/home/sky:/bin/bash
nginx:x:1002:1002::/home/nginx:/sbin/nologin
[root@127 ~]# echo ~sky
/home/sky
[root@127 ~]# cd /root/
[root@127 ~]# cd /tmp/
[root@127 tmp]# echo ~+
/tmp
[root@127 tmp]# echo ~-
/root

四,变量替换

image.png

练习

[root@127 tmp]# hi="Go Fight Go"
[root@127 tmp]# echo $hi
Go Fight Go
[root@127 tmp]# echo ${hi}
Go Fight Go
[root@127 tmp]# player="DUNCAN"
[root@127 tmp]# mvp=player
[root@127 tmp]# echo ${mvp}
player
[root@127 tmp]# echo ${!mvp}
DUNCAN
[root@127 tmp]# other=mvp
[root@127 tmp]# echo ${!other}
player
[root@127 tmp]# echo $animals

[root@127 tmp]# echo ${animals:-dog}
dog
[root@127 tmp]# echo $animals

[root@127 tmp]# animals=""
[root@127 tmp]# echo ${animals:-dog}
dog
[root@127 tmp]# echo ${animals:=lion}
lion
[root@127 tmp]# echo ${animals}
lion
[root@127 tmp]# color=""
[root@127 tmp]# echo ${color:=blue}
blue
[root@127 tmp]# echo $color
blue
[root@127 tmp]# content="road is long, i will be explore."
[root@127 tmp]# echo ${content:-OK}
road is long, i will be explore.
[root@127 tmp]# echo ${content:=OK}
road is long, i will be explore.
[root@127 tmp]# echo $content
road is long, i will be explore.
[root@127 tmp]# echo ${input:?"你没有输入变量的值!"}
-bash: input: 你没有输入变量的值!
[root@127 tmp]# echo ${key:+lock}

[root@127 tmp]# key=heart
[root@127 tmp]# echo ${key:+lock}
lock
image.png
image.png

练习

[root@127 tmp]# home="The oak tree, It was covered with yello handkerchiefs."
[root@127 tmp]# echo ${home:2}
e oak tree, It was covered with yello handkerchiefs.
[root@127 tmp]# echo ${home:14}
It was covered with yello handkerchiefs.
[root@127 tmp]# echo ${home:14:6}
It was
[root@127 tmp]# echo ${home#Th}
e oak tree, It was covered with yello handkerchiefs.
[root@127 tmp]# echo ${home#The}
oak tree, It was covered with yello handkerchiefs.
[root@127 tmp]# echo ${home#oak}
The oak tree, It was covered with yello handkerchiefs.
[root@127 tmp]# echo ${home#*y}
ello handkerchiefs.
[root@127 tmp]# echo ${home#*o}
ak tree, It was covered with yello handkerchiefs.
[root@127 tmp]# echo ${home##*o}
handkerchiefs.
[root@127 tmp]# echo ${home%efs.}
The oak tree, It was covered with yello handkerchi
[root@127 tmp]# echo ${home%d*}
The oak tree, It was covered with yello han
[root@127 tmp]# echo ${home%%d*}
The oak tree, It was covere
[root@127 tmp]# tools=(car helicoper airplane train)
[root@127 tmp]# echo ${tools[0]}
car
[root@127 tmp]# echo ${tools[0]:0:2}
ca
[root@127 tmp]# echo ${tools[1]:0:2}
he
[root@127 tmp]# echo ${tools[1]:2:2}
li
[root@127 tmp]# echo ${tools[1]#*e}
licoper
[root@127 tmp]# echo ${tools[1]##*e}
r
[root@127 tmp]# echo ${tools[1]%e*}
helicop
[root@127 tmp]# echo ${tools[1]%%e*}
h
image.png

练习

[root@127 tmp]# echo ${!U*}
UID USER
[root@127 tmp]# echo ${!U@}
UID USER
[root@127 tmp]# echo ${!HO@}
HOME HOSTNAME HOSTTYPE
[root@127 tmp]# test1=(11 22 33 44)
[root@127 tmp]# test2=(77 88 99 00)
[root@127 tmp]# echo ${!test1[*]}
0 1 2 3
[root@127 tmp]# echo ${!test1[@]}
0 1 2 3
[root@127 tmp]# declare -A str
[root@127 tmp]# str[a]=aaa
[root@127 tmp]# str[b]=bbb
[root@127 tmp]# str[word]="key value"
[root@127 tmp]# echo ${str[*]}
key value aaa bbb
[root@127 tmp]# echo ${!str[*]}
word a b
[root@127 tmp]# echo ${!str[@]}
word a b
[root@127 tmp]# play="Go Fight Go"
[root@127 tmp]# echo ${#play}
11
[root@127 tmp]# hi=(hello the world)
[root@127 tmp]# echo ${#hi}
5
[root@127 tmp]# echo ${#hi[0]}
5
[root@127 tmp]# echo ${#hi[1]}
3
[root@127 tmp]# echo ${#hi[2]}
5
[root@127 tmp]# phone=18811011011
[root@127 tmp]# echo ${phone/1/x}
x8811011011
[root@127 tmp]# echo ${phone//1/x}
x88xx0xx0xx
[root@127 tmp]# echo ${phone/110/x}
188x11011
[root@127 tmp]# echo ${phone//110/x}
188xx11
[root@127 tmp]# echo $phone
18811011011
[root@127 tmp]# lowers="hello the world"
[root@127 tmp]# echo ${lowers^}
Hello the world
[root@127 tmp]# echo ${lowers^^}
HELLO THE WORLD
[root@127 tmp]# echo ${lowers}
hello the world
[root@127 tmp]# echo ${lowers^h}
Hello the world
[root@127 tmp]# echo ${lowers^^h}
Hello tHe world
[root@127 tmp]# [heo}]
-bash: [heo}]: command not found
[root@127 tmp]# echo ${lowers^^[heo]}
HEllO tHE wOrld
[root@127 tmp]# uppers="HEELO THE WORLD"
[root@127 tmp]# echo ${uppers,}
hEELO THE WORLD
[root@127 tmp]# echo ${uppers,,}
heelo the world
[root@127 tmp]# echo ${uppers,H}
hEELO THE WORLD
[root@127 tmp]# echo ${uppers,,H}
hEELO ThE WORLD
[root@127 tmp]# echo "${uppers,,[HOL]}"
hEElo ThE WoRlD

五,命令替换

image.png

练习

[root@127 tmp]# echo -e "system CPU load:\n$(date +%Y-%m-%d;uptime)"
system CPU load:
2023-01-01
 11:06:52 up  1:21,  2 users,  load average: 0.00, 0.01, 0.04
[root@127 tmp]# echo -e "system CPUload:\n`date +%Y-%m-%d;uptime`"
system CPUload:
2023-01-01
 11:07:40 up  1:22,  2 users,  load average: 0.00, 0.01, 0.04
[root@127 tmp]# echo "系统当前登陆人数:$(who|wc -l"
> )
> 
> ^C
[root@127 tmp]# echo "系统当前登陆人数:$(who|wc -l)"
系统当前登陆人数:2
[root@127 tmp]# du -sh $(pwd)
4.0K    /tmp

六,算术替换

image.png

练习

[root@127 tmp]# i=1
[root@127 tmp]# echo $((i++))
1
[root@127 tmp]# echo $((i++))
2
[root@127 tmp]# echo $((i++))
3
[root@127 tmp]# i=1
[root@127 tmp]# echo ${{++i))
> }
-bash: ${{++i))
}: bad substitution
[root@127 tmp]# echo $((++i))
2
[root@127 tmp]# echo $((++i))
3
[root@127 tmp]# echo $((++i))
4
[root@127 tmp]# echo $((--i))
3
[root@127 tmp]# echo $((--i))
2
[root@127 tmp]# echo $((1+2))
3
[root@127 tmp]# echo $((4+2))
6
[root@127 tmp]# echo $((2*3))
6
[root@127 tmp]# echo $((20/5))
4
[root@127 tmp]# echo $((2**3))
8
[root@127 tmp]# echo $((2>=3))
0
[root@127 tmp]# echo $((8>=3))
1
[root@127 tmp]# echo $((3>=3))
1
[root@127 tmp]# echo $((3>3))
0
[root@127 tmp]# echo $((3<=3))
1
[root@127 tmp]# echo $((3==3))
1
[root@127 tmp]# echo $((3==4))
0
[root@127 tmp]# echo $((3!==4))
-bash: 3!==4: syntax error: operand expected (error token is "=4")
[root@127 tmp]# echo $((3!=4))
1
[root@127 tmp]# echo $[3!=4]
1
[root@127 tmp]# echo $[3<=3]
1
[root@127 tmp]# echo $[2*3]
6

七,进程替换

image.png

练习

[root@127 tmp]# who |wc -l
2
[root@127 tmp]# wc -l <(who)
2 /dev/fd/63
[root@127 tmp]# ls /dev/fd/63
ls: cannot access /dev/fd/63: No such file or directory
[root@127 tmp]# paste <(cut -d: -f1,6 /etc/passwd) <(cut -d: -f2 /etc/shadow)
root:/root      $6$gBr7BUAsEvyClDlv$Z0nP6VLv9ZPstmuvMbCa0w0WVADhd3DR.EFqfN1ZwWkxcchb/WQRoyQFfXe0nWWaYgkg9Chq/k2wFrZmcXf2V.
bin:/bin        *
daemon:/sbin    *
adm:/var/adm    *
lp:/var/spool/lpd       *
sync:/sbin      *
shutdown:/sbin  *
halt:/sbin      *
mail:/var/spool/mail    *
operator:/root  *
games:/usr/games        *
ftp:/var/ftp    *
nobody:/        *
systemd-network:/       !!
dbus:/  !!
polkitd:/       !!
sshd:/var/empty/sshd    !!
postfix:/var/spool/postfix      !!
chengang:/home/chengang $6$v/0L3WyR3kUSclLi$PRoG1b.7ttINyjEVY3eTLaKmwAqQvpURvnsQA.U3XVNX9Hc8FFCrKsZWRWByEFLGBu2WHnXkgD0W9O40PBxxB/
tss:/dev/null   !!
redis:/var/lib/redis    !!
mysql:/var/lib/mysql    !!
saslauth:/run/saslauthd !!
telegraf:/etc/telegraf  !!
apache:/usr/share/httpd !!
ntp:/etc/ntp    !!
sky:/home/sky   $6$zEU2bARu$lyEN9.DLJ2vMghRcFy3pnmBHQECjpTs.CgIEtgE6PWELjB876jZQJA2NcKWi/7moOVqjlNPgltUCmtAQOYUa11
nginx:/home/nginx       !!
[root@127 tmp]# ls /etc/*.conf
/etc/asound.conf  /etc/ld.so.conf      /etc/mke2fs.conf          /etc/rsyncd.conf     /etc/tcsd.conf
/etc/dracut.conf  /etc/libaudit.conf   /etc/nsswitch.conf        /etc/rsyslog.conf    /etc/vconsole.conf
/etc/e2fsck.conf  /etc/libuser.conf    /etc/ntp.conf             /etc/sestatus.conf   /etc/yum.conf
/etc/host.conf    /etc/locale.conf     /etc/redis.conf           /etc/sudo.conf
/etc/kdump.conf   /etc/logrotate.conf  /etc/redis-sentinel.conf  /etc/sudo-ldap.conf
/etc/krb5.conf    /etc/man_db.conf     /etc/resolv.conf          /etc/sysctl.conf
[root@127 tmp]# ls /etc/*.conf > /tmp/conf.log
[root@127 tmp]# ls /etc/*.conf|tee /tmp/conf.log 
/etc/asound.conf
/etc/dracut.conf
/etc/e2fsck.conf
/etc/host.conf
/etc/kdump.conf
/etc/krb5.conf
/etc/ld.so.conf
/etc/libaudit.conf
/etc/libuser.conf
/etc/locale.conf
/etc/logrotate.conf
/etc/man_db.conf
/etc/mke2fs.conf
/etc/nsswitch.conf
/etc/ntp.conf
/etc/redis.conf
/etc/redis-sentinel.conf
/etc/resolv.conf
/etc/rsyncd.conf
/etc/rsyslog.conf
/etc/sestatus.conf
/etc/sudo.conf
/etc/sudo-ldap.conf
/etc/sysctl.conf
/etc/tcsd.conf
/etc/vconsole.conf
/etc/yum.conf
[root@127 tmp]# touch {a,b,c}.sh
[root@127 tmp]# touch {11,22,33}.conf
[root@127 tmp]# ls|tee >(grep sh$ > sh.log) >(grep conf$ > conf.log)
11.conf
22.conf
33.conf
a.sh
b.sh
conf.log
c.sh
passwd
sh.log
systemd-private-9bf0aba4db6e44739d407b2b8edb5275-systemd-hostnamed.service-BNoQhZ
[root@127 tmp]# cat sh.log 
a.sh
b.sh
c.sh
[root@127 tmp]# cat conf.log 
11.conf
22.conf
33.conf

八,单词切割

image.png

练习

[root@127 tmp]# read -p "请输入3个字符:" x y z
请输入3个字符:1 2 3
[root@127 tmp]# echo $
$
[root@127 tmp]# echo $x
1
[root@127 tmp]# echo $y
2
[root@127 tmp]# echo $z
3
[root@127 tmp]# read -p "请输入3个字符:" x y z
请输入3个字符:123
[root@127 tmp]# echo $x
123
[root@127 tmp]# echo $y

[root@127 tmp]# echo $z

[root@127 tmp]# read -p "请输入3个字符:" x y z
请输入3个字符:1,2,3
[root@127 tmp]# echo $x
1,2,3
[root@127 tmp]# echo $y

[root@127 tmp]# echo $z

[root@127 tmp]# IFS=$',' read -p "请输入3个字符:" x y z
请输入3个字符:1,2,3
[root@127 tmp]# echo $x
1
[root@127 tmp]# echo $y
2
[root@127 tmp]# echo $z
3

九,路径替换

image.png

练习

[root@127 tmp]# touch {a,A,b,B}.txt
[root@127 tmp]# ls b*
b.sh  b.txt
[root@127 tmp]# ls B*
B.txt
[root@127 tmp]# shopt -s nocaseglog
-bash: shopt: nocaseglog: invalid shell option name
[root@127 tmp]# sho
shopt             show-changed-rco  showconsolefont   show-installed    showkey           
[root@127 tmp]# shopt nocaseglog
-bash: shopt: nocaseglog: invalid shell option name
[root@127 tmp]# ls B*
B.txt
[root@127 tmp]# shopt -s nocaseglob
[root@127 tmp]# shopt nocaseglob
nocaseglob      on
[root@127 tmp]# ls B*
b.sh  b.txt  B.txt
[root@127 tmp]# ls b*
b.sh  b.txt  B.txt
[root@127 tmp]# shopt -u nocaseglob
[root@127 tmp]# shopt nocaseglob
nocaseglob      off
[root@127 tmp]# shopt -s extglob
[root@127 tmp]# ls !(a.txt)
11.conf  22.conf  33.conf  a.sh  A.txt  b.sh  b.txt  B.txt  conf.log  c.sh  passwd  sh.log

systemd-private-9bf0aba4db6e44739d407b2b8edb5275-systemd-hostnamed.service-QVPD7X:
tmp
[root@127 tmp]# ls !(a.txt|b.txt)
11.conf  22.conf  33.conf  a.sh  A.txt  b.sh  B.txt  conf.log  c.sh  passwd  sh.log

systemd-private-9bf0aba4db6e44739d407b2b8edb5275-systemd-hostnamed.service-QVPD7X:
tmp
[root@127 tmp]# touch a{1,2,3,4}.txt
[root@127 tmp]# rm -rf a[1-4].txt
[root@127 tmp]# basename /a/b/c/d.txt
d.txt
[root@127 tmp]# dirname /a/b/c/d.txt
/a/b/c
[root@127 tmp]# basename /etc/hosts
hosts
[root@127 tmp]# dirname /etc/hosts
/etc
[root@127 tmp]# vim filebak.sh
[root@127 tmp]# cat filebak.sh 
#!/bin/bash
# 功能描述:循环对多个文件进行备份操作

for i in `ls /etc/*.conf`; do
  tar -cxvf /root/log/$(basename $i).tar.gz $i
done
上一篇下一篇

猜你喜欢

热点阅读