zoukankan      html  css  js  c++  java
  • 1、Bash Shell

    一、什么是Bash shell

    BashShell是一个命令解释器,它在操作系统的最外层,负责用户程序与内核进行交互操作的一种接口,将用户输入的命令翻译给操作系统,并将处理后的结果输出至屏幕。
    当我们使用远程连接工具连接linux服务,系统则会给打开一个默认的shell,我们可在这个界面执行命令、比如:获取系统当前时间,创建一个用户等等

    二、Bash shell 能做什么?

    使用Shell实现对Linux系统的大部分管理,例如:

    1. 文件管理
    2. 权限管理
    3. 用户管理
    4. 磁盘管理
    5. 网络管理
    6. 软件管理
    7. 等等

    三、Bash shell执行方式

    输入命令方式 --->效率低--->适合少量的工作

    shell script --->效率高--->适合复杂的工作

    四、了解Bash shell

    Bash ---> GNU/Bash

    [root@www ~]# bash -version
     GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

    Bash是默认的shell,其实还用很多的其他shell,例如:csh,sh等

    五、shell提示符

    $ = 普通用户,# = root用户(超级管理员)

     [root@xuliangwei ~]# whoami
     root
    
     [root@www ~]# echo $PS1
     [u@h W]$

    root:当前登录系统的用户

    www:当前系统的主机名称

    ~:当前所在的位置

    :通常情况下,是超级管理员的身份

    :符号

    命令行bash shell,为用户提供输入,执行命令的界面

    命令 选项 参数

    command [-options] [arguments]

    [root@xuliangwei ~]# ls #命令
    [root@xuliangwei ~]# ls -a #命令+选项
    [root@xuliangwei ~]# ls -a /home/ #命令+选项+参数

    命令:整条shell命令的主体

    选项:用于调节命令的具体功能

    #以 “-”引导短格式选项(单个字符),例如“-a”
    #以“--”引导长格式选项(多个字符),例如“--all”
    #多个短格式选项可以写在一起,只用一个“-”引导,例如“-al”
    

    参数:命令操作的对象,如文件、目录名等

    注意: 选项和参数在有些命令的时候时,位置可以发生变化。

    注意: 命令必须开头, 选项和参数位置可以发生变化

    出错的例子:

    [root@www ~]# ls-a
    bash: ls-a: command not found...    找不到命令(要么命令写错了,要么就真的没有该命令)
    
    [root@www ~]# ls /.bashrc
    ls: cannot access /.bashrc: No such file or directory   没有这个文件或目录

    六、Bash shell的特性

    1. Tab键补全支持

      命令补全 选项补全 参数补全

    命令补全: 按一下tab键没有任何反应,因为以user开头的命令有很多,系统并不知道你需要使用哪一个

    [root@www ~]# user
     useradd userdel userhelper usermod usernetctl users 

    路径补全:/etc/sysconfig/network-scripts/ifcfg-ens32

    1. bash shell常用快捷键

      ctrl +a 跳转光标至本行的首部
      ctrl +e 跳转光标至本行的尾部
      ctrl +u 删除从光标到行首的所有字符
      ctrl +k 删除从光标到行末的所有字符
      ctrl +l 清屏,还可以使用clear命令
      ctrl +c 终止当前的任务
      ctrl +d 退出服务器, 还可以使用logout或exit
      ctrl +w 按空格删除光标之前的内容
      ctrl +z 将当前的任务挂起
      ctrl +r 搜索命令,
      ctrl +s 锁住屏幕,解锁ctrl+c [忽略]
      ctrl + 左右方向键 按照单词快速的跳转光标, Xshell工具提供的

      号符: 注释(代表不执行)

    2. 历史记录history

    history 命令直接查看当前shell所执行过的命令

    !100 调用history历史命令中编号为100

    !! 执行上一次执行过的命令

    选项

    选项 描述
    -w 保存命令历史到历史文件 write #写入到当前用户的家目录下.bash_history
    -c 清空命令历史记录,不会清空文件 clear
    -d 删除命令历史到第N行 delete

    Esc+. 获取上一个命令的参数

    例子:
    保存命令历史到历史文件
    [root@liyang-98 ~]# history -w
    查看历史记录
    [root@liyang-98 ~]# history 
        1 uname -n
        2 man hostname
        ...
    清除历史记录
    [root@liyang-98 ~]# history -c
    [root@liyang-98 ~]# history 
        1 history 
    删除第二行历史记录
    [root@liyang-98 ~]# history 
        1 history 
        2 ls -al
        3 history 
    [root@liyang-98 ~]# history -d 2
    [root@liyang-98 ~]# history 
        1 history 
        2 history 
        3 history -d 2
        4 history 
    1. 命令别名alias

    1、如何设置别名

    [root@liyang-98 ~]# alias ifnet='vi /etc/sysconfig/network-scripts/ifcfg-ens32'
    [root@liyang-98 ~]# 
    [root@liyang-98 ~]# ifnet 
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=none
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=ens32
    UUID=251eb6ed-4bf8-4144-acd1-653f7fa3e908
    DEVICE=ens32
    ONBOOT=yes
    "/etc/sysconfig/network-scripts/ifcfg-ens32" 20L, 357C

    2、如何取消别名

    [root@liyang-98 ~]# unalias ifnet
    [root@liyang-98 ~]# ifnet
    -bash: ifnet: command not found

    3、为什么系统默认存在一些别名:

    防止在执行危险指令时,发生误操作的情况,比如:alias rm='rm -i'

    注意:当前的别名全部都是针对当前的Shell生效,临时生效。

    4.永久生效{扩展}

    将内容写入到 /etc/bashrc 所有的shell都生效

    5.命令帮助 --help man

    [root@www ~]# ls --help
    用法:ls [选项]... [文件]...

    ls 常见选项

    选项 描述
    -a 查看目录下的所有文件,包括隐藏文件
    -i 以长格式的方式显示文件的详细内容
    -h 以人性化的方式显示内容,配合-l使用
    -d 只列出目录名,不列出目录以下的内容
    -t 按修改时间进行排序
    -i 显示文件的inode号
    例子:
    1、显示当前目录下所有文件
    [root@liyang-98 ~]# ls -a
    . anaconda-ks.cfg .bash_logout .bashrc .pki
    .. .bash_history .bash_profile .cshrc .tcshrc
    2、以长格式显示当前目录下所有文件
    [root@liyang-98 ~]# ls -al
    total 28
    dr-xr-x---. 3 root root 147 Mar 29 10:52 .
    dr-xr-xr-x. 17 root root 224 Mar 28 17:17 ..
    -rw-------. 1 root root 1440 Mar 28 17:18 anaconda-ks.cfg
    -rw-------. 1 root root 1088 Mar 29 14:53 .bash_history
    -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
    -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
    -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
    -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
    drwxr-----. 3 root root 19 Mar 29 10:52 .pki
    -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
    3、以人性化的方式显示内容
    [root@liyang-98 ~]# ls -lh
    total 4.0K
    -rw-------. 1 root root 1.5K Mar 28 17:18 anaconda-ks.cfg
    4、只列出/etc目录的名字
    [root@liyang-98 ~]# ls -d /etc
    /etc
    5、按修改时间进行排序
    [root@liyang-98 ~]# ls -alt
    total 28
    -rw-------. 1 root root 1088 Mar 29 14:53 .bash_history
    dr-xr-x---. 3 root root 147 Mar 29 10:52 .
    drwxr-----. 3 root root 19 Mar 29 10:52 .pki
    -rw-------. 1 root root 1440 Mar 28 17:18 anaconda-ks.cfg
    dr-xr-xr-x. 17 root root 224 Mar 28 17:17 ..
    -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
    -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
    -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
    -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
    -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
    6、显示文件的inode
    [root@liyang-98 ~]# ls -li
    total 4
    67158083 -rw-------. 1 root root 1440 Mar 28 17:18 anaconda-ks.cfg




  • 相关阅读:
    表单校验神器
    插入排序
    数组去重的几种常使用的方式
    day44 mysql高级部分内容
    day43 多表查询和pymysql
    day42 字段的增删改查详细操作
    day41 mysql详细操作
    day40 mysql数据类型
    day39 mysql数据库基本操作
    day37 异步回调和协程
  • 原文地址:https://www.cnblogs.com/Forever-x/p/285862b0d2a8eb3761a219794c5a232e.html
Copyright © 2011-2022 走看看