怎么理解shell

58次阅读
没有评论

共计 3510 个字符,预计需要花费 9 分钟才能阅读完成。

怎么理解 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 问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注丸趣 TV 行业资讯频道了解更多相关知识。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计3510字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)