溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么理解shell

發(fā)布時間:2021-11-18 16:31:38 來源:億速云 閱讀:117 作者:柒染 欄目:云計算

怎么理解shell,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

Shell是系統(tǒng)的用戶界面,提供了用戶與內(nèi)核進(jìn)行交互操作的一種接口。它接收用戶輸入的命令并把它送入內(nèi)核去執(zhí)行。

實際上Shell是一個命令解釋器,它解釋由用戶輸入的命令并且把它們送到內(nèi)核。不僅如此,Shell有自己的編程語言用

于對命令的編輯,它允許用戶編寫由shell命令組成的程序。Shell編程語言具有普通編程語言的很多特點,比如它也有

循環(huán)結(jié)構(gòu)和分支控制結(jié)構(gòu)等,用這種編程語言編寫的Shell程序與其他應(yīng)用程序具有同樣的效果。

我們可以使用SHELL實現(xiàn)對Linux系統(tǒng)的大部分管理例如:

1. 文件管理

2. 用戶管理

3. 權(quán)限管理

4. 磁盤管理

5. 軟件管理

6. 網(wǎng)絡(luò)管理

......

使用Shell的兩種方式:

輸入命令 效率低 適合少量的工作

Shell Script 效率高 適合完成復(fù)雜,重復(fù)性工作

內(nèi)容提要:

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 語法

=====================

命令 選項 參數(shù)

[root@tianyun ~]# ls

[root@tianyun ~]# ls -a

[root@tianyun ~]# ls -a /home

命令:整條shell命令的主體

選項:會影響會微調(diào)命令的行為 //通常以 -, --

參數(shù):命令作用的對象

三、bash基本特性

1. 自動補(bǔ)全<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 終止前臺運(yùn)行的程序 //ping 10.18.40.100

^D 退出 等價exit

^L 清屏

^A 光標(biāo)移到命令行的最前端 //編輯命令

^E 光標(biāo)移到命令行的后端 //編輯命令

^U 刪除光標(biāo)前所有字符 //編輯命令

^K 刪除光標(biāo)后所有字符 //編輯命令

^R 搜索歷史命令,利用關(guān)鍵詞

Alt+. 引用上一個命令的最后一個參數(shù),等價于!$

ESC . 引用上一個命令的最后一個參數(shù),等價于!$

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

# cat ESC .

3. 歷史命令

# history

a. 光標(biāo)上下鍵

b. ^R //搜索歷史命令(輸入一段某條命令的關(guān)鍵字:必須是連續(xù)的)

c. !220 //執(zhí)行歷史命令中第220條命令

!字符串 //搜索歷史命令中最近一個以xxxx字符開頭的命令,例如!ser

!$ //引用上一個命令的最后一個參數(shù)

示例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' //建立別名(臨時的,僅在當(dāng)前Shell生效)

[root@tianyun ~]# unalias tianyun //取消tianyun這個別名

[root@tianyun ~]# alias //查看系統(tǒng)當(dāng)前的別名

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 //別名優(yōu)先

[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 只列出目錄名,不列出其他內(nèi)容

-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時間

系統(tǒng)時間,即Linux系統(tǒng)時間

2. man 手冊名 (針對命令幫助,針對配置文件幫助,針對函數(shù)幫助)

[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

命令幫助: 章節(jié)1,章節(jié)8

函數(shù)幫助: 章節(jié)2,章節(jié)3

文件格式: 章節(jié)5

一般情況是不需要使用章節(jié)號,例如:

# man ls

# man useradd

# man setfacl (/EXAMPLES)

技巧1:按章節(jié)查詢

/usr/bin/passwd 修改用戶口令命令

/etc/passwd 包含用戶信息的配置文件

# man -f passwd 列出所有章節(jié)中的passwd手冊

# man 1 passwd passwd命令的幫助

# man 5 passwd 用戶配置文件的幫助

技巧2:在所有章節(jié)中查詢

# man -a passwd

關(guān)于怎么理解shell問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI