雲計算面試常見問題,怎麼理解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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章