Linux基础-Bash简介

[root@oldboyedu ~]# who
root tty1 2018-08-08 18:51
root pts/0 2018-08-08 17:57 (10.0.0.1)
root pts/1 2018-08-08 18:51 (10.0.0.1)
root pts/2 2018-08-08 18:51 (10.0.0.1)
test1 tty2 2018-08-08 18:55
test2 tty3 2018-08-08 18:55
test3 tty4 2018-08-08 18:55

[root@oldboyedu ~]# w
18:59:36 up 1:03, 7 users, load average: 0.01, 0.03, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 18:51 2:16 0.04s 0.04s -bash
root pts/0 10.0.0.1 17:57 0.00s 0.06s 0.05s w
root pts/1 10.0.0.1 18:51 3:20 0.01s 0.00s -bash
root pts/2 10.0.0.1 18:51 8:00 0.00s 0.00s -bash
test1 tty2 18:55 4:00 0.01s 0.01s -bash
test2 tty3 18:55 4:00 0.00s 0.00s -bash
test3 tty4

pkill -9 -t tty2
-t 终端名称
-9 强制

tty表示本地终端登录
pts表示远程登录

  1. Linux系统提供的用户界面

字符界面
shell
bash(默认)
图形化界面
Gnome
Kde

echo $SHELL
查看当前系统使用的shell版本(默认bash)
[root@bogon ~]# echo $SHELL
/bin/bash

cat /etc/shells
查看当前系统支持的shell版本有哪些
[root@bogon ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@bogon ~]#

[root@bogon ~]# type sh
sh is /usr/bin/sh

[root@bogon ~]# ll /usr/bin/sh*
lrwxrwxrwx. 1 root root 4 Feb 17 19:17 /usr/bin/sh -> bash

sh --> bash的软(符号)链接--(类似于windows快捷方式)

bash

nologin

date
查看时间
一般使用 date +格式 的方式来显示我们所需要的时间格式
%F YYYY-mm-dd %Y-%m-%d
%R HH:MM %H-%M
%S
[root@bogon ~]# date
Mon Feb 25 23:58:44 CST 2019
[root@bogon ~]# date +%F_%R_%S
2019-02-25_23:58_46
[root@bogon ~]# date +%F_%R:%S
2019-02-26_00:09:18
[root@bogon ~]#

[root@bogon ~]# touch fix_trade-date +%F_%R:%S.log
[root@bogon ~]# ll | grep log
-rw-r--r-- 1 root root 0 Feb 26 00:11 fix_trade-2019-02-26_00:11:37.log
[root@bogon ~]#

推荐使用该方式

[root@bogon ~]# touch fix_trade-$(date +%F_%R:%S).log
[root@bogon ~]# ll | grep log
-rw-r--r-- 1 root root 0 Feb 26 00:11 fix_trade-2019-02-26_00:11:37.log
-rw-r--r-- 1 root root 0 Feb 26 00:13 fix_trade-2019-02-26_00:13:04.log
[root@bogon ~]#

  1. Bash特性: 命令快捷键
    终端快捷键的使用可以帮助我们大大提升效率
    Ctrl + A -- 光标跳转至正在输入的命令行的首部(hone)
    Ctrl + E -- 光标跳转至正在输入的命令行的尾部(end)
    Ctrl + C -- 终止前台运行的程序
    Ctrl + D -- 在shell中,'ctrl+d'表示退出当前shell
    Ctrl + Z -- 将任务暂停,挂至后台
    Ctrl + L -- 清屏,和clear命令等效
    Ctrl + K -- 删除从光标到行末的所有字符
    Ctrl + U -- 删除从光标到行首的所有字符
    Ctrl + R -- 搜索历史命令,利用关键字
    执行包含搜索的关键字的,最近的上一条命令

在命令行前面加 "#" 则该命令不会被执行

history
查看系统命令历史,追溯之前发生情况

