您的位置:首页 > 博客中心 > 网络系统 >

linux的shell基础介绍(1)

时间:2022-04-03 13:36

8.1 shell介绍:器之间的交互


1、shell是一个命令解释器,提供用户和机器之间的交互

2、 支持特定语法,比如逻辑判断、循环

3、每个用户都可以有自己特定的shell

4、 CentOS7默认shell为bash(Bourne Agin Shell)

5、 还有zsh、ksh等


查看系统是否有安装zsh、ksh,示例如下:

[root@aminglinux-01 ~]# yum list |grep zsh
zsh.x86_64                              5.0.2-25.el7                   installed
autojump-zsh.noarch                     22.3.0-3.el7                   epel    
zsh.x86_64                              5.0.2-28.el7                   base    
zsh-html.x86_64                         5.0.2-28.el7                   base    
zsh-lovers.noarch                       0.9.0-1.el7                    epel    
[root@aminglinux-01 ~]# yum list |grep ksh
ksh.x86_64                              20120801-34.el7                base    
mksh.x86_64                             46-5.el7                       base    
python-XStatic-Rickshaw.noarch          1.5.0.0-4.el7                  epel    
python-moksha-common.noarch             1.2.3-2.el7                    epel    
python-moksha-hub.noarch                1.5.3-2.el7                    epel    
python-moksha-wsgi.noarch               1.2.2-2.el7                    epel


8.2 命令历史:


1、history命令             //查看之前使用的命令。

2、.bash_history       //存储使用过的命令文件,在root的家目录下

3、最大1000条           //存储使用命令的最大数

4、变量HISTSIZE

5、/etc/profile中修改

6、HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

7、永久保存 chattr +a ~/.bash_history

8、!!                        //执行上一条命令

9、!n                     //n表示数字   比如:!176   这样它就会执行命令历史里的176这条命令

10、!echo            //echo表示你需要找命令历史里从下往上找以word开头的第一条命令执行它。


示例如下:

[root@aminglinux-01 ~]# ls /root/.bash_history
/root/.bash_history
[root@aminglinux-01 ~]# cat /root/.bash_history
[root@aminglinux-01 ~]# history
    1  ls -l /tmp/
    2  ls -l
......
  585  ls /root/.bash_history
  586  cat /root/.bash_history
  587  history
[root@aminglinux-01 ~]# echo $HISTSIZE
1000
[root@aminglinux-01 ~]# history -c         //清空当前命令历史记录
[root@aminglinux-01 ~]# history
    1  history
[root@aminglinux-01 ~]# cat .bash_history    //查看是否清空了配置文件命令,没有,只能清空当前命令历史的。
ls -l /tmp/
ls -l
which rm

注意:当前使用的命令并不会直接保存到.bash_history配置文件里面去,只有当你退出终端的时候才会保存到配置文件里。


更改命令历史变量数值:

[root@aminglinux-01 ~]# vi /etc/profile    //更改存储命令变量,进入这个文件找到以下图示位置更改

[root@aminglinux-01 ~]# echo $HISTSIZE           //保存退出后查看变更并没有生效
1000
[root@aminglinux-01 ~]# source /etc/profile         //更改变量扣执行这条命令
[root@aminglinux-01 ~]# echo $HISTSIZE       //完成更改
5000
[root@aminglinux-01 ~]# history
    1  history
    2  cat .bash_history
    3  vi /etc/profile
    4  echo $HISTSIZE
    5  source /etc/profile
    6  echo $HISTSIZE
    7  history

更改命令历史格式:

[root@aminglinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@aminglinux-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@aminglinux-01 ~]# history
    1  2017/11/14 16:45:35 history
    2  2017/11/14 16:46:43 cat .bash_history
    3  2017/11/14 17:59:55 vi /etc/profile
    4  2017/11/14 18:04:45 echo $HISTSIZE
    5  2017/11/14 18:05:12 source /etc/profile
    6  2017/11/14 18:05:14 echo $HISTSIZE
    7  2017/11/14 18:06:30 history
    8  2017/11/14 18:07:10 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
    9  2017/11/14 18:07:34 echo $HISTTIMEFORMAT
   10  2017/11/14 18:07:57 history
[root@aminglinux-01 ~]# vim /etc/profile  //如果想要保存如上history效果,就进入配置文件加上如下这条变量命令,保存退出。

[root@aminglinux-01 ~]# source /etc/profile 
[root@aminglinux-01 ~]# echo $HISTTIMEFORMAT    //这样哪怕再开一个终端这也保存了。
%Y/%m/%d %H:%M:%S

为了让命令历史永久保存,不想让其它人去破坏它,可以给它加一个特殊的权限。

[root@aminglinux-01 ~]# chattr +a ~/.bash_history   //运行这条命令就可以。

注意:如果执行了这条命令后,你并没有正常退出终端,就会导致保存的命令不全。


