cdchenli 发表于 2018-8-18 06:34:35

shell特性、shell变量、shell通配符

  shell特性
  1、history
  # history                        查看shell的命令历史
  # echo $HISTSIZE         查看历史记录的变量
  1000
  # !!                                  查看上一条命令
  echo $HISTSIZE
  1000
  # ls 1234.txt
  1234.txt
  # cat !$                           !$代表上一条命令的最后一个参数
  cat 1234.txt
  jlkdjlkfjslkf# history
  1171ls 1234.txt
  1172cat 1234.txt
  1173history
  # !1172                     !+命令历史编号,执行1172条命令,
  cat 1234.txt
  jlkdjlkfjslkf#
  # !c                              !c 执行命令历史中最近使用的以c开头的命令
  cat 1234.txt
  jlkdjlkfjslkf
  双击tab键补全命令
  2、别名alias
  # alias aaa='cat 1234.txt'   只在当前shell中有效,需要加入配置文件中,其他shell才能生效
  # alias
  alias aaa='cat 1234.txt'
  # aaa
  jlkdjlkfjslkf
  # unalias aaa                     取消aaa别名
  3、通配符
  * 匹配任何字符
  # ls *.txt                           列出了当前目录下任何以txt结尾的文件
  1234.txt123.txt12.txt1.txttest.txt
  ?匹配一个字符
  # ls ?.txt                           一个问号?匹配一个字符
  1.txt
  # ls ???.txt                        三个问号?匹配三个字符
  123.txt456.txt
  4、管道符 | 作用是把一条命令的结果丢给另一条命令
  # cat /etc/passwd | wc -l      查看/etc/passwd文件内容,查看行数
  32
  5、重定向
  重定向 >覆盖之前的内容
  # cat 1.txt
  test
  # echo "12345678" > 1.txt
  # cat 1.txt                           内容已经更改
  12345678
  追加重定向 >不会覆盖之前的内容
  # echo "9 10 11 12" >> 1.txt
  # cat 1.txt                            内容已经添加,并且之前内容没有被覆盖
  12345678
  9 10 11 12
  反向重定向 (把错误信息重定向到指定文件中),覆盖文件内容
  # ls 1111
  ls: 无法访问1111: 没有那个文件或目录
  # ls 1111 > 1.txt                错误信息不能重定向到1.txt文件中
  ls: 无法访问1111: 没有那个文件或目录
  # cat 1.txt
  # ls 1111 2> 1.txt            使用2> 可以将错误信息重定向到1.txt中
  # cat 1.txt
  ls: 无法访问1111: 没有那个文件或目录
  追加错误重定向,错误信息追加后不会将之前的文件内容覆盖
  # ls 1111 2>> 1.txt
  # cat 1.txt
  ls: 无法访问1111: 没有那个文件或目录
  ls: 无法访问1111: 没有那个文件或目录
  6、进程的前后台调用fg、bg,暂停ctrl+z,查看jobs
  # sleep 1234                     ctrl+Z中断到后台
  ^Z
  +Stopped               sleep 1234
  # jobs                                 3+优先级更高,2-优先级次,1优先级最低
     Stopped               sleep 100
  -Stopped               sleep 166
  +Stopped               sleep 1234
  # fg                                    fg调用优先级最高的到前台继续运行,调用的是默认的3+
  sleep 1234
  # fg 2                                 fg 2调用指定的id,id=2优先级升高
  sleep 166
  # jobs
  +Stopped               sleep 166
     Stopped               sleep 1234
  # fg
  sleep 166
  # fg
  sleep 1234
  ^Z
  +Stopped               sleep 1234
  # bg                                     使用bg将id=3的进程调到后台继续运行
  + sleep 1234 &
  # jobs
  +Running               sleep 1234 &            Running表示在后台继续运行
  shell变量
  系统变量和自定义变量   变量名不大写,不用ls等系统命令,不用数字开头,不用一些关键字if、else等
  1、系统环境变量
  # env                              查看系统环境变量
  HOSTNAME=daixuan
  TERM=xterm
  SHELL=/bin/bash
  # echo $HOSTNAME         显示变量的内容,或者引用变量,使用$
  daixuan
  # a=1
  # set                              set输出所有环境变量,包括系统变量和自定义变量
  BASH=/bin/bash
  a=1
  # a=1;b=2;a_2=12
  # echo $a $b $a_2
  1 2 12
  # b='ls /tmp/'                  变量内容包含空格、*,# 等字符需要单引号‘’
  # echo $b
  ls /tmp/
  # which vim
  /usr/bin/vim# myvim=`which vim`   使用反引号``,把命令作为变量来使用/usr/bin/vim
  # echo $myvim
  /usr/bin/vim
  # d="$myvim"3                把$myvim=/usr/bin/vim当做变量
  # echo $d                        输出变量d的内容
  /usr/bin/vim3                                             /usr/bin/vim3
  有时候使用变量需要做全局申明
  # bash                               输入bash进入到子shell,则子shell的变量已经不能使用了
  # echo $d                        输出为空
  # export a=1
  # bash
  # echo $a1
  # unset a
  # echo $a                            值为空
  注:反引号用来直接引用反引号里面的命令,双引号里面如果有特殊符合比如说$,它是可以识别其含义的。而单引号里面如果有特殊符号,是不被识别的
  PATH HOME SHELL定义环境变量
  1、/etc/profile 表示系统内针对任何用户(root或daixuan)都生效的环境变量的配置文件
  # vim /etc/profile       /etc/profle 会定义很多环境变量,直接修改/etc/profile是不太好的,最好是创建/etc/profile.d/custom.sh
  # /etc/profile
  # System wide environment and startup programs, for login setup
  # Functions and aliases go in /etc/bashrc

  # It's NOT a good>  # are doing. It's much better to create a custom.sh shell script in /etc/profile.d/
  # vim /etc/profile.d/path.sh
  #!/bin/bash
  export PATH=$PATH:/tmp/:data/bin/
  # source /etc/profile             让配置文件生效,source加载/etc/profile,会加载/etc/profile.d/下的所有.sh文件
  # echo $PATH
  /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/:data/bin/                                                                                     a/bin/
  2、/etc/bashrc                                                      全局的环境变量别名
  # vim /etc/bashrc
  # /etc/bashrc
  # System wide functions and aliases
  # Environment stuff goes in /etc/profile

  # It's NOT a good>  # are doing. It's much better to create a custom.sh shell script in
  # /etc/profile.d/ to make custom changes to your environment, as this
  # will prevent the need for merging in future updates.
  3、~/.bash_profile定义用户自己的一些环境变量的配置文件
  4、~/.bashrc用户自定义alias命令写入下面那个配置文件 ,用户登录时或者打开shell执行的文件
  shell通配符
  * 任何字符
  ?一个字符
  #注释符号,解释说明 # #ls 1.txt   没有结果,因为命令已经注释掉
  \ 脱意符号
  # ls #1.txt       111      1234.txt   1.tar.gz相当于# ls ,#后面的没有任何意义
  # ls \#1.txt                                                   系统认为#1.txt是一个目录
  ls: 无法访问#1.txt: 没有那个文件或目录
  # touch \#1.txt                                              创建#1.txt文件
  # ls \#*                                                          创建#1.txt文件成功
  #1.txt
  | 管道符,把一条命令的结果丢给另一条命令
  # cat 1.txt | wc -l
  2
  $
  # echo $PATH            表示在使用这个变量
  /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/:data/bin/
  # cat !$                      !$表示上一条命令的最后一个参数
  cat $PATH
  在vim中$符号作为一行的行尾
  ; 把两条命令写在一行,中间加分号
  # ll 1234.txt;ll 123.txt
  -rw-r--r-- 1 root root 24 10月 29 17:38 1234.txt
  -rw-r--r-- 1 root root 13 10月 25 00:37 123.txt
  &把一条命令移动到后台运行
  # sleep 10&
   22423
  # jobs
  +Running               sleep 10 &
  # jobs
  +Done                  sleep 10
  &&表示把两条命令写在一起
  # ls && touch 1.txt
  >          重定向,覆盖内容
  >>       追加重定向,不覆盖内容
  2>      错误重定向,错误信息追加,覆盖内容
  2>>   错误追加重定向,错误信息追加,但是不覆盖
  <         反重定向,把右边文档的内容重定向给左边的命令
  # wc -l < 1.txt
  2
  表示0-9之间的任何一位数字
  # ls .txt
  1.txt2.txt
  # ls 1.txt 2.txt
  1.txt2.txt
  # ls .txt
  1.txt2.txt
  # ls .txt
  1.txt2.txtb.txtC.txt

页: [1]
查看完整版本: shell特性、shell变量、shell通配符