设为首页 收藏本站
查看: 1320|回复: 0

linux的shell基础介绍(1)

[复制链接]

尚未签到

发表于 2018-8-21 06:22:20 | 显示全部楼层 |阅读模式
  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    //更改存储命令变量,进入这个文件找到以下图示位置更改
DSC0000.png

[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效果,就进入配置文件加上如下这条变量命令,保存退出。
DSC0001.png

[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: 没有那个文件或目录



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-554393-1-1.html 上篇帖子: shell执行过程 下篇帖子: shell 解码url url decode
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表