我們知道計算機是無法直接識别我們鍵入的指令的,在計算機語言裡隻有二進制的0和1,所有信息都将轉化為由0和1組成的代碼進行存儲和傳輸。那麼我們和計算機之間進行交互就需要有一個翻譯官來翻譯我們鍵入的指令以便計算機能夠識别,在Linux系統中這個翻譯官正是由Shell來擔任的。簡單來說,Shell就是用戶與Linux内核之間的解釋器,負責将用戶的指令翻譯為内核可以識别的指令。
1.2 常見Shell解釋器常見的Shell解釋器有:
/bin/bash
/bin/sh
/bin/csh
/bin/tcsh
等等。
1.3 查看Shell解釋器如何查看本機所支持的Shell解釋器有哪些呢?
可以使用命令:
cat /etc/shells
1.4 修改Shell解釋器
Linux系統中新創建的用戶默認的登錄Shell為/bin/bash,可以使用usermod或chsh命令修改用戶的登錄shell:
本系列文章使用的操作系統為CentOS7.5,Shell解釋器為/bin/bash。
Shell腳本基礎2.1 什麼是Shell腳本?簡單地說就是将Linux或類UNIX系統的命令寫入一個文件中,這個文件就是一個Shell腳本文件。
2.2 Shell腳本的書寫格式一個合格規範的腳本應該包含以下這些内容:
#!/bin/bash
# 第一個Shell腳本,輸入Hello World
echo "Hello World"
注釋的内容計算機将自動忽略,這部分内容是給閱讀者看的。
單行注釋:# 第一行的#!為标識所使用的解釋器
單行或多行注釋:<<字符
字符
腳本文件的執行方式3.1 腳本文件沒有可執行權限時
新創建的腳本文件,默認是沒有可執行權限的,不可以直接執行,此時可以使用bash、sh、source、. 來執行腳本文件。
[root@localhost shell]# chmod -x first.sh
[root@localhost shell]# ls -l first.sh
-rw-r--r-- 1 root root 122 Aug 30 11:55 first.sh
[root@localhost shell]# bash first.sh
Hello World
[root@localhost shell]# sh first.sh
Hello World
[root@localhost shell]# source first.sh
Hello World
[root@localhost shell]# . first.sh
Hello World
上述各命令執行腳本的區别:
bash/sh:執行腳本時将會開啟一個shhd子進程進行執行腳本命令
source/.:執行腳本時是直接在bash終端下執行的,沒有開啟子進程。
3.2 腳本文件有可執行權限時腳本文件分配了可執行權限後,可以直接使用絕對路徑或相對路徑來執行腳本文件。
[root@localhost shell]# ./first.sh
Hello World
[root@localhost shell]# /opt/shell/first.sh
Hello World
功能描述:echo命令主要用來輸出顯示字符串信息。
語法格式:echo [選項] 字符串
常用選項:
[root@localhost shell]# echo "Hello World" #echo默認換行輸出
Hello World
[root@localhost shell]# echo -n "Hello World" #添加-n選項不換行
Hello World[root@localhost shell]# echo -e "\033[031mHello World\033[0m" # 使用-e選項輸出帶顔色的字符串
Hello World
# 在第3行,第10列輸出紅色字體的Hello World
[root@localhost shell]# echo -e "\033[31m\033[3;10H Hello World\033[0m"
Hello World
4.1.2 printf 命令
功能描述:格式化輸出數據,printf默認不換行輸出
語法格式:printf [格式] 參數(一般參數就是要輸出的内容)
常用的格式字符串及功能描述如下表:
格式字符 |
功能描述 |
%d或%i |
十進制整數 |
%o |
八進制整數 |
%x |
十六進制整數 |
%u |
無符号十進制整數 |
%f |
浮點數(小數點數) |
%s |
字符串 |
\b |
退格鍵 |
\f |
換行且光标仍停留在原位置 |
\n |
換行且光标移至行首 |
\r |
光标移至行首,但不換行 |
\t |
Tab鍵 |
[root@localhost shell]# printf "%d" 23 #不換行輸出數字
23[root@localhost shell]# printf "%d\n" 23 # 換行輸出數字
23
[root@localhost shell]# printf "|]|\n" 23 #換行輸出數字并且寬度為5右對齊
| 23|
[root@localhost shell]# printf "|%-5d|\n" 23 #換行輸出數字并且寬度為5左對齊
|23 |
[root@localhost shell]# printf "|%-5s|\n" aaa #左對齊輸出字符串
|aaa |
[root@localhost shell]# printf "|%5s|\n" aaa #右對齊輸出字符串
| aaa|
[root@localhost shell]# printf "%3.2f\n" 626.327237 #輸出浮點數,保留兩位小數
626.33
[root@localhost shell]# printf "%.3f\n" 626.327237 #輸出浮點數,保留三位小數
626.327
功能描述:可以從标準輸入中讀取一行數據
語法格式:read [選項] [變量名]
默認變量名為REPLY。
常用選項:
[root@localhost shell]# read key1
aaa
[root@localhost shell]# echo $key1
aaa
[root@localhost shell]# read key1 key2 key3
11 22 33
[root@localhost shell]# echo $key1
11
[root@localhost shell]# echo $key2
22
[root@localhost shell]# echo $key3
33
[root@localhost shell]# read -p "請輸入賬号:" uname
請輸入賬号:test
[root@localhost shell]# echo $uname
test
[root@localhost shell]# read -s -p "請輸入密碼:" passwd
請輸入密碼:[root@localhost shell]# echo $passwd
123456
在Linux系統中輸出可以分為标準輸出和标準錯誤輸出。标準輸出的文件描述符為1,标準錯誤輸出的描述符為2。而标準輸入的文件描述符為0。
如果希望改變輸出信息的方向,可以使用>或>>符号将輸出信息重定向到文件中。
重定向符号與功能描述表
符号 |
功能描述 |
1> |
标準輸出覆蓋重定向 |
1>> |
标準輸出追加重定向 |
2> |
标準錯誤輸出覆蓋重定向 |
2>> |
标準錯誤輸出追加重定向 |
&> |
标準輸出和标準錯誤輸出覆蓋重定向 |
&>> |
标準輸出和标準錯誤輸出追加重定向 |
< |
輸入重定向,後面需要跟一個文件名 |
<< |
可以将數據内容重定向傳遞給前面的一個命令,作為命令的輸入 |
[root@localhost shell]# echo "Hello the world" > test.txt
[root@localhost shell]# cat test.txt
Hello the world
[root@localhost shell]# echo "My Shell Scripts" > test.txt
[root@localhost shell]# cat test.txt
My Shell Scripts
[root@localhost shell]# echo "test file" >> test.txt
[root@localhost shell]# cat test.txt
My Shell Scripts
test file
[root@localhost shell]# ls /etc/hosts /abc > test.txt
ls: cannot access /abc: No such file or directory
[root@localhost shell]# cat test.txt
/etc/hosts
[root@localhost shell]# ls /etc/hosts /abc 2> test.txt
/etc/hosts
[root@localhost shell]# cat test.txt
ls: cannot access /abc: No such file or directory
[root@localhost shell]# ls /etc/hosts /abc &> test.txt
[root@localhost shell]# cat test.txt
ls: cannot access /abc: No such file or directory
/etc/hosts
[root@localhost shell]# ls /etc/hosts /abc > ok.txt 2> error.txt
[root@localhost shell]# cat ok.txt
/etc/hosts
[root@localhost shell]# cat error.txt
ls: cannot access /abc: No such file or directory
另外,可以使用2>&1将錯誤輸出重定向到标準輸出,也可以使用1>&2将标準輸出重定向到錯誤輸出。
[root@localhost shell]# ls /abc > ok.txt 2>&1
[root@localhost shell]# cat ok
cat: ok: No such file or directory
[root@localhost shell]# echo "hello" 2> error.txt 1>&2
[root@localhost shell]# cat error.txt
Hello
Linux系統中有一個特殊的設備文件/dev/null,這是一個黑洞。無論往該文件中寫入多少數據,都會被系統吞噬、丢棄。
[root@localhost shell]# echo "hello" > /dev/null
[root@localhost shell]# ls /abc 2> /dev/null
[root@localhost shell]# ls /etc/hosts /abc &> /dev/null
例:将/etc/hosts文件中的内容輸入重定向到郵件内容中,發送給root用戶
[root@localhost shell]# mail -s warning root@localhost < /etc/hosts
[root@localhost shell]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Mon Aug 30 15:47 19/769 "warning"
& 1
Message 1:
From [email protected] Mon Aug 30 15:47:16 2021
Return-Path: <[email protected]>
X-Original-To: root@localhost
Delivered-To: [email protected]
Date: Mon, 30 Aug 2021 15:47:16 0800
To: [email protected]
Subject: warning
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
& exit
<<符号(也稱為Here Document)代表你需要的内容在這裡。
[root@localhost shell]# vim second.sh
#!/bin/bash
# cat 通過Here Document讀取數據,再通過輸出重定向将數據導入文件
cat > /opt/shell/test.txt << HERE
該文件為測試文件。
<<輸入重定向測試。
HERE
[root@localhost shell]# chmod x second.sh
[root@localhost shell]# ./second.sh
[root@localhost shell]# cat test.txt
該文件為測試文件。
<<輸入重定向測試。
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!