1. 输入重定向
1.1 覆盖文件
[root@YL Desktop]# cat EOF_txt
123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123
[root@YL Desktop]# cat <<EOF >EOF_txt
> qwertyuiop
> asdfghjkl
> zxcvbnm
> EOF
[root@YL Desktop]# cat EOF_txt
qwertyuiop
asdfghjkl
zxcvbnm
1.2 文件追加内容
[root@YL Desktop]# cat <<EOF >>EOF_txt
> zhinehida
> eewrwe
> EOF
[root@YL Desktop]# cat EOF_txt
qwertyuiop
asdfghjkl
zxcvbnm
zhinehida
eewrwe
2.输出重定向
[root@YL Desktop]# cat EOF_txt
qwertyuiop
asdfghjkl
zxcvbnm
zhinehida
eewrwe
[root@YL Desktop]# echo 12345 > EOF_txt
[root@YL Desktop]# cat EOF_txt
12345
3. --stdin 获取输入
--stdin 获取键盘输入
[root@YL Desktop]# useradd stu00001
[root@YL Desktop]# echo 123456 | passwd --stdin stu00001
Changing password for user stu00001.
passwd: all authentication tokens updated successfully.
4. | 管道符
| 将前面那命令的执行结果 作为后边命令的输入
[root@YL Desktop]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.153.153 netmask 255.255.255.0 broadcast 192.168.153.255
inet6 fe80::5cb5:4e44:d8ae:7fbb prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d4:7e:24 txqueuelen 1000 (Ethernet)
RX packets 19198 bytes 3536300 (3.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4469 bytes 554745 (541.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 126 bytes 10704 (10.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 126 bytes 10704 (10.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:e8:12:1e txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@YL Desktop]# ifconfig | grep ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
[root@YL tmp]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@YL tmp]# cat /etc/passwd | grep -E "(root|stu00001)"
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
stu00001:x:1001:1001::/home/stu00001:/bin/bash
[root@YL tmp]#
[root@YL tmp]# cat /etc/rc.local |grep -v ^#
touch /var/lock/subsys/local
[root@YL tmp]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
5. 逻辑与 && ,逻辑或 ||
[root@YL tmp]# abc &&ls
bash: abc: command not found...
[root@YL tmp]# abc || ls
bash: abc: command not found...
anaconda.log ssh-FvBIowmB3IFG test02 vmware-root_647-3988163046
hsperfdata_root storage.log tracker-extract-files.0 vmware-root_655-4021587944
ifcfg.log systemd-private-054c5050cdde4cf3b69725c64edac914-bolt.service-4nmPWU tracker-extract-files.1000 vmware-root_660-2697467306
ks-script-IsrDcS systemd-private-054c5050cdde4cf3b69725c64edac914-colord.service-85kbmq vmware-root_595-4013788883 vmware-root_671-3988556280
packaging.log systemd-private-054c5050cdde4cf3b69725c64edac914-cups.service-AiBNw0 vmware-root_604-2731152132 vmware-root_677-3980363868
program.log systemd-private-054c5050cdde4cf3b69725c64edac914-fwupd.service-lkywXZ vmware-root_606-2722828934 yum.log
sensitive-info.log systemd-private-054c5050cdde4cf3b69725c64edac914-rtkit-daemon.service-4EU0M0 vmware-root_620-2697598252
6. []条件判断
判断目录或者文件是否存在
[root@YL tmp]# # 0 代表最后一条命令执行正确,反之
[root@YL tmp]# [ -d /hahaha ]
[root@YL tmp]# echo $?
1
[root@YL tmp]# # 判断根下有没有/hahaha目录
[root@YL tmp]# [ -d /mnt ]
[root@YL tmp]# echo $?
0
7. find查找
查找文件或目录
[root@YL tmp]# find / -name my.cnf
/etc/my.cnf
[root@YL tmp]# find / -name mysql.cnf
[root@YL tmp]#
[root@YL tmp]# find / -name hosts
/etc/hosts
/etc/avahi/hosts
8. 自定义指令环境变量
[root@YL tmp]# #自定义$PATH
[root@YL tmp]# cd /tmp
[root@YL tmp]# ls
anaconda.log ssh-FvBIowmB3IFG test02 vmware-root_647-3988163046
hsperfdata_root storage.log tracker-extract-files.0 vmware-root_655-4021587944
ifcfg.log systemd-private-054c5050cdde4cf3b69725c64edac914-bolt.service-4nmPWU tracker-extract-files.1000 vmware-root_660-2697467306
ks-script-IsrDcS systemd-private-054c5050cdde4cf3b69725c64edac914-colord.service-85kbmq vmware-root_595-4013788883 vmware-root_671-3988556280
packaging.log systemd-private-054c5050cdde4cf3b69725c64edac914-cups.service-AiBNw0 vmware-root_604-2731152132 vmware-root_677-3980363868
program.log systemd-private-054c5050cdde4cf3b69725c64edac914-fwupd.service-lkywXZ vmware-root_606-2722828934 yum.log
sensitive-info.log systemd-private-054c5050cdde4cf3b69725c64edac914-rtkit-daemon.service-4EU0M0 vmware-root_620-2697598252
[root@YL tmp]# vim abc.sh
[root@YL tmp]# vim abc.sh
[root@YL tmp]# cat abc.sh
#!/bin/bash
echo 123
[root@YL tmp]# #所有人均可执行该文件
[root@YL tmp]# chmod +x abc.sh
[root@YL tmp]# ll
total 28
-rwxr-xr-x. 1 root root 21 Oct 26 17:34 abc.sh
-rw-r--r--. 1 root root 2341 Oct 14 23:52 anaconda.log
drwxr-xr-x. 2 root root 18 Oct 14 22:55 hsperfdata_root
-rw-r--r--. 1 root root 415 Oct 14 23:40 ifcfg.log
-rwx------. 1 root root 836 Oct 14 23:04 ks-script-IsrDcS
-rw-r--r--. 1 root root 0 Oct 14 23:40 packaging.log
-rw-r--r--. 1 root root 44 Oct 14 23:52 program.log
-rw-r--r--. 1 root root 0 Oct 14 23:40 sensitive-info.log
drwx------. 2 root root 25 Oct 26 14:04 ssh-FvBIowmB3IFG
-rw-r--r--. 1 root root 0 Oct 14 23:40 storage.log
drwx------. 3 root root 17 Oct 24 16:22 systemd-private-054c5050cdde4cf3b69725c64edac914-bolt.service-4nmPWU
drwx------. 3 root root 17 Oct 24 16:22 systemd-private-054c5050cdde4cf3b69725c64edac914-colord.service-85kbmq
drwx------. 3 root root 17 Oct 24 16:22 systemd-private-054c5050cdde4cf3b69725c64edac914-cups.service-AiBNw0
drwx------. 3 root root 17 Oct 26 14:04 systemd-private-054c5050cdde4cf3b69725c64edac914-fwupd.service-lkywXZ
drwx------. 3 root root 17 Oct 24 16:22 systemd-private-054c5050cdde4cf3b69725c64edac914-rtkit-daemon.service-4EU0M0
-rw-r--r--. 1 root root 4937 Oct 26 16:31 test02
drwx------. 2 root root 6 Oct 26 15:49 tracker-extract-files.0
drwx------. 2 yl yl 6 Oct 15 00:08 tracker-extract-files.1000
drwx------. 2 root root 6 Oct 20 22:18 vmware-root_595-4013788883
drwx------. 2 root root 6 Oct 17 18:03 vmware-root_604-2731152132
drwx------. 2 root root 6 Oct 20 22:41 vmware-root_606-2722828934
drwx------. 2 root root 6 Oct 24 15:07 vmware-root_620-2697598252
drwx------. 2 root root 6 Oct 14 23:59 vmware-root_647-3988163046
drwx------. 2 root root 6 Oct 24 16:22 vmware-root_655-4021587944
drwx------. 2 root root 6 Oct 24 13:34 vmware-root_660-2697467306
drwx------. 2 root root 6 Oct 24 16:05 vmware-root_671-3988556280
drwx------. 2 root root 6 Oct 14 23:40 vmware-root_677-3980363868
-rw-------. 1 root root 0 Oct 14 22:50 yum.log
[root@YL tmp]# abc.sh
bash: abc.sh: command not found...
[root@YL tmp]# sh abc.sh
123
[root@YL tmp]# #将指令文件定义到环境变量中
[root@YL tmp]# PATH=$PATH:/tmp
[root@YL tmp]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp
[root@YL tmp]# abc.sh
123
[root@YL tmp]# cd ..
[root@YL /]# abc.sh
123
9. if 条件语句
[root@YL tmp]# vim a1.sh
[root@YL tmp]# cat a1.sh
#!/bin/bash
NAME=abc
if [ $NAME == 'abc' ]
then
echo $NAME
fi
[root@YL tmp]# sh a1.sh
abc
[root@YL tmp]# vim a1.sh
[root@YL tmp]# cat a1.sh
#!/bin/bash
NAME=abc
if [ $NAME == 'abcd' ]
then
echo $NAME
fi
[root@YL tmp]# sh a1.sh
[root@YL tmp]#
[root@YL tmp]# vim a2.sh
[root@YL tmp]# cat a2.sh
#!/bin/bash
NAME=abc
if [ $NAME == 'abcd' ];then
echo $NAME
elif [ $NAME == 'ab' ];then
echo "my name is $NAME"
elif [ $NAME == 'abcd' ];then
echo "my name is $NAME"
else
echo "error"
fi
[root@YL tmp]# sh a2.sh
error
10. for循环
(1) base
[root@YL ~]# cd /tmp
[root@YL tmp]# vim a6.sh
[root@YL tmp]# cat a6.sh
for i in 1 2 3
do
echo $i
done
[root@YL tmp]# sh a6.sh
1
2
3
(2) 批量复制文件
[root@YL tmp]# vim a7.sh
[root@YL tmp]# cat a7.sh
#!/bin/bash
for dir in /etc /tmp /mnt
do
touch $dir/hehe.txt
ls -l $dir/hehe.txt
done
[root@YL tmp]# sh a7.sh
-rw-r--r--. 1 root root 0 Oct 27 11:21 /etc/hehe.txt
-rw-r--r--. 1 root root 0 Oct 27 11:21 /tmp/hehe.txt
-rw-r--r--. 1 root root 0 Oct 27 11:21 /mnt/hehe.txt
(3) 批量重命名文件夹中的所有文件
[root@YL aaa]# vim a8.sh
[root@YL aaa]# cat a8.sh
#!/bin/bash
echo "Please input directory name:"
read path
echo "Please input prefix:"
read prefix
for file in $(ls $path/)
do
if [ -f "$path/$file" ]
then
mv "$path/$file" "$path/$prefix$file"
echo "rename $path/$file is $path/$prefix$file"
fi
done
echo "Rename completed!"
[root@YL aaa]# pwd
/tmp/aaa
[root@YL aaa]# sh a8.sh
Please input directory name:
/tmp/aaa
Please input prefix:
phdvb-
rename /tmp/aaa/a8.sh is /tmp/aaa/phdvb-a8.sh
rename /tmp/aaa/aA.txt is /tmp/aaa/phdvb-aA.txt
rename /tmp/aaa/aB.txt is /tmp/aaa/phdvb-aB.txt
rename /tmp/aaa/aC.txt is /tmp/aaa/phdvb-aC.txt
rename /tmp/aaa/bA.txt is /tmp/aaa/phdvb-bA.txt
rename /tmp/aaa/bB.txt is /tmp/aaa/phdvb-bB.txt
rename /tmp/aaa/bC.txt is /tmp/aaa/phdvb-bC.txt
rename /tmp/aaa/cA.txt is /tmp/aaa/phdvb-cA.txt
rename /tmp/aaa/cB.txt is /tmp/aaa/phdvb-cB.txt
rename /tmp/aaa/cC.txt is /tmp/aaa/phdvb-cC.txt
Rename completed!
[root@YL aaa]# ll
total 4
-rw-r--r--. 1 root root 284 Oct 27 11:46 phdvb-a8.sh
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-aA.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-aB.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-aC.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-bA.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-bB.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-bC.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-cA.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-cB.txt
-rw-r--r--. 1 root root 0 Oct 27 11:32 phdvb-cC.txt
(4) print multi 表
[root@YL tmp]# vim a9.sh
[root@YL tmp]# cat a9.sh
#!/bin/bash
for i in {1..10}
do
for j in {1..10}
do
if [ $j -le $i ]
then
echo -n "$j*$i=$((j*i))"
fi
done
echo ""
done
[root@YL tmp]# sh a9.sh
1*1=1
1*2=22*2=4
1*3=32*3=63*3=9
1*4=42*4=83*4=124*4=16
1*5=52*5=103*5=154*5=205*5=25
1*6=62*6=123*6=184*6=245*6=306*6=36
1*7=72*7=143*7=214*7=285*7=356*7=427*7=49
1*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=64
1*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81
1*10=102*10=203*10=304*10=405*10=506*10=607*10=708*10=809*10=9010*10=100
11. While 循环
1. 猜数字
[root@YL tmp]# sh w1.sh
sh: w1.sh: No such file or directory
[root@YL tmp]# vim w1.sh
[root@YL tmp]# cat w1.sh
#!/bin/bash
echo "Welcome!"
echo "Please input num between 1-100:"
num=$((RANDOM%100+1))
guess=0
count=0
while [ $guess -ne $num ]
do
read guess
if [ $guess -lt $num ]
then
echo "small...again"
elif [ $guess -gt $num ]
then
echo "bigger...again"
else
echo "Congratulations!"
fi
count=$((count+1))
done
echo "Guess Time:$count"
[root@YL tmp]# sh w1.sh
Welcome!
Please input num between 1-100:
80
bigger...again
60
bigger...again
50
bigger...again
40
small...again
45
small...again
48
Congratulations!
Guess Time:6
12. AWK 文本处理
1.提取主机 ip 信息
[root@YL tmp]# echo "my name is phdvb" |awk '{print $3}'
is
[root@YL tmp]# echo "my name is phdvb" |awk '{print $4}'
phdvb
[root@YL tmp]# echo "my name is phdvb" |awk '{print $1}'
my
[root@YL tmp]# echo "my name is phdvb" |awk '{print $2}'
name
[root@YL tmp]# ifconfig ens33 | grep inet | head -1
inet 192.168.153.155 netmask 255.255.255.0 broadcast 192.168.153.255
[root@YL tmp]# ifconfig ens33 | grep inet | head -1 | awk '{print $2}'
192.168.153.155
[root@YL tmp]# echo "my name is henry" | awk '{print $1, $3}'
my is
[root@YL tmp]# echo "my name is henry" | awk '{print $1"-"$3}'
my-is
[root@YL tmp]# echo "my name is henry" | awk '{print $0}'
my name is henry
[root@YL tmp]# echo "my name is henry" | awk '{print $1}'
my
[root@YL tmp]# echo "my name is henry" | awk /henry/'{print $1}'
my
32万+

被折叠的 条评论
为什么被折叠?



