好程序员云计算分享初识shell

好程序员云计算分享初识shell,Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用
于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有
循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。

我们可以使用SHELL实现对Linux系统的大部分管理例如:

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

......
  使用Shell的两种方式:
输入命令 效率低 适合少量的工作
Shell Script 效率高 适合完成复杂,重复性工作

内容提要:
bash shell提示符
shell 语法
bash 特性
Linux获得帮助

一、bash shell提示符:

[root@tianyun ~]# echo $PS1
[u@h W]$

[root@tianyun ~]# date
2019年 10月 24日 星期三 09:38:54 CST

[root@tianyun ~]# whoami
root

[root@tianyun ~]# useradd jack
[root@tianyun ~]# passwd jack
Changing password for user jack.
New UNIX password:
BAD PASSWORD: it is WAY too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

  二、shell 语法

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

命令:整条shell命令的主体
选项:会影响会微调命令的行为 //通常以 -, --
参数:命令作用的对象
  三、bash基本特性

  1. 自动补全<tab>

ls /etc/sysconfig/network-scripts/

ls /etc/sysconfig/network-scripts/ifcfg-eth0

cat /etc/sysconfig/network-scripts/ifcfg-eth0

systemctl restart crond.service

date -s 12:30

  1. 快捷键

^C 终止前台运行的程序 //ping 10.18.40.100
^D 退出 等价exit
^L 清屏
^A 光标移到命令行的最前端 //编辑命令
^E 光标移到命令行的后端 //编辑命令
^U 删除光标前所有字符 //编辑命令
^K 删除光标后所有字符 //编辑命令
^R 搜索历史命令,利用关键词

Alt+. 引用上一个命令的最后一个参数,等价于!$
ESC . 引用上一个命令的最后一个参数,等价于!$

ls /etc/sysconfig/network-scripts/ifcfg-eth0

cat ESC .

  1. 历史命令

history

a. 光标上下键
b. ^R //搜索历史命令(输入一段某条命令的关键字:必须是连续的)
c. !220 //执行历史命令中第220条命令
!字符串 //搜索历史命令中最近一个以xxxx字符开头的命令,例如!ser
!$ //引用上一个命令的最后一个参数
示例1:
[root@instructor ~]# ls /root /home
[root@instructor ~]# cd !$
cd /home

示例2:
[root@instructor ~]# ls /root /home
[root@instructor ~]# touch !$/file1
touch /home/file1

示例3:
[root@instructor ~]# systemctl restart crond
[root@instructor ~]# ls
[root@instructor ~]# date
[root@instructor ~]# !sy

一周之后讲

  1. 命令别名

[root@tianyun ~]# alias tianyun='cat /etc/sysconfig/network-scripts/ifcfg-eth0' //建立别名(临时的,仅在当前Shell生效)
[root@tianyun ~]# unalias tianyun //取消tianyun这个别名

[root@tianyun ~]# alias //查看系统当前的别名
ll='ls -l --color=tty'
[root@tianyun ~]# ll

[root@tianyun ~]# /bin/ls
[root@tianyun ~]# /bin/ls --color
[root@tianyun ~]# type -a ls //查看命令类型
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls

[root@tianyun ~]# /bin/ls
[root@tianyun ~]# /usr/bin/ls
[root@tianyun ~]# ls //别名优先
[root@tianyun ~]# ls //跳过别名

[root@tianyun ~]# cp -rf /etc /tmp //第一次
[root@tianyun ~]# cp -rf /etc /tmp //第二次
[root@tianyun ~]# cp -rf /etc /tmp

[root@tianyun ~]# type -a cp
cp is aliased to ‘cp -i’
cp is /usr/bin/cp
cp is /bin/cp

永久别名:
/etc/bashrc shell配置文件之一
[root@tianyun ~]# gedit /etc/bashrc //添加如下行
alias tianyun='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
  四、Linux获得帮助

  1. 命令 --help

ls --help

用法:ls [选项]... [文件]...

ls 常见选项
-a all,查看目录下的所有文件,包括隐藏文件
-l 长列表显示
-h human 以人性化方式显示出来
-d 只列出目录名,不列出其他内容
-t 按修改时间排序
-S 按文件的Size排序
-r 逆序排列reverse
-i 显示文件的inode号(索引号)
图片描述

  # date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

date

date +%H

date +%F

date 0214080019

date 0214080019.30

date -s 12:00

[root@tianyun ~]# touch date +%F_file.txt
[root@tianyun ~]# ls
2017-07-24_file.txt
  两种时间:
硬件时间,即主板BIOS时间
系统时间,即Linux系统时间

  1. man 手册名 (针对命令帮助,针对配置文件帮助,针对函数帮助)

图片描述

[root@tianyun ~]# man man
MANUAL SECTIONS
The standard sections of the manual include:

1 User Commands

2 System Calls

3 C Library Functions

4 Devices and Special Files

5 File Formats and Conventions

6 Games et. Al.

7 Miscellanea

8 System Administration tools and Deamons

命令帮助: 章节1,章节8
函数帮助: 章节2,章节3
文件格式: 章节5

一般情况是不需要使用章节号,例如:

man ls

man useradd

man setfacl (/EXAMPLES)

技巧1:按章节查询
/usr/bin/passwd 修改用户口令命令
/etc/passwd 包含用户信息的配置文件

man -f passwd 列出所有章节中的passwd手册

man 1 passwd passwd命令的帮助

man 5 passwd 用户配置文件的帮助

技巧2:在所有章节中查询

man -a passwd

  1. 官方手册

docs.redhat.com Enterprise Linux (5/6/7) Linux系统相关帮助指南
www.mysql.com docs MySQL使用指南
www.zabbix.com Documentation Zabbix使用指南
图片描述

  1. baidu, google 应用或系统的报错信息

评论

暂无评论