01
Linux特殊符号运用详解
1
|: 管道符,前一个命令的标准输出作为后一个命令的标准输入,每个管 道后面接受的第一个数据必是命令 或者用于匹配一组可选的字符:case语句,正则表达式
2
||:逻辑或or;前一个指令执行失败则执行后一个指令。
3
>或1>:输出重定向并覆盖原有数据,将显示到终端显示器的所有输出重定向至文 件;或是清空文件内容>>:输出追加重定向2>:重定向错误 ++++++++++++++++++++++++++++++
[root@yanjiaxi ~]# ls -alh > test2 [root@yanjiaxi ~]# cat test2 total 164K -rw------- 1 root root 32K Jul 13 13:39 .mysql_history -rw------- 1 root root 104 Jun 30 00:11 .mysql_secret drwxr-xr-x. 3 root root 50 May 24 14:33 python3 -rw-------. 1 root root 7 May 24 15:04 .python_history -rw-------. 1 root root 7 May 24 19:30 .rediscli_history drwxr-xr-x 2 root root 59 Jul 12 18:39 shell
[root@yanjiaxi ~]# who >> test2 [root@yanjiaxi ~]# cat test2 total 164K -rw------- 1 root root 32K Jul 13 13:39 .mysql_history -rw------- 1 root root 104 Jun 30 00:11 .mysql_secret drwxr-xr-x. 3 root root 50 May 24 14:33 python3 -rw-------. 1 root root 7 May 24 15:04 .python_history -rw-------. 1 root root 7 May 24 19:30 .rediscli_history drwxr-xr-x 2 root root 59 Jul 12 18:39 shell
yanjiaxi pts/0 2019-07-13 09:05 (192.168.142.1)
清空文件内容:
[root@yanjiaxi ~]# >test1
[root@yanjiaxi ~]# cat test1
错误输出重定向:
[root@yanjiaxi ~]# ls nofile.txt 2>test3
[root@yanjiaxi ~]# cat test3
ls: cannot access nofile.txt: No such file or directory
++++++++++++++++++++++++++++++
>|:强制重定向,强制覆盖一个现存的文件
<或0<:输入重定向,linux会重定向指定的文件来替换标准输入文件描 述符
<<:追加输入重定向
[root@yanjiaxi ~]# cat > test1 <<eof
> my name is yanjiaxi
> welcome to my linux world
> eof
[root@yanjiaxi ~]# cat < test1
#cat命令会用test1的内容作为标准输入
my name is yanjiaxi
welcome to my linux world
++++++++++++++++++++++++++++++++ 4
/:路径分隔符 \:转义字符 #:配置或普通文件的注释 ~:当前用户的家目录 -:上一次目录 cd -:直接跳回上一次使用目录 .:当前目录的硬链接或当前目录 ..:上级目录的硬链接或上级目录 cd ..:上一级目录 ;:连续不同命令的分隔符 +++++++++++++++++++++++++++[root@yanjiaxi ~]# who;pwdyanjiaxi pts/0 2019-07-13 09:05 (192.168.142.1) /root 5
‘’:单引号,不具有变量置换功能,输出时所见即所得 “”:具有变量置换功能,解析变量后输出。不加引号相当于双引号
+++++++++++++++++++++++++++++++[root@yanjiaxi ~]# s=123 [root@yanjiaxi ~]# echo '$s' $s [root@yanjiaxi ~]# echo "$s" 123
[root@yanjiaxi ~]# y=yanjiaxi [root@yanjiaxi ~]# awk 'BEGIN {print $y}'
[root@yanjiaxi ~]# awk 'BEGIN {print '$y'}'
[root@yanjiaxi ~]# awk 'BEGIN {print "$y"}' $y [root@yanjiaxi ~]# awk 'BEGIN {print "'$y'"}' yanjiaxi++++++++++++++++++++++++++++++++ 6
``或$():引用命令被执行的结果,变量解析 +++++++++++++++++++++++++++ [root@yanjiaxi ~]# s=`ls` [root@yanjiaxi ~]# echo $s access_log.sql anaconda-ks.cfg apps.sql demo1.sh demo2.sh [root@yanjiaxi ~]# m=$(ls) [root@yanjiaxi ~]# echo $m access_log.sql anaconda-ks.cfg apps.sql demo1.sh demo2.sh [root@yanjiaxi ~]# echo ${m} access_log.sql anaconda-ks.cfg apps.sql demo1.sh demo2.sh +++++++++++++++++++++++++++ 7
$:结尾符,变量前需加的符号(正则) $0:当前脚本的文件名 $n:脚本的n个传入参数,$1,......,$9,${10}.. $#:脚本后接参数的总个数 $*:脚本所有传参参数,加上双引号时将视为单个字符串 $@:脚本所有传参参数,加上双引号时,视为不同的独立字串 $?:退出状态变量,保存一个函数、命令、脚本本身的退出状态 $$:进程ID,保存正在运行的进程的ID ++++++++++++++++++++++++++++++ [root@yanjiaxi ~]# cat test4.sh #!/bin/bash echo $0 echo $1 echo $2 echo $3 echo $* echo $@ echo $# [root@yanjiaxi ~]# ./test4.sh my name is yanjiaxi ./test4.sh my name is my name is yanjiaxi my name is yanjiaxi 4
[root@yanjiaxi ~]# cat test5.sh #!/bin/bash set -- "my name" is yanjiaxi hello linux for i in "$*" do echo $i done [root@yanjiaxi ~]# ./test5.sh my name is yanjiaxi hello linux [root@yanjiaxi ~]# ./test5.sh my name is yanjiaxi hello linux ++++++++++++++++++++++++++++++
8
^:开头符(正则) *:通配符,代表所有(0个或多个字符)或数学乘法号 +++++++++++++++++++++++++ [root@yanjiaxi test]# ls ./*.jpg ./stu_102999_1_.jpg ./stu_102999_3_.jpg ./stu_102999_5_.jpg ./stu_102999_2_.jpg ./stu_102999_4_.jpg +++++++++++++++++++++++++
**:数学求幂 ?:通配符,代表任意一个字符 [root@yanjiaxi ~]# ls access_log.sql initial-setup-ks.cfg shell test3 anaconda-ks.cfg menu.sh sifangcaixt_scripts test4.sh apps.sql pid.sh test websites.sql demo1.sh ping.sh test1 demo2.sh python3 test2 [root@yanjiaxi ~]# ls ???.sh pid.sh
&:让程序或脚本切换到后台进行 ./test4.sh & &&:当前一个指令执行成功时,执行后一个指令 !:逻辑运算中的非 []:条件测试,左右需有一空格或使用字符匹配一个范围,如正则[a- zA-Z0-9] [[]]:条件测试语法&&、||、>、<等操作符可以应用于[[]]中但 不能用于[]中
9
{}:产生一个序列(可批量创建一个文件通配符)或封装一个函数的代码 块 [root@yanjiaxi ~]# cd yjx/ [root@yanjiaxi yjx]# touch {a..z}.sh [root@yanjiaxi yjx]# ls a.sh c.sh e.sh g.sh i.sh k.sh m.sh o.sh q.sh s.sh u.sh w.sh y.sh b.sh d.sh f.sh h.sh j.sh l.sh n.sh p.sh r.sh t.sh v.sh x.sh z.sh
[root@yanjiaxi yjx]# cat a.sh my name is yanjiaxi [root@yanjiaxi yjx]# cat b.sh hello linux [root@yanjiaxi yjx]# cat {a.sh,b.sh} > commenfile [root@yanjiaxi yjx]# cat commenfile my name is yanjiaxi hello linux
[root@yanjiaxi yjx]# cp a.{sh,sh.bak} a.sh a.sh.bak
---------------------------------------------------------------------------------------------------------------------- 我们尊重原创,也注重分享,文章来源于微信公众号:Linux与数据库自学之道,建议关注公众号查看原文。如若侵权请联系qter@qter.org。 ----------------------------------------------------------------------------------------------------------------------
|