命令选项:
-w 保存命令历史到历史文件
-c 清空命令历史记录,不会清空文件
-d 删除命令历史的第n条
!可以调用之前的命令
!num 调用第num行的命令
!! 调用上一条命令
!cmd 调用离当前最近以该cmd开头的
!$ 调用上一次命令后面的参数

命令别名
alias
alias cmd='cmd' 声明别名
unalias cmd 取消别名
注意:
1. 使用alias命令就可以查看当前生效的别名有哪些
2. 使用unalias接别名名称
3. alias只对当前环境临时生效
4. 如需永久生效,要将定义别名的命令写入登录配置文件
~/.bash_profile 只对当前用户生效,局部配置文件
~/.bashrc 只对当前用户生效,局部配置文件

        /etc/profile    对所有用户生效,全局配置文件
        /etc/bashrc     对所有用户生效,全局配置文件
  1. Bash特性:

当我们执行一个命令,整个命令的执行流程如下:

1.  判断命令是否通过绝对路径执行
2.  判断命令是否存在别名
3.  判断 用户输入的是内部命令还是外部命令
        内部命令:  shell程序自带的命令
        外部命令:  在系统PATH变量的某个路径下的可执行程序
    使用type命令检测用户输入的命令属于内部命令还是外部命令
4.  内部命令直接执行,外部命令检测是否存在缓存
5.  检测PATH路径,有则执行,无则报错

事实上过的外部命令都会保存在内存缓存中,当再次执行相同的命令,会通过缓存调取执行,
也就意味着不会搜索PATH路径

[root@wll ~]# type tree
tree is /usr/bin/tree
[root@wll ~]# type sh
sh is /usr/bin/sh
[root@wll ~]# type make
make is /usr/bin/make
[root@wll ~]# type git
git is /usr/bin/git
[root@wll ~]# hash
hits command
1 /usr/bin/cat
2 /usr/bin/yum
1 /usr/bin/ls
[root@wll ~]# type yum
yum is hashed (/usr/bin/yum)
[root@wll ~]#

--hash表示命令缓存所在的位置

[root@bogon ~]# hash
hits command
3 /usr/bin/more
3 /usr/bin/cat
1 /usr/bin/touch
1 /usr/bin/less
[root@bogon ~]# ls
anaconda-ks.cfg fix_trade-2019-02-26_00:11:37.log fix_trade-2019-02-26_00:13:04.log index.html song-2019-02-26_00:10:55.mp3 song-2019-02-26_00:35:01.mp3 ssx-test
[root@bogon ~]# hash
hits command
3 /usr/bin/more
3 /usr/bin/cat
1 /usr/bin/touch
1 /usr/bin/less
1 /usr/bin/ls
[root@bogon ~]#

--已缓存命令,如果移动位置会导致无法找到该命令
[root@server ~]# mv /sbin/ifconfig /bin/
[root@server ~]# ifconfig
-bash: /sbin/ifconfig: No such file or directory

--删除缓存过的ifconfig命令, 即可执行
[root@server ~]# hash -d ifconfig
[root@server ~]# ifconfig

--当然可以清空缓存表
[root@server ~]# hash -r

--命令缓存hash需要注意如下情况:
1. 如果执行外部命令一次就会对该命令进行缓存
2. 如果将命令移动了位置
a. 使用绝对路径执行
b. 删除hash表的缓存命令

总结:
当我们执行了一个ping命令之后,整个命令执行的流程步骤如下:

  1. 检查执行的是否是绝对路径

  2. 检查该ping命令是否存在别名

  3. 检查ping命令是内部命令还是外部命令

  4. 如果是内部执行直接执行,如果是外部命令

  5. 检测该命令是否有缓存,如果没有查找PATH变量

  6. 检查PATH路径知道查找到该命令然后执行

  7. 如果没有找到该命令,则返回报错: command not found

  8. Bash特性: 路径展开
    Linux shell下路径展开用花括号包括,用逗号分隔;这样花括号里的内容会被展开形成列表

