1. 程式人生 > 實用技巧 >shell基礎01

shell基礎01

推薦電影:《模仿遊戲》

一 shell介紹

## 什麼是程式語言?
程式語言的本質就是一門語言,而語言是用來一種事物和另外一種事物溝通的介質

人=============》程式語言=============》計算機


##什麼是程式設計
分為兩個環節
	1、想清楚做事的步驟
    2、用一種計算機能聽懂的語言(程式語言)

## 為何要程式設計?
為了讓計算機幫人去做事,從而解放人力

## 什麼是程式
程式就是一堆程式碼檔案

## 什麼是程序
程序是程式的執行過程,也可以說是作業系統幹活的過程,因為是作業系統負責控制硬體來執行應用程式

ps:程序與程序之間的記憶體空間是'互相隔離'的

## 計算機體系的三層結構

應用程式
作業系統
計算機硬體

## 什麼是shell
shell是一門程式語言,用來與計算機溝通,從而控制計算機的

## 什麼是shell直譯器
用於解釋執行shell語言語法/命令的一個'應用軟體'
[root@Centos7 ~]# chsh -l
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

## 什麼是shell script
用shell這門程式語言編寫的程式

## 執行shell程式兩種方式
方式一:互動式環境(登入使用者之後預設就會進入使用者的shell直譯器互動式環境)
	優點:輸入命令立即拿到程式碼的執行結果,即時執行
    缺點:無法永久儲存程式碼,退出互動式環境,程式碼全部丟失
方式二:把程式碼寫到檔案中,然後執行
	優點:可以永久儲存程式碼,以便後期重複使用
    缺點:無法即時執行

二 元字元

1、什麼是 元字元

元字元屬於shell這門程式語言的語法,被shell直譯器解釋的'特殊字元'

ps:grep命令解釋的特殊符號=》正則表示式,正則與元字元中的符號都是公用的,但是表示的意義截然不同

2、為何要用元字元

讓命令的操作更加豐富

3、如何用元字元

#1、~:家目錄
#2、``:取命令的執行結果
$():支援巢狀
[root@Centos7 test]# res=$(ls $(pwd))
[root@Centos7 test]# echo $res
#3、!
#3.1 !歷史命令
#3.2 !$取上一條命令的引數
#3.3 取反
對命令的結果取反:
[root@Centos7 test]# ! pwd
/test
[root@Centos7 test]# echo $?
1
[root@Centos7 test]# find /test ! -name "*.txt"

#[]表示一個範圍,!和^在[]中表示取反,一個[]表示一個字元
#一位數字.txt
[root@Centos7 test]# ls /test/[0-9].txt
#批量建立,以A到Z開頭的.png檔案
[root@web01 mnt]# touch {a..z}.png
[root@web01 mnt]# touch {A..Z}.png
[root@web01 mnt]# touch {1..10}.png
[root@web01 mnt]# touch 11.txt aa.png
#取反
[root@web01 mnt]# ll [!a-z].png
[root@web01 mnt]# ll [!A-Z].png
[root@Centos7 test]# ls [!0-9].txt
[root@Centos7 test]# ls [^0-9].txt

#查詢
[root@web01 mnt]# ll [A-Z].png		#不含a.png
[root@web01 mnt]# ll [a-z].png		#不含Z.png
[root@web01 mnt]# ll [0-9].txt		#不能寫成[0-10]

#查詢多位開頭
[root@web01 mnt]# ll [1-9][1-9].txt  #查詢以2位數字開頭的 .txt檔案或目錄
[root@web01 mnt]# ll [a-Z][a-Z].png  #查詢以2位字母開頭的 .png檔案或目錄

#4、@沒有特殊意義
#5、#註釋
原則:給關鍵程式碼加註釋
地方:
	1、程式碼的正上方單獨一行
    2、程式碼正後方
#6、$
取變數的值:
x=111
echo $x
取命令的執行結果
echo $(pwd)
#7、+、-、*、/、%:取餘數
$[]
$(())
expr
let 

bc  #計算小數

# 示例
[root@Centos7 test]# n=10
[root@Centos7 test]# echo $[$n+1]
[root@Centos7 test]# echo $[$n + 1]
11
[root@Centos7 test]# echo $[$n-1]
9
[root@Centos7 test]# echo $[$n/3]		#忽略小數
3
[root@Centos7 test]# echo $[$n%3]		#取餘數
1
[root@Centos7 test]# echo $[$n*3]
30
[root@Centos7 test]# 
[root@Centos7 test]# echo $(($n+1))
11
[root@Centos7 test]# echo $(($n-1))
9
[root@Centos7 test]# echo $(($n/3))
3
[root@web01 mnt]# echo $(($n*3))
30
[root@Centos7 test]# echo $(($n%3))
1
[root@Centos7 test]# expr $n + 1		#必須加上空格
11
[root@Centos7 test]# expr $n / 3
3
[root@web01 mnt]# expr $n \* 3			#必須加上轉義符
30
[root@Centos7 test]# expr $n+1  #  一定記得在運算子左右兩側加空格 = echo
10+1
[root@web01 mnt]# echo $n+1
10+1

# let運算子
[root@Centos7 test]# age=18
[root@Centos7 test]# age=$[$age+1]
[root@Centos7 test]# echo $age
19

[root@Centos7 test]# let age=age+1		#let支援不呼叫,直接計算
[root@Centos7 test]# echo $age
20
[root@Centos7 test]# let age+=1  # 等於 age=age+1
[root@Centos7 test]# echo $age
21

[root@Centos7 test]# unset m		#取消變數定義
[root@Centos7 test]# unset n
[root@Centos7 test]# unset x
[root@Centos7 test]# unset y
[root@Centos7 test]# let x=m++		#賦值
[root@Centos7 test]# let y=++n		#先自增,後自增賦值
[root@Centos7 test]# echo $x
0
[root@Centos7 test]# echo $y
1

[root@web01 mnt]# echo "scale=2;33/100"|bc
.33
[root@Centos7 test]# echo $(echo "scale=2;33/100" | bc |cut -d. -f2)%
33%

#特殊檔名
[root@web01 opt]# touch a1b.txt  a2b.txt  a3b.txt  a-b.txt  a+b.txt
[root@web01 opt]# ll a[1-+]b.txt		# - 符號只能放在最左和最右,[或者1或者2]
ls: cannot access a[1-+]b.txt: No such file or directory
[root@web01 opt]# ll a[a+-]b.txt
[root@web01 opt]# ll a[!-a+]b.txt