怎么理解shell

发布时间:2021-11-18 16:31:38 作者:柒染
来源:亿速云 阅读:108

怎么理解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

2. 快捷键

^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 .

3. 历史命令

# 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

一周之后讲

4. 命令别名

[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系统时间

2. 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

关于怎么理解shell问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 如何理解shell - 0
  2. 如何理解SHELL

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell

上一篇:怎么关闭mysql_history文件

下一篇:MySQL的OPTIMIZE TABLE操作的建议有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》