[root@server ~]# mkdir /tmp/zz/a/b /tmp/yy/a/b -pv
mkdir: created directory /tmp/zz' mkdir: created directory /tmp/zz/a'
mkdir: created directory /tmp/zz/a/b' mkdir: created directory /tmp/yy'
mkdir: created directory /tmp/yy/a' mkdir: created directory /tmp/yy/a/b'

--删除目录
[root@server ~]# rm -rf /tmp/{zz,yy}

--通过路径展开方式创建目录
[root@server ~]# mkdir /tmp/{zz,yy}/a/b -pv
mkdir: created directory /tmp/zz' mkdir: created directory /tmp/zz/a'
mkdir: created directory /tmp/zz/a/b' mkdir: created directory /tmp/yy'
mkdir: created directory /tmp/yy/a' mkdir: created directory /tmp/yy/a/b'

思考:在/tmp 路径下创建以下目录
etc/init.d
etc/sysconfig
usr/lib
usr/bin
usr/include
var/spool
var/run
proc
sys
bin

/tmp/
├── etc
└── init.d
└── sysconfig
├── usr
└── bin
└── lib
└── include
└── var
└── spool
└── run
└── proc
└── sys
└── bin

[root@wll ~]# mkdir -pv /tmp/{etc{init.d,sysconfig},usr{bin,lib,include},var{spool,run},proc,sys,bin}

  1. Bash特性: 转义字符
    Shell解释器提供了非常丰富的转移字符,来实现字符处理以及命令替换
    -- 反斜杠,使反斜杠后面的一个变量变为字符串
    '' -- 单引号,转义其中所有的变量为单纯的字符串
    "" -- 双引号,保留其中的变量属性,不进行转义处理
    `` -- 反引号,把其中的命令执行后返回结果

-- " m *" == "rm -f *" 去掉rm的alias的别名作用
-- "touch 1 3.txt" 将空格转义为真正的空格,而不是参数的分隔符

[root@bogon ~]# cd ssx-test/
[root@bogon ssx-test]# ls
test.ext
[root@bogon ssx-test]# touch 1 3.txt
[root@bogon ssx-test]# ls
1 3.txt test.ext
[root@bogon ssx-test]# rm -f *
[root@bogon ssx-test]# ls
[root@bogon ssx-test]# touch 1 3.txt
[root@bogon ssx-test]# ls
1 3.txt
[root@bogon ssx-test]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 9 01:49 1 3.txt
[root@bogon ssx-test]# touch 1 3.txt
[root@bogon ssx-test]# ls
1 1 3.txt 3.txt
[root@bogon ssx-test]# m *
[root@bogon ssx-test]# ls
[root@bogon ssx-test]#

--将特殊字符转换为正常字符
[root@server ~]# echo "shoping is $500"
shoping is 00
[root@server ~]# echo "shoping is $500"
shoping is $500

--转义其中特殊字符为普通字符串
[root@server ~]# echo 'shoping is $500'
shoping is $500

--使用$()实现命令替换
[root@server ~]# echo "The Directory is $(pwd)"
The Directory is /root
--使用``实现命令替换
[root@server ~]# echo "The Directory is pwd"
The Directory is /root

//转义其中所有的变量为单纯的字符串
[root@server ~]# touch file-date +%F-%H-%S
[root@server ~]# ls
file-2017-12-03-04-18

[root@server ~]# touch 'file-date +%F-%H-%S'
[root@server ~]# ls
file-2017-12-03-04-18 file-date +%F-%H-%S

运维工程师进阶之路
1、能部署固定环境
2、能自动化运维(综合实验架构LNMP、rsync、NFS、ansible、zabbix、tomcat)
3、shell脚本开发
4、数据库(mysql redis mha)
5、架构师(openstack、docker,git)
6、python

原文地址:https://www.cnblogs.com/s-sx/p/11841949.html