• shell编程01_Shell基础


    达内教育
    最新版Linux Shell脚本全套,终于讲明白了!
    丁明一老师

    shell编程01_Shell基础


    shell命令 需要shell命令解释器

     

    常见的shell解释器
    /bin/bash
    /bin/sh
    /bin/csh
    /bin/tcsh

    解释器:将用户的指令翻译为内核可以识别的指令
    通过usermod、chsh可以更改登录shell


    创建临时的用户的时候,如果我们没有指定指定解释器的时候,
    默认的解释器为/bin/bash

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# useradd test1
    2. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
    3. test1:x:1001:1001::/home/test1:/bin/sh


    usermod  chsh 来修改解释器

    通过 /etc/shells
    可以看到支持的shell解释器

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# cat /etc/shells
    2. # /etc/shells: valid login shells
    3. /bin/sh
    4. /bin/bash
    5. /usr/bin/bash
    6. /bin/rbash
    7. /usr/bin/rbash
    8. /bin/dash
    9. /usr/bin/dash

    我们也可以安装更多的shell解释器

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# apt-get install tcsh
    2. Reading package lists... Done
    3. Building dependency tree       
    4. Reading state information... Done
    5. The following packages were automatically installed and are no longer required:
    6.   linux-headers-5.11.0-27-generic linux-hwe-5.11-headers-5.11.0-27
    7.   linux-image-5.11.0-27-generic linux-modules-5.11.0-27-generic
    8.   linux-modules-extra-5.11.0-27-generic
    9. Use 'apt autoremove' to remove them.
    10. The following NEW packages will be installed:
    11.   tcsh
    12. 0 upgraded, 1 newly installed, 0 to remove and 90 not upgraded.
    13. Need to get 427 kB of archives.
    14. After this operation, 1,363 kB of additional disk space will be used.
    15. Get:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 tcsh amd64 6.21.00-1 [427 kB]
    16. Fetched 427 kB in 7s (61.1 kB/s)                                              
    17. Selecting previously unselected package tcsh.
    18. (Reading database ... 227919 files and directories currently installed.)
    19. Preparing to unpack .../tcsh_6.21.00-1_amd64.deb ...
    20. Unpacking tcsh (6.21.00-1) ...
    21. Setting up tcsh (6.21.00-1) ...
    22. update-alternatives: using /bin/tcsh to provide /bin/csh (csh) in auto mode
    23. Processing triggers for man-db (2.9.1-1) ...


    再次查看支持的shell解释器

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# cat /etc/shells
    2. # /etc/shells: valid login shells
    3. /bin/sh
    4. /bin/bash
    5. /usr/bin/bash
    6. /bin/rbash
    7. /usr/bin/rbash
    8. /bin/dash
    9. /usr/bin/dash
    10. /bin/tcsh
    11. /usr/bin/tcsh


    test1 用户默认shell解释器为/bin/bash

    修改默认的shell解释器方法
    usermod -s /bin/tcsh test

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# usermod -s /bin/tcsh test1
    2. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
    3. test1:x:1001:1001::/home/test1:/bin/tcsh


    可以使用chsh -s /bin/bash  test1  修改用户默认shell解释器
    chsh:change shell的意思

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# chsh -s /bin/bash test1
    2. root@ubuntu-virtual-machine:/home/ubuntu/songweibin/shell# grep test1 /etc/passwd
    3. test1:x:1001:1001::/home/test1:/bin/bash


    为什么默认支持/bin/bash 呢?
    因为Bash 很多功能和特性

    快捷键 Tab键补齐
    快捷键:
    Ctrl + a 命令行的最前面
    Ctrl + e 命令行的最后面
    Ctrl + C 当前执行的命令撤销
    Ctrl + l 清屏
    Tab键补齐:命令补齐 选项都可以补齐


    命令历史(history)
    通过上下键翻阅历史

    命令的别名(aliase)
    ll= ls-l

    标准输入与输出的重定向(>、 >>、2>、2>>、&>、 &>>)

    > : 正确的信息重定向
    2>: 错误的信息重定向
    &>: 既要正确的信息又要错误的信息

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls
    2. 42fputcandwrite  84fatherandsoncommunication    fl
    3. 82testpipe       85brotherscommunication        shell
    4. 83mypipe         87fileforprocesscommunication  test
    5. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls > a.txt
    6. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt
    7. 42fputcandwrite
    8. 82testpipe
    9. 83mypipe
    10. 84fatherandsoncommunication
    11. 85brotherscommunication
    12. 87fileforprocesscommunication
    13. a.txt
    14. fl
    15. shell
    16. test

    为什么还有a.txt自己呢?

    ls > a.txt 命令是都是先创建 a.txt文件
    然后将ls 的结果输入到a.txt中

    如果使用两个大于号的时候,是表示追加
    是在原有内容的基础上追加内容

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls >>a.txt
    2. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt 
    3. 42fputcandwrite
    4. 82testpipe
    5. 83mypipe
    6. 84fatherandsoncommunication
    7. 85brotherscommunication
    8. 87fileforprocesscommunication
    9. a.txt
    10. fl
    11. shell
    12. test
    13. 42fputcandwrite
    14. 82testpipe
    15. 83mypipe
    16. 84fatherandsoncommunication
    17. 85brotherscommunication
    18. 87fileforprocesscommunication
    19. a.txt
    20. fl
    21. shell
    22. test
    23. root@ubuntu-virtual-machine:/home/ubuntu/songweibin


    使用2>将错误信息导出到指定文件

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls song.txt
    2. ls: cannot access 'song.txt': No such file or directory
    3. root@ubuntu-virtual-machine:/home/ubuntu/songweibin

    使用>只能输出正确的信息。

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls song.txt > a.txt
    2. ls: cannot access 'song.txt': No such file or directory
    3. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt

    使用2>将错误信息导出到指定文件

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# ls song.txt 2> a.txt
    2. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat a.txt
    3. ls: cannot access 'song.txt': No such file or directory

    也可以使用&> (也是重新创建文件,会覆盖)可以同时输出正确和错误的信息

    单>: 都是重新创建输出的文件,所以会覆盖
    双>:都是追加


    管道(|)

    可以将第一条命令的输出结果作为第二条命令的输入

    搜索安装过的软件:

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# apt-cache search all|grep tcsh
    2. tcsh - TENEX C Shell, an enhanced version of Berkeley csh
    3. root@ubuntu-virtual-machine:/home/ubuntu/songweibin

    apt-get install ksh
    添加新的解释器

    1. root@ubuntu-virtual-machine:/home/ubuntu/songweibin# cat /etc/shells
    2. # /etc/shells: valid login shells
    3. /bin/sh
    4. /bin/bash
    5. /usr/bin/bash
    6. /bin/rbash
    7. /usr/bin/rbash
    8. /bin/dash
    9. /usr/bin/dash
    10. /bin/tcsh
    11. /usr/bin/tcsh
    12. /usr/bin/ksh2020
    13. /usr/bin/rksh2020


     通过ksh 直接使用ksh解释器
     
     ksh中只能通过clear 来清屏
     无法通过Ctrl+l

     
     
     ksh 无法使用方向键 所以默认使用/bin/bash
     

    # ^[[C^[[C^[[C^[[C^[[C^[[D^[[D^[[D^[[D^[[A^[[A^[[A^[[B^[[B


     
    ksh 通过exit返回上次默认的解释器

    1. # exit
    2. root@ubuntu-virtual-machine:/home/ubuntu/songweibin

    Shell执行命令的方式

    1.交互式(命令行)
    人工干预
    逐条解释执行 效率低

    2.非交互式(脚本)
    需要提前设计
    批量执行 效率高

  • 相关阅读:
    Git版本控制管理——版本库管理
    链表删除-leetcode19. 删除链表的倒数第 N 个结点
    高楼扔鸡蛋问题
    Matlab:使用plot函数绘制数据曲线
    【JAVA学习笔记】67 - 坦克大战1.5 - 1.6,防止重叠,记录成绩,选择是否开新游戏或上局游戏,播放游戏音乐
    Hard negtive node(硬负样本节点)与 Easy negative nodes(简单样本节点)
    关于谷歌浏览器设置打开时页面不起作用的解决方法
    【JavaScript高级程序设计】重点-第五章笔记:Date、RegExp、原始值包装类、单例内置对象
    赋能工业数字化转型|辽宁七彩赛通受邀出席辽宁省工业互联网+安全可控先进制造业数字服务产业峰会
    【框架】Flask
  • 原文地址:https://blog.csdn.net/sinat_36070482/article/details/125532098