8.3 命令补全和别名:


1、tab键,敲一下,敲两下

2、参数补全,安装bash-completion

3、alias别名给命令重新起个名字

4、 各用户都有自己配置别名的文件 ~/.bashrc    //存放别名的路径地址

5、ls /etc/profile.d/                              //存放别名的路径地址

6、 自定义的alias放到~/.bashrc         //自定义后别名存放的地址


示例如下:

[root@aminglinux-01 ~]# systemctl restart network  //使用tab补全这条命令参数
[root@aminglinux-01 ~]# yum install -y bash-completion //安装这个库
[root@aminglinux-01 ~]# reboot                       //重启系统
[root@aminglinux-01 ~]# alias restartnet=‘systemctl restart network.service‘ //alias别名给命令重新命名
[root@aminglinux-01 ~]# restartnet                  //设置好的别名命令生效
[root@aminglinux-01 ~]# alias            //查看已有的别名
alias cp=‘cp -i‘
alias egrep=‘egrep --color=auto‘
alias fgrep=‘fgrep --color=auto‘
alias grep=‘grep --color=auto‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias restartnet=‘systemctl restart network.service‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@aminglinux-01 ~]# unalias restartnet    //取消设置好的别名
[root@aminglinux-01 ~]# restartnet             //别名命令失效
-bash: restartnet: 未找到命令


8.4 通配符:


1、ls *.txt              //表示查找.txt通配文件

2、ls ?.txt             //表示一个任意的字符

3、ls [0-9].txt        //列出满足条件范围内的文件

4、ls {1,2}.txt        //用花括号列出你需要的文件


示例如下:

[root@aminglinux-01 ~]# ls
111  1_heard.txt.bak  1.txt.bak  2.txt  456     aminglinux      
123  1_sorft.txt.bak  234        3.txt  aming2  anaconda-ks.cfg
[root@aminglinux-01 ~]# ls *.txt
2.txt  3.txt
[root@aminglinux-01 ~]# ls *txt
2.txt  3.txt
[root@aminglinux-01 ~]# ls *txt*
1_heard.txt.bak  1_sorft.txt.bak  1.txt.bak  2.txt  3.txt
[root@aminglinux-01 ~]# ls 1*
1_heard.txt.bak  1_sorft.txt.bak  1.txt.bak

111:

123:
[root@aminglinux-01 ~]# ls ?.txt
2.txt  3.txt
[root@aminglinux-01 ~]# touch 1.txt
[root@aminglinux-01 ~]# ls ?.txt
1.txt  2.txt  3.txt
[root@aminglinux-01 ~]# touch a.txt
[root@aminglinux-01 ~]# touch bb.txt
[root@aminglinux-01 ~]# ls ?.txt
1.txt  2.txt  3.txt  a.txt
[root@aminglinux-01 ~]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@aminglinux-01 ~]# ls [12].txt
1.txt  2.txt
[root@aminglinux-01 ~]# ls [23].txt
2.txt  3.txt
[root@aminglinux-01 ~]# ls {1,2}.txt
1.txt  2.txt


8.5 输入输出重定向


1、cat 1.txt >2.txt        //>它会把命令产生的正确信息输出到指定文件里去,删除之前文件内容重写。

2、cat 1.txt >> 2.txt     //>>把前面命令输出内容重定向追加到后面命令里去,不删除旧的。

3、ls aaa.txt 2> err      //它会把命令产生的错误信息输出到指定文件里去

4、ls aaa.txt 2>> err   //  错误信息输出追加重定向                                                  

5、wc -l < 1.txt           //查看一个文件的行数


示例如下:

[root@aminglinux-01 ~]# lsaaa         //输出一条错误信息
-bash: lsaaa: 未找到命令
[root@aminglinux-01 ~]# lsaaa 2> a.txt   //保存一条信息到指定文件里去
[root@aminglinux-01 ~]# cat a.txt            //查看结果
-bash: lsaaa: 未找到命令
[root@aminglinux-01 ~]# lsaaa 2>> a.txt    //追加一次错误信息到文件
[root@aminglinux-01 ~]# cat a.txt               //查看结果
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
[root@aminglinux-01 ~]# ls [12].txt aaa.txt &> a.txt     //&>结合正确错误信息重定向到一个文件里去
[root@aminglinux-01 ~]# cat a.txt                           //查看结果
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
[root@aminglinux-01 ~]# ls [12].txt aaa.txt &>> a.txt     //追加正确错误信息重定向
[root@aminglinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
[root@aminglinux-01 ~]# ls [12].txt aaa.txt > 1.txt  2>a.txt    //既有正确也有错误的输出,区分开来保存在指定的文件里。
[root@aminglinux-01 ~]# cat 1.txt
1.txt
2.txt
[root@aminglinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录


本文出自 “” 博客,请务必保留此出处

本类排行

今日推荐

热门手游