Linux基础系列-Day8

Shell编程基础

Shell介绍

Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器)。它类似于windows下的的cmd.exe。它接收用户命令,然后调用相应的应用程序,shell可以使命令行也就可以是图形界面,用来提供人机交互。

通配符

通配符可以理解为shell的特殊代号字符,通配符就是一类特殊符号的集合,在shell解释器中有特殊的含义。

通配符字符含义说明:

  ~   表示用户家目录

[root@linux-test etc]# pwd
/etc
[root@linux-test etc]# cd ~
[root@linux-test ~]# pwd
/root

  ``和$()  被``和()裹起来的命令先执行

[root@linux-test ~]# echo `ls`
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@linux-test ~]# echo $(ls)
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@linux-test ~]# echo ls
ls

  !历史  可以调用历史命令,也表示逻辑非(下面^部分说明)

[root@linux-test test]# history 
    1  pwd
    2  history 
[root@linux-test test]# !1      匹配历史命令编号
pwd
/root/test
[root@linux-test test]# !pwd    匹配最近一条历史
pwd
/root/test

  *   表示任意多个字符

[root@linux-test ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music   Pictures  Public  Templates  Videos
[root@linux-test ~]# ls *.cfg              #查看以.cfg结尾文件和目录,之前字符任意全匹配
anaconda-ks.cfg  initial-setup-ks.cfg

  ?   表示一个字符,可以多个?同时用

[root@linux-test test]# ls
123.txt  12.txt  1.txt  45ab.txt
[root@linux-test test]# ls ?.txt
1.txt
[root@linux-test test]# ls ??.txt
12.txt

  []   代表一个和,如[0-9]表示0-9十个数字,[1,3]表示1和3

[root@linux-test test]# ls
123.txt  12.txt  16.txt  1.txt  45ab.txt
[root@linux-test test]# ls 1[0-9].txt
12.txt  16.txt

  ^   表示取反,这个通配符必须要在[]中使用,如下显示不是以cfg结尾的文件和目录,^可以用!取代

[root@linux-test ~]# ls *[^cfg]
Desktop:
Documents:
Downloads:
Pictures:
Templates:
Videos:

  @   无意义,一般用来隔离字符串,一般用于文件内容中

  #   用于注释字符串,一般用于配置文件中的注释

  $   取值,一般用于变量取值

[root@linux-test test]# x=5
[root@linux-test test]# echo x
x
[root@linux-test test]# echo $x
5

  %   用来杀死一个进程

  &  后台执行命令

[root@linux-test test]# touch aaa.txt &
[1] 46465    &挂后台执行会反馈一个进程号

  &&  逻辑与,当前一个指令执行成功时,执行后一个指令

[root@linux-test test]# ls && pwd
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt
/root/test

  ||  逻辑或,当前一个指令执行失败时,执行后一个指令

[root@linux-test test]# lp || pwd
lp: Error - no default destination available.
/root/test
[root@linux-test test]# ls || pwd
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt

  ()  子进程运行命令

[root@linux-test test]# (y=6)
[root@linux-test test]# echo $y      #由于不是在当前进程中执行,所以无法查看变量

  +-*/=  运算符,多用于循环和判断语句中

  ''  硬引用,里面的字符无意义

  ""  软引用,具有变量置换功能

[root@linux-test test]# z=6
[root@linux-test test]# echo 'Num = $z'
Num = $z
[root@linux-test test]# echo "Num = $z"
Num = 6

  :  执行后永远为真

  echo $?  上一次执行命令的执行状态,0表示成功

[root@linux-test test]# ls
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt
[root@linux-test test]# echo $?
0
[root@linux-test test]# aaaa
bash: aaaa: command not found...
[root@linux-test test]# echo $?
127
[root@linux-test test]# :
[root@linux-test test]# echo $?
0

  {}  多个文件集合

[root@linux-test test]# ls
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt
[root@linux-test test]# ls {123.txt,12.txt}
123.txt  12.txt

  /    路径分隔符号

  >和>>     输出重定向导向,分别为"覆盖"与"累加" 

变量

变量是用来表示程序运行时候改变的状态的,是一个抽象的概念。

变量组成:

  [变量名]=[变量实际的值]

  变量名:命名一般字母或下划线开头,剩下的可以是字母和数字,变量名不能和关键字冲突,如ls=1

  变量值:可以改变的值,一个变量名可以重复赋值,但是变量实际的值为最后赋值后的值

变量类型:

  局部变量:只在某一段代码中生效的变量

  全局变量:在整个程序的运行中生效的变量

[root@linux-test ~]# n=1          #当前bash进程中设置局部变量
[root@linux-test ~]# bash          #切换bash到子进程
[root@linux-test ~]# echo $n        #查看变量为空

[root@linux-test ~]# export x=2      #当前bash进程中设置全局变量
[root@linux-test ~]# bash          #切换bash到子进程
[root@linux-test ~]# echo $x        #查看变量
2

Shell命令执行优先级

==> alias        别名
  ==> Compound Commands    复合命令(if、for、while等)
    ==> function               函数
      ==> build_in                内置命令(cd等)
        ==> hash                    hash命令,缓存中用过的命令
          ==> $PATH                    环境变量标明的命令
            ==> error: command not found              找不到命令时候

bash环境

以下四个文件在登录用户的过程中会依次执行

  /etc/profile

  ~/.bash_profile

  ~/.bashrc

  /etc/bashrc

su user不加-登录,只会加载~/.bashrc、/etc/bashrc

全局环境变量配置一般放在/etc/profile文件中,用户级环境变量一般放在~/.bash_profile

临时设置环境变量:

[root@linux-test ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@linux-test ~]# PATH=/usr/local/nginx/sbin/:$PATH
[root@linux-test ~]# echo $PATH
/usr/local/nginx/sbin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

环境变量中查找命令的顺序是依次往后,比如ls命令在当前设置的变量中,首先会从/usr/local/nginx/sbin下找,找不到会从/usr/local/sbin下找,依次找到为止。

永久设置环境变量:

[root@linux-test ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile    #将环境变量的修改写入到开启加载的脚本文件中即可

grep命令详细使用说明

  -v  反转匹配

  -i  忽略大小写匹配

  -r  递归匹配

  -w  单词匹配,属完全匹配,只要有分隔符分割即可

  -n  显示行号

  -q  静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容

  -A  如果匹配成功,则将匹配行及其后n行一起打印出来

  -B  如果匹配成功,则将匹配行及其前n行一起打印出来

  -C  如果匹配成功,则将匹配行及其前后n行一起打印出来

  -c  匹配成功,只打印匹配的行数(一共匹配到多少行的数量)打印出来

  -E  等于egrep,扩展,多用于加入正则表达式后的匹配

  -l  列出文件内容符合指定的范本样式的文件名称,多和-r联用

  -L  列出文件内容不符合指定的范本样式的文件名称,多和-r联用

  -s  不显示错误信息

  -o  只输出文件中匹配到的部分

正则表达式

正则表达式和通配符一样,也是一组特殊符号,通配符是由shell解释执行,正则表达式是由命令解释。

原文地址:https://www.cnblogs.com/zero527/p/6930470.html