zoukankan      html  css  js  c++  java
  • 01Shell入门01-bash Shell特性

    命令和文件自动补齐

    [root@hadoop04 ~]# yum -y install bash-completion
    

    命令历史记忆功能

    1.上下键 
    查看历史命令
    
    2.!number 
    执行history中第number个记录
    
    3.!string
    执行history最近的一个以某个字符串开头的命令
    
    4.!$ 
    上一条命令的最后一个参数
    
    5.!!
    上一条命令(完整的一条命令)
    
    6.^R
    搜索历史命令
    

    别名功能

    1.alias
    
    查看当前shell中的别名
    
    2.unalias 
    
    取消当前shell中的别名
    
    (1)在命令前面加上可以跳过别名,比如 alias cp='cp -i',cp 就是执行真正的'cp',而不会执行别名'cp -i'
    
    (2)定义永久生效的别名,需要在用户家目录下的.bashrc文件中( ~username/.bashrc)
    

    快捷键

    Ctrl + 具体某个字母键

    1.^R 
    搜索历史命令
    2.^D
    相当于 exit/logout,退出Shell
    3.^A
    光标移到到首部
    4.^E
    光标移到到尾部
    5.^L
    相当于clear,清屏
    6.^U
    剪切光标之前的所有
    7.^K
    剪切光标以及光标之后的所有
    8.^Y
    在光标处粘贴(由^U、^K剪切的命令可以通过这个方式粘贴)
    9.^S 
    锁屏
    10.^Q
    解锁
    

    前后台作业控制

    1.&
    后台运行,但是终端退出就结束了
    
    2.nohup
    后台运行,即使终端退出依旧不会结束
    
    3.^C
    结束前台进程
    
    4.^Z
    将前台进程切换到后台,并且暂停运行
    [root@hadoop04 ~]# sleep 800
    ^Z
    [1]+  Stopped                 sleep 800
    
    5.bg %1
    将一个在后台暂停的命令,变成继续执行
    [root@hadoop04 ~]# fg %1
    sleep 800
    ^Z
    [1]+  Stopped                 sleep 800
    [root@hadoop04 ~]# bg %1
    [1]+ sleep 800 &
    
    6.fg %1
    将后台运行的编号为1的job切换到前台继续运行
    
    7.kill %3
    向当前终端中job号为3的进程发送信号
    如果是 kill 3,意味着向当前终端中pid为3的进程发送信号
    
    8.screen(推荐)
    # 创建会话,名称为test_screen,如果不命名的话,就直接使用screen;之后在screen会话中操作
    [root@hadoop04 ~]# screen -S test_screen
    
    # 查看当前有哪些screen会话
    [root@hadoop04 ~]# screen -list
    There are screens on:
    	4585.test_Screen2	(Attached)
    	4553.test_screen	(Detached)
    2 Sockets in /var/run/screen/S-root.
    
    # 进入到某一个screen会话
    [root@hadoop04 ~]# screen -r 4553
    
    # 在具体screen会话中结束screen会话
    [root@hadoop04 ~]# exit
    exit
    [screen is terminating]
    

    输入输出重定向

    1. 0 标准输入(键盘)
    2. 1 标注输出(屏幕)
    3. 2 错误输出 (屏幕)
    4. > 输出重定向(覆盖) 
    5. >> 输出重定向(追加)
    6. 2> 错误输出重定向
    7. 2>>错误输出重定向 
    8. 2>&1 错误输出重定向到标准输出
    9. &> 混合输出
    10. cat < /etc/hosts 将/etc/hosts输入给cat,标准输出到屏幕
    11. cat < /etc/hosts > /etc/hosts1 将/etc/hosts输入给cat,再重定向输出到/etc/hosts1,相当于cp命令
    12. cat <<EOF
    [root@hadoop04 ~]# cat <<-EOF
    > 111
    > 222
    > 333
    > EOF
    111
    222
    333
    13. cat >file1 <<EOF  
    标准输入(以EOF为结束),输出重定向到文件file1
    [root@hadoop04 ~]# cat <<-EOF > test.txt
    > 111
    > 222
    > 333
    > EOF
    [root@hadoop04 ~]# cat test.txt 
    111
    222
    333
    

    管道 | tee

    | 前一个命令的输出,作为后一个命令的输出

    tee 从标准输入中复制到每一个文件,并输出到标准输出

    ip addr |grep 'inet ' |grep eth0

    # 将ip addr的输出作为grep的输入,grep 在输入中匹配出存在'inet '的行
    [root@hadoop04 ~]# ip addr |grep 'inet '
        inet 127.0.0.1/8 scope host lo
        inet 172.22.34.20/24 brd 172.22.34.255 scope global noprefixroute eth0
    
    # 将ip addr的输出作为grep的输入,grep 在输入中匹配出存在'inet '的行,再次作为输入给后一个grep 去匹配
    [root@hadoop04 ~]# ip addr |grep 'inet ' |grep eth0
        inet 172.22.34.20/24 brd 172.22.34.255 scope global noprefixroute eth0
    

    ip addr |grep 'inet ' |tee test.txt |grep eth0 (覆盖test.txt的内容)
    ip addr |grep 'inet ' |tee -a test.txt |grep eth0 (-a 追加到test.txt)

    # 将ip addr的输出作为grep的输入,grep 在输入中匹配出存在'inet '的行
    [root@hadoop04 ~]# ip addr |grep 'inet '
        inet 127.0.0.1/8 scope host lo
        inet 172.22.34.20/24 brd 172.22.34.255 scope global noprefixroute eth0
    
    # 将ip addr的输出作为grep的输入,grep 在输入中匹配出存在'inet '的行,再次作为输入传给tee,tee将内容保存到test.txt中,并且标准输出到屏幕
    [root@hadoop04 ~]# ip addr |grep 'inet '| tee test.txt
        inet 127.0.0.1/8 scope host lo
        inet 172.22.34.20/24 brd 172.22.34.255 scope global noprefixroute eth0
        
    [root@hadoop04 ~]# cat test.txt 
        inet 127.0.0.1/8 scope host lo
        inet 172.22.34.20/24 brd 172.22.34.255 scope global noprefixroute eth0
    
    [root@hadoop04 ~]# date &> date.txt
    
    [root@hadoop04 ~]# date| tee date.txt
    Sat Dec  7 16:15:47 CST 2019
    [root@hadoop04 ~]# cat date.txt 
    Sat Dec  7 16:15:47 CST 2019
    

    命令排序

    1.; 
    不具备逻辑判断
    
    # 即使;前面的命令执行失败,;后面的命令依旧会执行,所以;不具备逻辑判断的功能
    [root@hadoop04 ~]# cd /home/444444;ls
    -bash: cd: /home/444444: No such file or directory
    anaconda-ks.cfg  date.txt  test.txt
    
    
    2. && || 
    具备逻辑判断  
    
    # && 前面的命令执行失败,&& 后面的命令不会执行;反之,&& 前面的命令执行成功,&& 后面的命令才会执行
    [root@hadoop04 ~]# cd /home/444444 && ls
    -bash: cd: /home/444444: No such file or directory
    [root@hadoop04 ~]# cd /home/sysadmin && ls
    test.txt
    
    
    # || 前面的命令执行失败,|| 后面的命令才会执行;反之,|| 前面的命令执行成功,|| 后面的命令就不会执行
    
    [root@hadoop04 ~]# cd /home/444444 || ls
    -bash: cd: /home/444444: No such file or directory
    anaconda-ks.cfg  date.txt  test.txt
    [root@hadoop04 ~]# cd /home/sysadmin || ls
    [root@hadoop04 sysadmin]# 
    

    注意

    command & 后台执行
    command &>/dev/null 混合重定向(标准输出 1,错误输出 2)
    command1 && command2 命令排序,逻辑判断  
    

    shell 通配符

    shell 通配符(元字符),表示的不是本意

    * 星号

    *匹配任意多个字符

    ls in* 
    
    rm -rf * 
    
    rm -rf *.pdf
    
    find / -iname "*-eth0"
    

    ? 问号

    ? 匹配任意一个字符

    touch love loove live l7ve; 
    
    ll l?ve
    

    [] 中括号

    [] 匹配括号中任意一个字符

    [abc] [a-z] [0-9] [a-zA-Z0-9]

    ll l[io]ve 
    ll l[^a-z]ve 
    ll /dev/sd[a-z]
    

    () 小括号

    () 在子 shell 中执行

    (cd /boot;ls)
    (umask 077; touch file1000)
    

    在子shell(subshell)中执行命令

    (命令)
    

    在子shell中执行脚本

    ./xxxx/xxxx/xxxx.sh
    
    bash /xxxx/xxxx/xxxx.sh
    

    在当前shell中执行脚本

    source /xxxx/xxxx/xxxx.sh
    
    . /xxxx/xxxx/xxxx.sh(脚本与.之间有空格)
    

    {} 大括号

    {} 集合

    touch file{1..9}
    mkdir /home/{111,222} 
    mkdir -pv /home/{333/{aaa,bbb},444}
    
    # cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.old
    
    # cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,ifcfg-eth0.old}
    
    # cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0{,.old}  
    

    在子shell(subshell)中执行命令

    (命令)
    

    在子shell中执行脚本

    /xxxx/xxxx/xxxx.sh
    
    bash /xxxx/xxxx/xxxx.sh
    

    在当前shell中执行脚本

    source /xxxx/xxxx/xxxx.sh
    
    . /xxxx/xxxx/xxxx.sh
    

    转义符

    转义符,让元字符回归本意

    # echo *
    # echo *
    
    # touch yang sheng
    
    [root@hadoop04 ~]# echo \
    
    
    [root@hadoop04 ~]# echo -e "atb"
    atb
    [root@hadoop04 ~]# echo -e "a	b"
    a	b
    [root@hadoop04 ~]# echo -e "anb"
    anb
    [root@hadoop04 ~]# echo -e "a
    b"
    a
    b 
    
  • 相关阅读:
    【ALearning】第三章 Android基本常见控件
    【问题汇总】ListView的FooterView设置可见性的问题
    shell重定向调试信息
    Android提供的系统服务之--TelephonyManager(电话管理器)
    工作一年的总结
    【甘道夫】HBase基本数据操作的详细说明【完整版,精绝】
    未定义标识符_ConnectionPtr
    POJ 1329 三角外接圆
    IOS开发-加载本地音乐
    SQL Server 性能优化3 该指数(Index)保养
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/12014902.html
Copyright © 2011-2022 走看看