Shell基本了解

2021-09-10 21:15:24 字數 2417 閱讀 7223

shell是linux的一外殼,它包在linux核心的外面,為使用者和核心之間的互動提供了乙個介面,當使用者下達指令給作業系統的時候,實際上是把指令告訴shell,經過shell解釋,處理後讓核心做出相應的動作,系統的回應和輸出的資訊也由shell處理,然後顯示在使用者的螢幕上

二、什麼是shell指令碼?

簡單的說,當命令或者程式不在命令列執行,而是通過乙個程式檔案來執行,這個程式就被稱為shell指令碼,也就是在shell指令碼裡內建了多條命令,語句,迴圈控制,然後將這些命令一次性執行完畢,這種通過檔案執行命令的方式稱為非互動式

乙個完整的shell指令碼需要有以下幾個部分:

指令碼名:最好以.sh結尾

第一行:#!/bin/bash

指定直譯器:由哪個程式來執行指令碼內容

#!:幻數

注意:#!/bin/bash必須寫在第一行,否則會被認為是注釋

有使用者判斷,否則任何使用者都可以執行這個指令碼

有流程控制,否則只是把簡單的命令進行順序操作,沒有成功與否的判斷

注釋:可以命令後,也可以自成一行(方便自己也方便他人)

1.sh

script.sh | bash script.sh

沒有執行許可權時

[root@localhost ~]# sh log.sh

logs cleaned up…

2.path/script.sh | ./script(要在指令碼的當前目錄下)   

絕對路徑,需要給指令碼新增執行許可權

[root@localhost ~]# /root/log.sh

bash: /root/log.sh: permission denied ##沒有執行許可權時,不可執行

[root@localhost ~]# chmod +x log.sh

[root@localhost ~]# /root/log.sh ##新增許可權後正常執行

logs cleaned up…

3.source script.sh | . script.sh

這種方式會使用source或.號來讀如入指定shell檔案,並會把其他shell中的變數值或函式返回給父shell繼續使用

前兩種方式,在執行指令碼的時候,會預設開啟乙個新的shell,而新shell的變數值和函式不會返回給父shell

[root@localhost mnt]# vim test.sh

#!/bin/bash

username=zafkiel

[root@localhost mnt]# sh test.sh

[root@localhost mnt]# echo $username ##無返回值

[root@localhost mnt]# source test.sh

[root@localhost mnt]# echo $username ##有返回值

expr命令

[root@server mnt]# a=123

[root@server mnt]# expr $a + 10

133[root@server mnt]# expr $a - 10

113[root@server mnt]# expr $a * 10 (此命令不能識別*,必須加)

expr: syntax error

[root@server mnt]# expr $a * 10

1230

[root@server mnt]# expr $a / 10

12[root@server mnt]# expr $a % 10

3[root@server mnt]# echo $[a+10]

20[root@server mnt]# echo $[a-10]

0[root@server mnt]# echo $[a*10]

100[root@server mnt]# echo $[a/10]

1[root@server mnt]# echo $[a%10]

0[root@server mnt]# echo $((a+10))

20[root@server mnt]# echo $((a-10))

0let命令在執行後會儲存新的值

[root@server mnt]# let a+=10

[root@server mnt]# echo $a

20[root@server mnt]# let a-=10

[root@server mnt]# echo $a

10[root@server mnt]# let a*=10

[root@server mnt]# echo $a

100[root@server mnt]# let a/=10

[root@server mnt]# echo $a

10[root@server mnt]# let a%=10

[root@server mnt]# echo $a

0

了解shell及shell基本命令

一 shell是什麼?shell就是乙個應用程式,我們可以通過串列埠等給它傳送命令,回車它就會去執行這些命令。二 shell應用程式執行過程 1 接收資料並回顯 2 當接收到回車時解析輸入的字串,在path裡的目錄下尋找程式,執行程式。三 如何在path新增目錄?1 在path中臨時新增目錄 終端輸...

SHELL簡單了解

9.1.1 定義 一種命令直譯器 9.1.2 shell執行命令過程 1.讀取命令 2.根據引用規則對命令做展開,同時進行別名擴充套件 別名擴充套件 alias name1 str 3.將符號解析為簡單或者複雜命令 複雜命令 4.進行各種shell擴充套件 波浪號擴充套件 引數和變數擴充套件 命令擴...

Linux基本了解

vm具有快照功能,轉殖 分割槽型別 格式化 格式化是為了寫入檔案系統 等大的塊及許可權時間資訊表 不是為了刪除檔案 dos使用fat windows95公升級到fat16,fat16單個檔案不能超過2g windows98公升級到fat32,fat32單個檔案不能超過4g,u盤拷不了 windows...